MsgBox Error Checking on

G

Guest

I am trying to put up a Message Box on Form_Close, and I think I've got most
of it working, but I am unsure of the syntax for the "Cancel" button. Can
one of the experts review my code below and tell me what I'm missing? (See
Below)

I appreciate your help in advance.

Cheers,
Leslie

Private Sub Form_Close()

Dim iQuit As Integer
If Not IsNull("Me![VendorID]") Then
IQuite = MsgBox("Do you want to Save before Exiting? Yes to Save, " _
& "No to Quit without Saving or Cancel to return to the form.", _
vbYesNoCancel, "Vendor Update")
Cancel = False
If iQuit = vbCancel Then
DoCmd.Restore
End If
If iQuit = vbYes Then
DoCmd.Save
End If
If iQuit = vbNo Then
Me.Undo
' DoCmd.Close
End If

Else
DoCmd.Close
End If
End Sub
 
R

Rick Brandt

Leslie said:
I am trying to put up a Message Box on Form_Close, and I think I've
got most of it working, but I am unsure of the syntax for the
"Cancel" button. Can one of the experts review my code below and
tell me what I'm missing? (See Below)

You've got several problems here. First off by the time the Close event
fires Access has already saved any changes to the record so asking that
question is pretty pointless. Perhaps you should have this in the
BeforeUpdate event.

Second, I don't understand why you set Cancel = False. Cancel is already
False unless you do something earlier in the code to make it true. Do you
understand that in that context Cancel is referring to the Close event?

DoCmd.Restore does not do what you apparently think it does. I can see no
logical reason for that to be in there. Restore is what you do to a window
that is maximized or minimized. If your intent was to NOT have the form
actually close then THAT is when you would set Cancel = True.

DoCmd.Save is for saving design changes to the form. If what you wanted was
to save the record that would be...

DoCmd.RunCommand acCmdSaveRecord

....but even that is completely unnecessary because as stated earlier the
record has already been saved by the time this event is running. For this
reason your Me.Undo is also too late to accomplish anything.
Private Sub Form_Close()

Dim iQuit As Integer
If Not IsNull("Me![VendorID]") Then
IQuite = MsgBox("Do you want to Save before Exiting? Yes to
Save, " _ & "No to Quit without Saving or Cancel to return to the
form.", _ vbYesNoCancel, "Vendor Update")
Cancel = False
If iQuit = vbCancel Then
DoCmd.Restore
End If
If iQuit = vbYes Then
DoCmd.Save
End If
If iQuit = vbNo Then
Me.Undo
' DoCmd.Close
End If

Else
DoCmd.Close
End If
End Sub
 
G

Guest

Rick,

Thank you for reviewing the code. I am brand new at using VB and Access, so
bear with me as I stumble through my code -- I am by no means an expert.
That's why I posted the question.

If you would kindly help me with what I'm trying to accomplish, I would
truly appreciate it. I've been working on this project for only a week, and
am trying to learn as I go.

What I'm basically trying to accomplish is some error control if someone
chooses the "X" to close the window. I have buttons already for saving
specific data within the form, but I want to give folks the option if they
choose "X" in the upper RH corner of the window to write or discard the data
already entered into the screen.

You suggest using the "Before Update", but that doesn't work for what I'm
trying to do, as it is specifically the "X" I am trying to put controls on.

Thanks for the pointer of "Cancel = True". That helps. Would I actually
put that logic between my 'If Then End If" statement for the "Cancel" check?

If you could give me some pointers on how to accomplish what I'm trying to
do with using the "X" to close the screen, I would truly appreciate it.

Maybe I should have used the "Getting Started" discussion group, yes?

Cheers,
Leslie

Rick Brandt said:
Leslie said:
I am trying to put up a Message Box on Form_Close, and I think I've
got most of it working, but I am unsure of the syntax for the
"Cancel" button. Can one of the experts review my code below and
tell me what I'm missing? (See Below)

You've got several problems here. First off by the time the Close event
fires Access has already saved any changes to the record so asking that
question is pretty pointless. Perhaps you should have this in the
BeforeUpdate event.

Second, I don't understand why you set Cancel = False. Cancel is already
False unless you do something earlier in the code to make it true. Do you
understand that in that context Cancel is referring to the Close event?

DoCmd.Restore does not do what you apparently think it does. I can see no
logical reason for that to be in there. Restore is what you do to a window
that is maximized or minimized. If your intent was to NOT have the form
actually close then THAT is when you would set Cancel = True.

DoCmd.Save is for saving design changes to the form. If what you wanted was
to save the record that would be...

DoCmd.RunCommand acCmdSaveRecord

....but even that is completely unnecessary because as stated earlier the
record has already been saved by the time this event is running. For this
reason your Me.Undo is also too late to accomplish anything.
Private Sub Form_Close()

Dim iQuit As Integer
If Not IsNull("Me![VendorID]") Then
IQuite = MsgBox("Do you want to Save before Exiting? Yes to
Save, " _ & "No to Quit without Saving or Cancel to return to the
form.", _ vbYesNoCancel, "Vendor Update")
Cancel = False
If iQuit = vbCancel Then
DoCmd.Restore
End If
If iQuit = vbYes Then
DoCmd.Save
End If
If iQuit = vbNo Then
Me.Undo
' DoCmd.Close
End If

Else
DoCmd.Close
End If
End Sub
 
R

Rick Brandt

Leslie said:
Rick,

Thank you for reviewing the code. I am brand new at using VB and
Access, so bear with me as I stumble through my code -- I am by no
means an expert. That's why I posted the question.

If you would kindly help me with what I'm trying to accomplish, I
would
truly appreciate it. I've been working on this project for only a
week, and am trying to learn as I go.

What I'm basically trying to accomplish is some error control if
someone chooses the "X" to close the window. I have buttons already
for saving specific data within the form, but I want to give folks
the option if they choose "X" in the upper RH corner of the window to
write or discard the data already entered into the screen.

You suggest using the "Before Update", but that doesn't work for what
I'm trying to do, as it is specifically the "X" I am trying to put
controls on.

But if they have any unsaved changes when they press the X then the
BeforeUpdate event WILL fire before the Close event. In that event you can
use...

Cancel = True
Me.Undo

....should they decide to discard the changes.
 

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

Similar Threads


Top