Use of Cancel In Unload Event

T

tkosel

I have this code in a forms unload event. (V_Password is a Public Variable)
My purpose is to prevent the user from exiting the application using any
method I do not approve of. (Such as X control button, etc. My reason for
this is that the application runs continously in the background, no user
input is required. However, if it isn't running due to some user closing it,
it won't do what it is supposed to!) Of course I want to have a way out
myself, so If I have the password, I can exit.

Private Sub Form_Unload(Cancel As Integer)

DoCmd.OpenForm "PasswordForm", , , , , acDialog

If V_Password = "test" Then 'go ahead and quit, allow the event to
complete
GoTo Done
Else 'don't quit, cancel the unload event
Cancel = True
Exit Sub
End If

Done:

Exit Sub

End Sub

It works, except Access 2007 gives me the typical message about how it has
encountered a problem and needs to close. "Sorry for the inconvienience!"
Asks if I want to send or not and when I click debug, I get no dialog.

Kind of irritating, any ideas what causes this and why? How can I eliminate
it? This application will be using the Access 2007 Runtime when it is
deployed, but testing it using Access 2007 full.
 
W

Wayne-I-M

Not sure about what you doing - am ok with the code just not about the roces.
What if you are off work, depart from the company, etc

Give this a try ON A BLANK DB - just in case you forget the password.
Oh... in this case it's PASSWORD

Create 2 forms
1 with a text box called txtMyTextBox
The 2nd form called frmMt2ndForm

put this afterUpdate of txtMyTextBox or OnClick of a button

Static Counter As Integer
If txtMyTextBox = "PASSWORD" Then
DoCmd.OpenForm "frmMt2ndForm", acNormal, "", "", , acNormal
Else
If Counter < 2 Then
MsgBox "The password you entered is not correct - try again - MAX 3
ATTEMPTS?", vbOKOnly, "You can't open my form"
Counter = Counter + 1
txtMyTextBox = ""
Else
DoCmd.Quit
End If
End If


At least this will take three attemps before kicking in. In you case you
will need to swap the actions - quite if correct etc.

HTH
 

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