Steve, you can also modify that code so it looks something like:
If Nz(Me.txtPreferredName, "") = "" Then
MsgBox "Enter selection in 1stField!!!"
Me.txt1stField.SetFocus
ElseIf Nz(Me.cboCampus, "") = "" Then
MsgBox "Enter selection in 2ndField!!!"
Me.cbo2ndField.SetFocus
ElseIf Nz(Me.txtLastName, "") = "" Then
MsgBox "Enter selection in 3rdField!!!"
Me.txt3rdField.SetFocus
End If
Which is a lot easier to read than the format you are using.
But I would not put this in the Click event of any controls, I would put it in the BeforeUpdate event of the form. Actually, I have a function in each data entry form called PassesChecks. It returns a boolean value back to the BeforeUpdate event which sets the Cancel argument of that event and prevents the form from updating if any of the criteria is not met. It looks something like:
Private Sub Form_BeforeUpdate(Cancel as integer)
if NOT PassesChecks then exit sub
'do whatever else you want to do here
me!ModifiedBy = "steve13"
me!modifiedDT = Now()
End Sub
and
Private Function PassesChecks() as boolean
If Nz(Me.txtPreferredName, "") = "" Then
MsgBox "Enter selection in 1stField!!!"
Me.txt1stField.SetFocus
ElseIf Nz(Me.cboCampus, "") = "" Then
MsgBox "Enter selection in 2ndField!!!"
Me.cbo2ndField.SetFocus
ElseIf Nz(Me.txtLastName, "") = "" Then
MsgBox "Enter selection in 3rdField!!!"
Me.txt3rdField.SetFocus
Else
PassesChecks = true
End If
End Function
Dale's solution is probably what you want but he left out something critical. When you validate data and the validation fails, you MUST cancel the event in order to prevent the bad data from being saved.
If Not PassesChecks Then
Cancel = True
Exit Sub
End If
'do whatever else you want to do here
Me!ModifiedBy = "steve13"
Me!modifiedDT = Now()
End Sub
To be clear, most validation belongs in the FORM's BeforeUpdate event. You can NEVER put validation in click events because if the user doesn't enter a field, the validation code won't run. The BeforeUpdate event is the last Form level event that runs before a record is saved. It runs regardless of what caused the record to be saved and YOU are in control of whether or not the record get saved. Think of the BeforeUpdate event as the flapper at the end of a funnel. If you cancel the event, the flapper stays closed and the record doesn't get saved. If you don't cancel the event, the record falls through and gets saved.
PS, I'm pretty sure we've had this discussion before.
Looks like a bug in the interface. I can't get the code tag to work no matter what I do.
Great catch, Pat. What I usually do is:
Private Sub Form_BeforeUpdate(Cancel as integer)
Cancel = NOT PassesChecks
if Cancel then exit sub
'do whatever else you want to do here
me!ModifiedBy = "steve13"
me!modifiedDT = Now()
End Sub
OK, Dale, what am I doing wrong? Why does my code box put all the code on one line?
Private Function ErrorChecking()
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
If Len(Nz(ctl, "")) = 0 Then
MsgBox "Please Fill TextBox #" & ctl.Name
Exit For
End If
End If
Next
End Function
Tested and working fine..... (the previous sample had some missing 'Then'....i will never be good typing code from my mobile)
Validation in events other than the FORM's BeforeUpdate event can always be bypassed.
Open in new window
Then call it where you need to by just using the Sub Procedure Name
Open in new window
If you need to use this across more than one form then you will need to make the sub "Public" and you will need to update the textbox references to use the Form!ControlName rather than Me.Control