Preventing user from closing form without hitting command button

Voltron

Defender of the universe
Local time
Today, 14:49
Joined
Jul 9, 2009
Messages
77
I want to make it so that the user has to hit my close form button in order to close the form. This is the code that I found. This works perfectly, except I am unable to close the form because I get the following error...

Compile Error: Label Not Defined

It then highlightes the line Private Sub btn_Close_Click() in yellow and puts the focus on the next line.

What am I doing wrong?


Public AllowClose As Boolean


Private Sub Form_Load()
AllowClose = False
End Sub


Private Sub Form_Unload(Cancel As Integer)
Cancel = Not AllowClose
End Sub


Private Sub btn_Close_Click()

On Error GoTo Err_btn_Close_Click

AllowClose = True
DoCmd.Close

Exit_btn_Close_Click:
Exit Sub

Err_cmdClose_Click:
MsgBox Err.Description
Resume Exit_btn_Close_Click

End Sub
 
Because the actual name is

Err_cmdClose_Click
 
Thanks. I new I was missing something tiny.

I am now getting the error...

The Microsoft Jet database engine could not find the object ". Make sure the object exists and that you spell its name and the path correctly.

I am not even sure what this means exactly, let alone how to correct it.

Am I missing a name for something somewhere?
 
The line...

Public AllowClose as Boolean

was one line to low for some reason so it was declared in the wrong spot.

However, I am unable to view the form in design view now. Is this because you are essentially closing the form when you do so? Or is there another reason?

Any idea how to fix this aside form taking out the code?
 
Last edited:
Yes, the unload event fires when you switch to design view. You should be able to open it directly into design view from the database window/nav pane.
 
Yes, the unload event fires when you switch to design view. You should be able to open it directly into design view from the database window/nav pane.

Yeah. This is the thing that I was afraid of. I was thinking of trying a different even procedure. Are there any out there that might work if I switched it from an Unload event procedure to the other one?
 
Not offhand, but why do you care? Users shouldn't be going into design view anyway, and you can get into it to make any necessary changes. Getting around this limitation would defeat the purpose of what you're trying to do anyway (prevent the user from exiting the form via unauthorized means).
 
Not offhand, but why do you care? Users shouldn't be going into design view anyway, and you can get into it to make any necessary changes. Getting around this limitation would defeat the purpose of what you're trying to do anyway (prevent the user from exiting the form via unauthorized means).

The only reason I even thought about it is the fact that I will most likely pass the project on after an undetermined amount of time and the other person may not know what to do AND the other people that I am working with on the project have already forced me to change a number of aspects of the database of similar nature (error prevention and preventing the user from doing certain things).

I can see that there will be no easy way to deal with this so I am just going to leave the code in and they can deal with it or they can find another way to deal with preventing the user from closing the form using any of the other methods beside the button on the form.

Thank you for your help. I really appreciate it.

Have a nice weekend.
 
You could use the form Format property where you can set the Min Max Buttons to neither and the Close button to No.
 

Users who are viewing this thread

Back
Top Bottom