Field validation

  • Thread starter Thread starter emma
  • Start date Start date
E

emma

Hi all!

I am trying to require that a field on a form be filled in
by the user. They should not be allowed to tab past the
field. So I want a message box to pop up when they try to
tab past the field without putting anything in it.

I was thinking that I should put the code in the Lost
Focus event of the field as follows:

Private Sub Accounting_Reference_No_LostFocus()

If Forms!frm_Request.Accounting_Reference_No = "" Then
MsgBox ("You must enter a reference number")
End If

End Sub

However, when I tab out of the field my msgbox does not
appear. What am I doing wrong?
 
Emma,

Your code is testing for the existence of "" whereas I assume you need
to see if there is nothing entered, i.e. Null.

You can use the Me keyword to refer to the active form, and I would
suggest the Exit event would be more suitable than the Lost Focus event.
So....
Private Sub Accounting_Reference_No_Exit(Cancel As Integer)
If IsNull(Me.Accounting_Reference_No) Then
MsgBox "You must enter a reference number", "Entry required"
Cancel = True
End If
End Sub
 
Hi Steve,

I copied and pasted your code but had to put another comma
between "You must enter a reference number." and "Entry
Required"

As follows:

Private Sub Accounting_Reference_No_Exit(Cancel As
Integer)
If IsNull(Me.Accounting_Reference_No) Then
MsgBox "You must enter a reference
number.", , "Entry Required"
Cancel = True
End If
End Sub

Thanks for your help!
 
Back
Top