Validate Data on a Combo Box

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

Guest

I am trying to insure that the user does not leave this box blank. I am
trying the code listed below. But it is not validating the blank combo box.

Private Sub Operator_BeforeUpdate(Cancel As Integer)
If IsNull([Operator]) Then
MsgBox "Must Enter Operator Name", vbOKOnly, "Name Validation"

Me.Undo ' Clear the textbox
Cancel = True ' Hold the cursor in this textbox
End If
End Sub
 
You can not use the BeforeUpdate as the user has not entered a value, if you
use AfterUpdate you can not check for a value as no data was entered.

I use the LostFocus event as follows

Private Sub date_requested_LostFocus()
On Error GoTo Err_date_requested_LostFocus

If IsNull(Me!date_requested) Then
DoCmd.CancelEvent
MsgBox ("A valid date MUST be entered")
DoCmd.GoToControl "request_period"
DoCmd.GoToControl "date_requested"
End If

Exit_date_requested_LostFocus:
Exit Sub

Err_date_requested_LostFocus:
MsgBox (" request Error # " & Str(Err.Number) & " was generated by" _
& Err.Source & Chr(13) & Err.Description)
Resume Exit_date_requested_LostFocus
End Sub
 
Thank you very much. The code works well. But I am not able to exit form
if the field is blank. If the user decides to abort input.


Allan Murphy said:
You can not use the BeforeUpdate as the user has not entered a value, if you
use AfterUpdate you can not check for a value as no data was entered.

I use the LostFocus event as follows

Private Sub date_requested_LostFocus()
On Error GoTo Err_date_requested_LostFocus

If IsNull(Me!date_requested) Then
DoCmd.CancelEvent
MsgBox ("A valid date MUST be entered")
DoCmd.GoToControl "request_period"
DoCmd.GoToControl "date_requested"
End If

Exit_date_requested_LostFocus:
Exit Sub

Err_date_requested_LostFocus:
MsgBox (" request Error # " & Str(Err.Number) & " was generated by" _
& Err.Source & Chr(13) & Err.Description)
Resume Exit_date_requested_LostFocus
End Sub
--
Allan Murphy
Email: (e-mail address removed)


iholder said:
I am trying to insure that the user does not leave this box blank. I am
trying the code listed below. But it is not validating the blank combo box.

Private Sub Operator_BeforeUpdate(Cancel As Integer)
If IsNull([Operator]) Then
MsgBox "Must Enter Operator Name", vbOKOnly, "Name Validation"

Me.Undo ' Clear the textbox
Cancel = True ' Hold the cursor in this textbox
End If
End Sub
 
If Operator is part of your primary key then you will have the same problem.

Under what conditions would the user abort the data entry?

If the user clicks on the close symbol X on the top right hand side of the
form a series of messages are displayed and clicking OK will override
the checking for null data in operator and will not save the current data
entry.

--
Allan Murphy
Email: (e-mail address removed)

iholder said:
Thank you very much. The code works well. But I am not able to exit form
if the field is blank. If the user decides to abort input.


Allan Murphy said:
You can not use the BeforeUpdate as the user has not entered a value, if you
use AfterUpdate you can not check for a value as no data was entered.

I use the LostFocus event as follows

Private Sub date_requested_LostFocus()
On Error GoTo Err_date_requested_LostFocus

If IsNull(Me!date_requested) Then
DoCmd.CancelEvent
MsgBox ("A valid date MUST be entered")
DoCmd.GoToControl "request_period"
DoCmd.GoToControl "date_requested"
End If

Exit_date_requested_LostFocus:
Exit Sub

Err_date_requested_LostFocus:
MsgBox (" request Error # " & Str(Err.Number) & " was generated by" _
& Err.Source & Chr(13) & Err.Description)
Resume Exit_date_requested_LostFocus
End Sub
--
Allan Murphy
Email: (e-mail address removed)


iholder said:
I am trying to insure that the user does not leave this box blank. I am
trying the code listed below. But it is not validating the blank combo box.

Private Sub Operator_BeforeUpdate(Cancel As Integer)
If IsNull([Operator]) Then
MsgBox "Must Enter Operator Name", vbOKOnly, "Name Validation"

Me.Undo ' Clear the textbox
Cancel = True ' Hold the cursor in this textbox
End If
End Sub
 
I prefer to use the Form_BeforeUpdate to perform data validation (Entity
Integrity) check rather than the Events of the Control to avoid this sort of
problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top