PDA

View Full Version : Solved: UserForm Show Input error Message before proceeding



Darren
09-04-2007, 04:00 AM
Hi Captains

Need to code an error message ...

When my UserForm, opens the user must enter an amount in the Turnover TextBox Before he/she can continue on the UserForm.

Userform is called BasicTerms

This is the first Userform that opens on running the program

The Workbook code to open is as follows

Private Sub Workbook_Open()
'hide the workbook
Application.Visible = False
'display message
MsgBox "Welcome to Your Profit Wizard" & vbNewLine & _
vbNewLine & _
"Enter your Turnover"
'show userform
BasicTerms.Show False
End Sub

The TextBox on the BasicTerms Userform that is set to tab index of 0 is called txtTurnoverIncl.

txtTurnoverIncl is the Textbox that must be filled in before moving on to cmbRoyalty....

The code as the BasicTerms opens is as follows

Option Explicit
Private Sub txtTurnoverExcl_Change()
If txtRoyalty.Text <> "" Then

lblRoyaltcost.Caption = Format(CDbl(txtTurnoverExcl.Text) * _
CDbl(txtRoyalty.Text) / 100, "#,##0.00")
End If
End Sub
Private Sub txtRoyalty_Change()
If txtTurnoverExcl.Text <> "" Then

lblRoyaltcost.Caption = Format(CDbl(txtTurnoverExcl.Text) * _
CDbl(txtRoyalty.Text) / 100, "#,##0.00")
End If
End Sub
Private Sub cmbRoyalty_Change()

If lblTurnoverExcl.Caption <> "" Then
If cmbRoyalty.Value <> "" Then

lblRoyaltcost.Caption = Format(CDbl(lblTurnoverExcl.Caption) * _
Replace(cmbRoyalty.Value, "%", "") / 100, "#,##0.00")
End If
End If
End Sub
Private Sub CommandButton1_Click()
Hide
OverHeads.TextBox14.Text = BasicTerms.lblTurnoverExcl.Caption
OverHeads.Show
End Sub
Private Sub CommandButton2_Click()
Hide
TheBasicFormulas.Show
End Sub
Private Sub CommandButton3_Click()
Hide
SalesTaxCalc.Show
End Sub
Private Sub CommandButton4_Click()
Hide
PromoCalc.Show
End Sub
Private Sub CommandButton5_Click()
Me.PrintForm
End Sub
Private Sub CommandButton6_Click()
Hide
Summary.Show
End Sub
Private Sub CommandButton7_Click()
Hide
DailyControl.Show
End Sub
Private Sub txtTurnoverIncl_AfterUpdate()
With txtTurnoverIncl
.Text = Format(.Text, "#,##0.00")
End With
End Sub
Private Sub txtTurnoverIncl_Change()
With txtTurnoverIncl
If .Text = "" Then
lblT.Caption = ""
Else
lblT.Caption = Format(Round(.Text * 14 / 114, 2), "#,##0.00")
lblTurnoverExcl.Caption = Format(CDbl(.Text - .Text * 14 / 114), "#,##0.00")
End If
End With
End Sub
Private Sub UserForm_Initialize()
Dim i As Long
For i = 0 To 35
cmbRoyalty.AddItem Format(i / 100, "0%")
Next i
End Sub


I have attached an immage for more clarity

Thanks for your attention

Darren
South Africa

Simon Lloyd
09-04-2007, 06:19 AM
at the top of your commanbutton"x"(s)_Click add something like: Call TurnOverCheckadd a sub like this:

Sub TurnOverCheck()
If txtTurnoverIncl.Value = 0 Then
MsgBox "You have not entered your turnover, Turnover box shows a value of " & txtTurnoverIncl.value
SetFocus = txtTurnoverIncl
End If
in your userform initialize you could SetFocus = txtTurnoverIncl or something along those lines.

Darren
09-04-2007, 10:16 AM
Hi Simon

Thanks for the help i will try the code

Kindest regards

Darren

Norie
09-04-2007, 10:41 AM
Darren

Why not use the Exit event of the textbox?

Darren
09-04-2007, 11:35 AM
Hi Norie and Simon

I tried to input the code as explained above but i get an error

Private Sub txtTurnoverIncl_Exit()
If txtTurnoverIncl.Value = 0 Then
MsgBox "You have not entered your turnover, Turnover box shows a value of " & txtTurnoverIncl.Value
SetFocus = txtTurnoverIncl

End Sub

Not sure about TextBox Exit so could you please show me an example or a knowledge base entry

Thaking you all
Darren

Norie
09-04-2007, 11:44 AM
Darren

This is what the code stub for the exit event should look like.


Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
End Sub
Obviously you would change TextBox1.

And instead of SetFocus you would use this to stop the user exiting.

Cancel=True

Darren
09-04-2007, 12:06 PM
Norie

It works well but i have to enter 0 for the message box to open.... and thank you for fixing the stub... thanks also to simon for the bit in the middle.

When the Userform initilizes i need to show txtTurnoverIncl with a value of 0.00 is this possible

Thanks
Darren

Simon Lloyd
09-05-2007, 12:15 AM
in your initialize use something like: txtTurnoverIncl.text="0.00"