PDA

View Full Version : MsgBox not showing



chrismid259
09-16-2011, 03:39 AM
Hi,
I have a form with a combo box that holds 7 options. What I want to be able to do is when nothing is entered and the save button on the form is clicked, a message box displays asking the user to select a value from the combo box.

So far, I've got this but it doesn't seem to be working. Anyone know what I'm doing wrong?

Private Sub cmdSave_Click()

On Error GoTo Err_cmdSave_Click

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Exit_cmdSave_Click:
Exit Sub

If Combo88.Text = "" Then
MsgBox ("Please enter a value.")
End If

Err_cmdSave_Click:
MsgBox Err.Description
Resume Exit_cmdSave_Click

Any help would be appreciated.

Thanks.

hansup
09-16-2011, 09:33 AM
I think the problem is your If condition.

If Combo88.Text = "" Then
If Combo88 contains Null, the MsgBox will not be displayed because Null is not equal to "".

You can prevent the user from leaving the combo as Null with this change. That would also reject an empty string ("") for the combo's value, but I'm not sure how the user would supply an empty string.

If Len(Me.Combo88.Value & vbNullString) = 0 Then
However, I'll suggest you validate Combo88's value immediately ... before the user moves to other controls on the form. You can do that in the combo's before update event.

Private Sub Combo88_BeforeUpdate(Cancel As Integer)
If Len(Me.Combo88.Value & vbNullString) = 0 Then
MsgBox "Value required!"
Cancel = True
End If
End Sub
Cancelling the combo's update will keep the cursor positioned in the combo and not allow the user to move to another form control without supplying an acceptable value for the combo.