Disabling the "Close" button on a form?

I

Ian

Using Word 97.

I want to trap a form close event, using QueryClose. However, the
following code (based on an example in Hart-Davis' "Mastering VBA6")
does not recognize the QueryClose event:

~~~~~~~~~~~~~~
Private Sub MyForm_QueryClose _
(Cancel As Integer, CloseMode As Integer)
Select Case CloseMode
Case 0
MsgBox "gotcha"
Case 1
Case 2
Case 3
End Select
End Sub

~~~~~~~~~~~~~~

When I click on the Close button, the form just closes, without
displaying the message box in Case 0. Putting a breakpoint on the
"Select Case" line shows that this point is never reached.

What is wrong with this code?

Also, where is the value of "Cancel" defined? (I believe it should be
zero for the code to work).
 
J

Jonathan West

Hi Ian,

This line is the problem

Private Sub MyForm_QueryClose _
(Cancel As Integer, CloseMode As Integer)


The events for a UserForm should always carry the UserForm prefix, no matter
what the UserForm's actual name is. So the line should be lke this

Private Sub UserForm_QueryClose _
(Cancel As Integer, CloseMode As Integer)
 
I

Ian

Hi Jonathan.

OK, but how do you distinguish between different forms? The template I
am working on now has two forms, but I only want special QueryClose
actions to apply to one of them.
 
J

Jonathan West

The code is placed in the code module for that form. When you have the form
on screen in the VBA editor, right-click on an empty area of the form, and
select View Code. Paste the routine into the editing window there.

Each form has its own code module.
 
I

Ian

Follow-up: I have just realized the simple answer to my question. It's
been a long week!

Many thanks for your help Jonathan.
 

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