Must enter something in a field

  • Thread starter Thread starter LG
  • Start date Start date
L

LG

How do I go about putting something on a form's field that says a number must
be entered besides a 0?
 
One approach might be to set the Validation Rule for that control to <>0


Regards

Jeff Boyce
Microsoft Office/Access MVP
 
Another method would be to use the controls BeforeUpdate event to check to
see whether the value meets your criteria, and if not, then set the Cancel
variable to true. Something like:

Private Sub txt_SomeValue_BeforeUpdate(Cancel as integer)

if len(me.txt_SomeValue & "") = 0 then
msgbox "Enter a value in this field!"
Cancel = True
elseif me.txt_SomeValue <> 0 then
msgbox "Value cannot equal 0!"
Cancel = True
endif

End sub
 
I tried your idea and entered in my field name but it still says there is an
error?
Any ideas what I did wrong?

Private Sub REASON1_BeforeUpdate(Cancel As Integer)
If Len(Me.REASON1_SomeValue & "") = 0 Then
MsgBox "Enter a value in this field!"
Cancel = True
ElseIf Me.REASON1_SomeValue <> 0 Then
MsgBox "Value cannot equal 0!"
Cancel = True
End If

End Sub
 
I got a few errors first with the 0 it let it go through so I changed to
Private Sub REASON1_BeforeUpdate(Cancel As Integer)
If Len(Me.REASON1_SomeValue & "") > 0 Then
MsgBox "Enter a value in this field!"
Cancel = True
ElseIf Me.REASON1_SomeValue <> 0 Then
MsgBox "Value cannot equal 0!"
Cancel = True
End If
Then the error I got Compile error:
Method or data member not found
 
Lets try this again. I was not paying close enough attention to the name of
your fields (or controls). I use a naming convention for all of my controls
so that I know whether I'm referring to a control on the form, or directly
refering to a field value.

Private Sub REASON1_BeforeUpdate(Cancel As Integer)

If Len(Me.REASON1 & "") = 0 Then
'This first line checks to make sure there is something
'in the field, so if the length of the value in the field plus
'an empty string is zero, then there is obviously a problem
msgBox "Enter a value in this field!"
Cancel = True
ElseIf Me.REASON1 = 0 Then
'If there is a value in the field, then check to see if it is 0
'If so, display an error message
MsgBox "Value cannot equal 0!"
Cancel = True
End If

End Sub
 
Thank you so much that worked like a charm.

Dale Fye said:
Lets try this again. I was not paying close enough attention to the name of
your fields (or controls). I use a naming convention for all of my controls
so that I know whether I'm referring to a control on the form, or directly
refering to a field value.

Private Sub REASON1_BeforeUpdate(Cancel As Integer)

If Len(Me.REASON1 & "") = 0 Then
'This first line checks to make sure there is something
'in the field, so if the length of the value in the field plus
'an empty string is zero, then there is obviously a problem
msgBox "Enter a value in this field!"
Cancel = True
ElseIf Me.REASON1 = 0 Then
'If there is a value in the field, then check to see if it is 0
'If so, display an error message
MsgBox "Value cannot equal 0!"
Cancel = True
End If

End Sub
 
Your control is named Reason1. It is not named Reason1_SomeValue.

Try the following.

Private Sub REASON1_BeforeUpdate(Cancel As Integer)
If Len(Me.REASON1 & "") = 0 Then
MsgBox "Enter a value in this field!"
Cancel = True
ElseIf Me.REASON1 <> 0 Then
MsgBox "Value cannot equal 0!"
Cancel = True
End If

End Sub


John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 
Back
Top