P
Pat Hartman\(MVP\)
Notice that there is a cancel argument in the BeforeUpdate event. By
setting Cancel to true, you are telling Access that you do not want to save
the record. The cancel response to the MsgBox does not do this. It only
tells you which button the user pressed.
setting Cancel to true, you are telling Access that you do not want to save
the record. The cancel response to the MsgBox does not do this. It only
tells you which button the user pressed.
Tee See said:A further question ... If the MsgBox=vbCancel why is the code "cancel=true"
required?
Pat Hartman(MVP) said:The code you posted belongs in the form's BeforeUpdate event. Not in the
events suggested by the others. The BeforeUpdate event is the ONLY place
where you are certain to trap the record save since no matter what causes
the record save, all roads lead to the BeforeUpdate event for dirty
records. An even better solution for required fields is to set their
Required property to Yes in the table definition. That way, no matter
how the record gets added, Jet will not allow the record to be saved if
the required fields are null. In your forms, you can trap the standard
error message and replace it with your own if you prefer.
If IsNull(Me![CustomerIDCode]) Then
If MsgBox("'CustomerIDCode' must contain a value." _
& Chr(13) & Chr(10) _
& "Press 'OK' to return and enter a value." _
& Chr(13) & Chr(10) _
& "Press 'Cancel' to abort the record.", _
vbOKCancel, "A Required field is Null") = _
vbCancel Then
Cancel = True
Me.Undo
End If
End If
Tee See said:On a popup form I have a button to close the form. If the form is open
to ADD a record and the user changes his/her mind I'd like to close the
form without adding a blank record. The following bit of code (I
thought) would handle this but I still get a blank rcord added. The code
by the way comes right from an example page within access help.
If IsNull(Me![CustomerIDCode]) Then
If MsgBox("'CustomerIDCode' must contain a value." _
& Chr(13) & Chr(10) _
& "Press 'OK' to return and enter a value." _
& Chr(13) & Chr(10) _
& "Press 'Cancel' to abort the record.", _
vbOKCancel, "A Required field is Null") = _
vbCancel Then
DoCmd.Close
End If
End If
Any advice sincerely appreciated