Prevent Combo Error

S

scott

I'm trying to prevent a user from leaving or tabbing past a combo box
control without selecting a value. If they don't select a value, they get
the referential integrity error below. My AfterUpdate code below won't catch
the error. I'd like to display a warning and then force focus back to the
control when this happens and suppress Access's error warning.

Any help?


ERROR: *****

You cannot add or change a record because a related record is required in
table 'myTable'

CODE: ******

Private Sub cboMyCombo_AfterUpdate()
If Me.Dirty Then
If Not IsNumeric(Me.cboMyCombo) Or IsNothing(Me.cboMyCombo) Then
MsgBox "You must select a record", vbCritical,
CurrentDb().Properties("AppTitle")
Me.cboMyCombo.SetFocus
Exit Sub
End If
End If

End Sub
 
K

Ken Snell \(MVP\)

Use the ComboBox's BeforeUpdate event to test that there is a value in the
combo box; also use the form's BeforeUpdate event to do the same.
 
S

scott

The Form's BeforeUpdate event catches it, but Access's error message follows
my message box.

How can i catch or suppress Access's error message?
 
S

scott

I should mention that my situation is being caused by referential integrity
violation, the BeforeUpdate event method I'm using would normally
bullet-proof error control on the control.
 
K

Ken Snell \(MVP\)

Set the Cancel variable to True in the code just before you show your
message box:
Cancel = True
MsgBox "message"
 

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

Top