Making a field required on a form. My code doesn't work!

G

Guest

It sets the focus back on the control, but then if you hit the next record
button it moves to the next record and saves the form. (not in that order of
course.) On the dropdown menu control (CarrierID) it gives me the error
message about how the table requires this. (I have made it required in the
table) I want it to just show the msgbox only not the error msg. Am I going
about this situation in the right way, and what should I do?

-----start code-------
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_BeforeUpdate

If IsNull(Me.Service) Then
MsgBox "You must enter a service before saving."
Me.Service.SetFocus
Exit Sub
End If

If IsNull(Me.CarrierID) Then
MsgBox "You must select a carrier from the list before saving."
Me.CarrierID.SetFocus
Exit Sub
End If

If Not Me.NewRecord Then
If MsgBox("Are you sure you want to save these changes?", vbYesNo,
"Confirm Changes") = vbNo Then
Cancel = True
Me.Undo
End If
End If


Exit_BeforeUpdate:
Exit Sub

Err_BeforeUpdate:
MsgBox Err.Description
Resume Exit_BeforeUpdate

End Sub
Private Sub CarrierID_NotInList(NewData As String, Response As Integer)

On Error Resume Next

MsgBox "Please select from the dropdown list."
Me.CarrierID.SetFocus
Response = acDataErrContinue

End Sub
-----end code-----
 
J

John Vinson

It sets the focus back on the control, but then if you hit the next record
button it moves to the next record and saves the form. (not in that order of
course.) On the dropdown menu control (CarrierID) it gives me the error
message about how the table requires this. (I have made it required in the
table) I want it to just show the msgbox only not the error msg. Am I going
about this situation in the right way, and what should I do?

Set Cancel to True if the record is in violation. This will prevent
the record from being saved and leave it open for editing. e.g.

If IsNull(Me.Service) Then
MsgBox "You must enter a service before saving."
Me.Service.SetFocus
Cancel = True
Exit Sub
End If


John W. Vinson[MVP]
 
G

Guest

That worked for the text box, but I am still getting the error message for
the drop-down menu. (error message:"You cannot add or change a record because
a related record is required in 'ClientInfo'.") How do I get rid of this
message?
 
J

John Vinson

That worked for the text box, but I am still getting the error message for
the drop-down menu. (error message:"You cannot add or change a record because
a related record is required in 'ClientInfo'.") How do I get rid of this
message?

By only entering values which have a related record in ClientInfo, I
presume! It sounds like your combo box is attempting to enter an
invalid value.

John W. Vinson[MVP]
 

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