Detect if a msgbox is open?

BadSikander

Registered User.
Local time
Today, 11:04
Joined
Jan 19, 2007
Messages
31
Hello.

Is there any way to have a VB timer event check to see if a message box is open in the database before it runs?

I have a switchboard form that is always open in the background and it runs a regular timer event. I've noticed that on other forms, any message box appearing can cause problems.

e.g. Form2 has an event that does:

MsgBox "Hello"
DoCmd.Close

If the switchboard timer event runs whilst a user is staring at this msgbox the focus seems to have shifted to the switchboard by the time they press the OK button. DoCmd.Close is then applied to the switchboard instead of Form2.

I was hoping to avoid having to find every message box and ensuring the correct form is actually named in all subsequent commands. I thought it would be a lot simpler if I could just prevent the timer event running if a msgbox is open somewhere, but don't know how to have it check.

Thanks for any help.
 
Instead of the docmd.close statement you could use docmd.close acform, "Form2"
This closes Form2 whatever the focus is.

HTH:D
 
Instead of the docmd.close statement you could use docmd.close acform, "Form2"
This closes Form2 whatever the focus is.

HTH:D

Ah, but I would have to make that change for every single one of my forms, and for every call to msgbox. The shift of focus problem with msgbox doesn't just affect DoCmd.Close, but delete commands, requery, etc. so it's a lot of instances to find and amend.

That's why I was hoping I could avoid the problem at the "source", by amending the timer event itself to accommodate msgbox being called by any form, no matter the name.

Many thanks for suggestion though.
 
A messagebox, by nature, stops all other code from execution until it is responded to. The only thing I could suggest was to set on of the dreaded global variable anytime the messagbox is popped then resetting it after the messagebox is closed and check it before triggering the Timer event code.

Timer events frequently cause problems when multiple forms are open. Exactly what are you doing in this Timer event?
 
A messagebox, by nature, stops all other code from execution until it is responded to....

.... Timer events frequently cause problems when multiple forms are open. Exactly what are you doing in this Timer event?

Hello.

The timer event does a dlookup against a query. The query uses information from a field on the background form in its criteria.

If it finds any records in the query, a popup form appears and warns the user. If it doesn't find any records (which is 99% of the time), nothing happens.

I too assumed timer events just wouldn't happen if a message box was open somewhere.

However, events on the other forms work over and over without a problem. When they do wrong, the only common factor is that they opened a msgbox at the time when this background event was scheduled to happen and they've all got a similar generic DoCmd line following after the msgbox which suddenly gets applied to the background form instead.

But the timer event certainly doesn't shift focus to the background form normally, so I'm very confused.
 
Could you not just set the focus back onto the form once the message box has been closed?

Msgbox
Setfocus
close command, or query or whatever.
 
This doesn't help with this problem but I do want to point out that you can use this GENERIC close code and it will close the form the code is on, regardless of which has focus at the time:

DoCmd.Close acForm, Me.Name, acSaveNo

no need to change any of that at all, you can use it in any form and because of the Me.Name part it will close the form the code is on.
 
Could you not just set the focus back onto the form once the message box has been closed?

Msgbox
Setfocus
close command, or query or whatever.

Hello.

Only by changing the code on every form and every event where a msgbox might be called and cause this problem. I expect that's what I'll have to end up doing, but I was just hoping there might be a way to prevent the timer event itself running to save some time.

Thanks though
 
This doesn't help with this problem but I do want to point out that you can use this GENERIC close code and it will close the form the code is on, regardless of which has focus at the time:

DoCmd.Close acForm, Me.Name, acSaveNo

no need to change any of that at all, you can use it in any form and because of the Me.Name part it will close the form the code is on.


That certainly looks the quickest way if I have to change the individual forms themselves, thanks.

Probably a stupid question, but what does the 'acSaveNo' bit do? Tell it not to save the record? If so, will it still close the form if I just leave that bit out?

And does the same format work with Requery and delete record commands?

Many thanks
 
No the acSaveNo tells it to not save any DESIGN changes to the form (this would include any filters or anything that have been set).
 
No the acSaveNo tells it to not save any DESIGN changes to the form (this would include any filters or anything that have been set).

Great, I'll give that a try.

Thanks for your help
 
To make your life faster you could do a search and replace on your forms...

Seach for the docmd.close
and replace it with SOS's... (??) code
 

Users who are viewing this thread

Back
Top Bottom