Trouble with logic

D

DebbieG

I'm trying to disable the Access "X" button in my application. It's working
but I've got a logic problem. Maybe another set of eyes will see it. This
code is behind my frmMain which I use as a switchboard. I'm only showing
the code where I'm having the problem.

Private AllowExit As Boolean 'can only exit on frmMain

Private Sub Form_Open(Cancel As Integer)
AllowExit = False
End Sub

Private Sub cmdExit_Click()
AllowExit = True
Application.Quit
End Sub

Private Sub Form_Unload(Cancel As Integer)
If AllowExit = False Then
msg1 = "To close this application, first close all forms and"
msg2 = "then click the 'Exit Program' button on the Main form."
style = vbExclamation
Response = MsgBox(msg1 & vbCrLf & msg2, style, conTitle)
Cancel = True
End If
End Sub

If I click the Access "X" button, I get the message from Form_Unload.
Great! If I click the cmdExit button, the application closes but I still
get the message from Form_Unload. What am I missing?

Thanks,
Debbie
 
D

Dan Artuso

Hi,
If I were you I'd put a break point on:
AllowExit = True
in your Private Sub cmdExit_Click() routine
and then step through the code.
 
J

John Smith

It's really a strange issue. I cannot explain why, but the following code
worked find on my end:

Dim DisAllowExit As Boolean

Private Sub Form_Open(Cancel As Integer)
DisAllowExit = True
End Sub

Private Sub cmdExit_Click()
DisAllowExit = False
Application.Quit
End Sub

Private Sub Form_Unload(Cancel As Integer)
If DisAllowExit = True Then
msg1 = "To close this application, first close all forms and"
msg2 = "then click the 'Exit Program' button on the Main form."
Style = vbExclamation
Response = MsgBox(msg1 & vbCrLf & msg2, Style, conTitle)
Cancel = True
End If
End Sub

Hope it helps!

-John Smith
-------------
 
D

Dan Artuso

Yes, the code *should* work. That's why I wanted her to step through hers to
see where the MsgBox was coming from.
 
J

John Smith

Dan,

Debugging the original code reveals nothing. We can reproduce the same
result as Debbie stated.

However, I've made a small change and now the code works fine. You can see
that I did not alter the programming logic. This is very strange to me.

-John Smith
 
D

DebbieG

It's reassuring to know I wasn't going crazy ... this time!

Debbie

| Dan,
|
| Debugging the original code reveals nothing. We can reproduce the same
| result as Debbie stated.
|
| However, I've made a small change and now the code works fine. You can see
| that I did not alter the programming logic. This is very strange to me.
|
| -John Smith
|
| | > Yes, the code *should* work. That's why I wanted her to step through
hers
| to
| > see where the MsgBox was coming from.
| >
| > --
| > HTH
| > Dan Artuso, Access MVP
| >
| >
| | > > It's really a strange issue. I cannot explain why, but the following
| code
| > > worked find on my end:
| > >
| > > Dim DisAllowExit As Boolean
| > >
| > > Private Sub Form_Open(Cancel As Integer)
| > > DisAllowExit = True
| > > End Sub
| > >
| > > Private Sub cmdExit_Click()
| > > DisAllowExit = False
| > > Application.Quit
| > > End Sub
| > >
| > > Private Sub Form_Unload(Cancel As Integer)
| > > If DisAllowExit = True Then
| > > msg1 = "To close this application, first close all forms and"
| > > msg2 = "then click the 'Exit Program' button on the Main
form."
| > > Style = vbExclamation
| > > Response = MsgBox(msg1 & vbCrLf & msg2, Style, conTitle)
| > > Cancel = True
| > > End If
| > > End Sub
| > >
| > > Hope it helps!
| > >
| > > -John Smith
| > > -------------
| > > | > > > Hi,
| > > > If I were you I'd put a break point on:
| > > > AllowExit = True
| > > > in your Private Sub cmdExit_Click() routine
| > > > and then step through the code.
| > > >
| > > > --
| > > > HTH
| > > > Dan Artuso, Access MVP
| > > >
| > > >
| > > | > > > > I'm trying to disable the Access "X" button in my application.
It's
| > > working
| > > > > but I've got a logic problem. Maybe another set of eyes will see
| it.
| > > This
| > > > > code is behind my frmMain which I use as a switchboard. I'm only
| > > showing
| > > > > the code where I'm having the problem.
| > > > >
| > > > > Private AllowExit As Boolean 'can only exit on frmMain
| > > > >
| > > > > Private Sub Form_Open(Cancel As Integer)
| > > > > AllowExit = False
| > > > > End Sub
| > > > >
| > > > > Private Sub cmdExit_Click()
| > > > > AllowExit = True
| > > > > Application.Quit
| > > > > End Sub
| > > > >
| > > > > Private Sub Form_Unload(Cancel As Integer)
| > > > > If AllowExit = False Then
| > > > > msg1 = "To close this application, first close all forms
| and"
| > > > > msg2 = "then click the 'Exit Program' button on the Main
| form."
| > > > > style = vbExclamation
| > > > > Response = MsgBox(msg1 & vbCrLf & msg2, style, conTitle)
| > > > > Cancel = True
| > > > > End If
| > > > > End Sub
| > > > >
| > > > > If I click the Access "X" button, I get the message from
| Form_Unload.
| > > > > Great! If I click the cmdExit button, the application closes but
I
| > > still
| > > > > get the message from Form_Unload. What am I missing?
| > > > >
| > > > > Thanks,
| > > > > Debbie
| > > > >
| > > > >
| > > >
| > > >
| > > >
| > >
| > >
| >
| >
|
|
 

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