How to prevent a blank or null value in a combo box on a form?
I have an unbound form that has a combo box (cboLotNum)with a OnNotInList event. The box gets populated from a lookup query if a record exists. Edits are allowed so the user can type a new lot number (if one doesn’t exist) or change an existing one. The OnNotInList will fire and handle add new or updating existing. This part works fine.
The issue is the OnNotInList only works if the user types something in the field. If the field is blank, the user can tab right through it.
I want the program to force the user to enter a value in that field.
The ultimate goal of the user is to print a report based on the values on the form so I added the following to the Print command button (btnPrint) and this works fine:
Private Sub btnPrintCert_Click()
Dim lotNum As String
lotNum = Me.cboLotNum.Value
If lotNum = Null Or lotNum = "" Then
MsgBox "Lot number cannot be blank. Please enter a valid lot number."
Me.cboLotNum.SetFocus
Else
DoCmd.OpenReport "rptCertCompView", acViewPreview, , , acWindowNormal
End If
End Sub
What I want to know is it possible to catch this before the user clicks the print button?
I tried the following on the combo box’s Before Update and On Lost Focus events.
If Me.cboLotNum.Value = Null Or Me.cboLotNum.Text = "" Then
MsgBox "Lot number cannot be blank. Please enter a valid lot number."
Me.cboLotNum.SetFocus
End If
The Before Update does nothing and the On Lost Focus works as far as giving the message but then goes to the next field. I can’t get it to focus back on itself.
I also tried setting the box’s Validation Rule to Is not Null and <>”” in which neither worked.
No error, just let me tab right through.
Do I need to just keep this in the Click event of the print button or can I force the issue during data entry?
I have an unbound form that has a combo box (cboLotNum)with a OnNotInList event. The box gets populated from a lookup query if a record exists. Edits are allowed so the user can type a new lot number (if one doesn’t exist) or change an existing one. The OnNotInList will fire and handle add new or updating existing. This part works fine.
The issue is the OnNotInList only works if the user types something in the field. If the field is blank, the user can tab right through it.
I want the program to force the user to enter a value in that field.
The ultimate goal of the user is to print a report based on the values on the form so I added the following to the Print command button (btnPrint) and this works fine:
Private Sub btnPrintCert_Click()
Dim lotNum As String
lotNum = Me.cboLotNum.Value
If lotNum = Null Or lotNum = "" Then
MsgBox "Lot number cannot be blank. Please enter a valid lot number."
Me.cboLotNum.SetFocus
Else
DoCmd.OpenReport "rptCertCompView", acViewPreview, , , acWindowNormal
End If
End Sub
What I want to know is it possible to catch this before the user clicks the print button?
I tried the following on the combo box’s Before Update and On Lost Focus events.
If Me.cboLotNum.Value = Null Or Me.cboLotNum.Text = "" Then
MsgBox "Lot number cannot be blank. Please enter a valid lot number."
Me.cboLotNum.SetFocus
End If
The Before Update does nothing and the On Lost Focus works as far as giving the message but then goes to the next field. I can’t get it to focus back on itself.
I also tried setting the box’s Validation Rule to Is not Null and <>”” in which neither worked.
No error, just let me tab right through.
Do I need to just keep this in the Click event of the print button or can I force the issue during data entry?