I have a button on my form that pops up a yesnocancel message box.
Here is the code on the form:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim iAns As Integer
If Me.Dirty Then
strMsg = "Are you sure you want to save this record?" & vbCrLf
strMsg = strMsg & "Click Yes to Save the Record, No to Delete the Record, or Cancel to Return to the Record."
iAns = MsgBox(strMsg, vbQuestion + vbYesNoCancel)
If iAns = vbNo Then
ElseIf iAns = vbCancel Then
Cancel = True
(Don't hurt me, I copied that code from somewhere else and then just modified it for my uses.
Yes and No work fine, but of course, when I hit Cancel, I get "The DoMenuItem action was canceled."
I say of course because I gather that other people have also had this problem. After some Googling, I tried the setwarnings command (although the Access helpfile says that command doesn't prevent messages from popping up, only from interrupting macros - ?). I'm also not sure exactly where to place that command line, anyway.
I only get this error when I try to return to a record without saving or deleting it. I'm reiterating that, just because I saw a solution for this sort of problem that involved a BeforeDelConfirm event on the main form, and that did not work either... possibly because I'm not actually deleting anything?
You could try docmd.cancelevent rather than cancel = -1, but if Form_BeforeUpdate is running, then clicking that button is either running it explicitly or you have left the form or subform which is triggering Access to save the record. Is there any going back at this point? Is cancel event a possibility at this point?
Yes, I used the full syntax for setwarnings, and that gave me the compile error. I tried it both inside and outside of the If.
I'm sorry, I didn't understand at first about the cancel question... I may be doing something very strange, then. I was simply trying to give a labelled escape to the user; if they hit Yes, their data is saved and disappears. If they hit No, their data is not saved... and still disappears. I wanted them to have an option just in case they hit the button accidentally and didn't want to have to retype in a bunch of entries... the form is rather large. The users are most likely at a low level of computer experience (even lower than me! ) so I'm not expecting them to know for sure that the close button will cancel just as effectively.
Under the circumstances, if it's not possible to do anything else, I'm satisfied with the "You canceled the previous operation" msg. It's fairly simple and friendly, and most likely the users will not need the Cancel button anyway.
(In which case, why am I so worried about all of this? *sigh* Perfectionism...)
When I want more control, I will create the form based on temporary tables then insert into the main database only when the user hits save or accept. This works well in a multi user environment when issuing a key is only done when the form is saved, like an order form, journal entry batch, or invoice. In this manner, cancel does have significance.
I rarely use wizard created buttons as they too often produce effects that I can program around. Your message is one good example. Can you please post the vb code that gets run by the button the wizard created? -- John M Reynolds