Solved Disable Red Cross Puzzle (1 Viewer)

bob fitz

AWF VIP
Local time
Today, 10:08
Joined
May 23, 2011
Messages
3,972
Please forgive me if I'm just having a "Senior Moment". They seem to be getting more frequent with every passing day :rolleyes:
Some years ago I found/copied/wrote some code to disable the use of the Red Cross in an app, forcing users to use a Quit button on a form. Naturally, I can't find it now, so I thought I'd rewrite it. It's only a few lines of code. Quite a few hours later and I'm going nuts. I just can't see why it doesn't work as I expect it to.
The process i had in mind was:

In a standard module declare a Public boolean variable:
Public boOKToClose as boolean 'the default value is False
In the click event of the Quit button set boOKToClose to True before attempting to call the Quit method:
boOKToClose =True
Application.Quit

Finally in the Unload event of a hidden form, check the value before displaying the promt:
If boOKToClose = 0 Then
MsgBox "Use the QUIT button to exit app."
Cancel = True
End If


And it WORKS but the message shows, even if the Quit button has been used.
I found that I can stop the message from showing if I use a Public Integer but not with a Public Boolean :unsure:
The attached db should illustrate the problem.

Thank you for your interest if you've made it to here.

If you have any explanation for me that would be fantastic, even if it's that I'm missing something really obvious. Like my mind :)
 

Attachments

moke123

AWF VIP
Local time
Today, 05:08
Joined
Jan 11, 2013
Messages
1,593
Hi Bob,
Closing the hidden form before quitting seems to work.

Code:
Private Sub cmdQuit_Click()
    i = 1
    boOKToClose = -1
    
    DoCmd.Close acForm, "frmHidden"
    
    Application.Quit

End Sub
 

cheekybuddha

AWF VIP
Local time
Today, 10:08
Joined
Jul 21, 2014
Messages
556
Hi,

It seems as if your module level variables are reset once you call Application.Quit.

However, you're logic in Form_Unload() of frmHidden is muddled with respect to i.

To match the test you perform on boOKToClose you would need to test:
If i = 0 Then

and then the message would also appear.

Doesn't help your situation though. 😖
 

bob fitz

AWF VIP
Local time
Today, 10:08
Joined
May 23, 2011
Messages
3,972
Hi Bob,
Closing the hidden form before quitting seems to work.

Code:
Private Sub cmdQuit_Click()
    i = 1
    boOKToClose = -1
  
    DoCmd.Close acForm, "frmHidden"
  
    Application.Quit

End Sub
Thanks Moke. That does work :)

It seems as if your module level variables are reset once you call Application.Quit.
Yes. That would seem to be the case. Do you know if that has always been the case? I don't remember that being the case many years ago.
 

Micron

AWF VIP
Local time
Today, 05:08
Joined
Oct 20, 2018
Messages
3,408
I see why the message opens regardless (as noted, boolean becomes false after button makes it true) but what's weird is that when i becomes zero, the >0 test is evaluated as if true when it is false.
 

cheekybuddha

AWF VIP
Local time
Today, 10:08
Joined
Jul 21, 2014
Messages
556
the >0 test is evaluated as if true when it is false.
As originally written, it evaluates to False, hence the MsgBox is not displayed.

The logic is backward. It should be: =0 and then would behave the same as the boolean test.

The issue is why the variables are already reset before the code executes.
 

Micron

AWF VIP
Local time
Today, 05:08
Joined
Oct 20, 2018
Messages
3,408
I edited that line and would have to download again, so I'm not sure if we're talking the same code but I think so. I ensured it was 1 when the button was clicked. When it got to
If i > 0 Then
msgbox
i was 0, so it shouldn't have run because 0 is not greater than 0?
 

cheekybuddha

AWF VIP
Local time
Today, 10:08
Joined
Jul 21, 2014
Messages
556
I edited that line and would have to download again, so I'm not sure if we're talking the same code but I think so. I ensured it was 1 when the button was clicked. When it got to
If i > 0 Then
msgbox
i was 0, so it shouldn't have run because 0 is not greater than 0?
Exactly - it doesn't run, which is what Bob said in his OP.

But it's just a happy coincidence because the logic is backward!
 

bob fitz

AWF VIP
Local time
Today, 10:08
Joined
May 23, 2011
Messages
3,972
As originally written, it evaluates to False, hence the MsgBox is not displayed.

The logic is backward. It should be: =0 and then would behave the same as the boolean test.

The issue is why the variables are already reset before the code executes.
I'm not sure about the logic being backwards but in the attached db, when using a boolean the variable gets reset but not when using an integer :unsure:
 

Attachments

Micron

AWF VIP
Local time
Today, 05:08
Joined
Oct 20, 2018
Messages
3,408
Exactly - it doesn't run, which is what Bob said in his OP.
No, I'm saying it shouldn't have, but it did. Not really important.
I'm not surprised that the variable values are dropped by calling Quit. Whether it has always been that way or not I have no idea, but when you think about it, if you're going to call Quit I'd expect all values to be dumped as part of the cleanup.
 

cheekybuddha

AWF VIP
Local time
Today, 10:08
Joined
Jul 21, 2014
Messages
556
when using a boolean the variable gets reset but not when using an integer :unsure:
The integer was reset too for me.

How can I explain the reversed logic?
(Ignoring for now that the variables are reset)

If boOKToClose = False Then <<Show message and Cancel>>
Closing via Quit button sets boOKToClose = True, so you expect NOT to see message

If i > 0 Then <<Show message and Cancel>>
Closing via Quit button sets i = 1, so you expect NOT to see message
BUT logic is checking i > 0 so logic says you SHOULD see message.

To make the logic equivalent, you want:
If i = 0 Then <<Show message and Cancel>>

Of course, we have discovered that the variables are reset on calling Application.Quit, and so the opposite of what you expect happens.

But in the case of i it's like a double negative!
 

moke123

AWF VIP
Local time
Today, 05:08
Joined
Jan 11, 2013
Messages
1,593
Odd. try putting msgboxes in the form events. the forms re-load
 

MickJav

It wasn't me
Local time
Today, 10:08
Joined
Nov 28, 2005
Messages
2,096
I've just added a feature like this to the current Template I am working on after seeing another topic like this.

Mine makes user's to use the Ribbon Exit, but as it's a template I hide a overide on the form.

I also have a hidden form but mine seems to close the way I want and I use a boolean as a public

I'm not quite ready to upload the project but will if it will help as only working on the linked help system on my site
 

bob fitz

AWF VIP
Local time
Today, 10:08
Joined
May 23, 2011
Messages
3,972
OK. My sincere thanks to everybody.

I hadn't been expecting the resetting of variable values but Microns comments on this make sense.
but when you think about it, if you're going to call Quit I'd expect all values to be dumped as part of the cleanup
Moke123 "came good" with little gem that made it work as required
Closing the hidden form before quitting seems to work.
cheekybuddha's explanation made me realize that I had an errant line of code from earlier testing that made the situation even more perplexing for a mere mortal like myself.
How can I explain the reversed logic?
Thanks once again for helping me to retain some sanity ;)
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top Bottom