If data is not selected from list, then give an error message

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need the code to state that if the user does not select an item from the
combo box(which is on the form), then create an error message. For example,
the field name is State(which list all the states in the US), if the user
does not select or type one of the states, then it will create an error
stating, "You must select or type a state from the list" . How can I write
VBA code for this?
 
One way is to open the table design and set the Validation Rule for the
field to
Is Not Null
and then you can add to the Validation Text
"You must enter a state"

Or Make the field Required.

If you want to ensure it just through the form (don't make changes to the
table), then you will need to check the combobox value in the before update
event of the form. And cancel the update if the combobox has no value.
UNTESTED AIR CODE follows

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.Cbox) = True Then
Cancel = True
MsgBox "You must enter a state."
End If
End Sub
 
I sometimes don't explain myself clearly, but this is what I want: Say for
instances the user types, "UT", which stands for UTAH which is located in the
state drop down box, but the person mistankely types, "UP", which is not a
value in the combo box, I want to alert them to add a state from the combo
box. Is there a easier way to make this happen, w/o listing all the
states' abbreviations(see code below)?

If Not(Me.State = "LA" or Me.State="UT"......) Then
value=Msgbox("Add a state from the list")
End If
 
If you want to ensure that the person only selects values that are in the
list, then set the combobox property LimitToList to True. It is on the data
tab of the property sheet.

This will automatically limit the input and will present an error message if
someone tries to enter a non-existent value. No code needed. It will allow
them to leave the field blank.
 
Thank you very much!

John Spencer said:
If you want to ensure that the person only selects values that are in the
list, then set the combobox property LimitToList to True. It is on the data
tab of the property sheet.

This will automatically limit the input and will present an error message if
someone tries to enter a non-existent value. No code needed. It will allow
them to leave the field blank.
 
Back
Top