How do I close a currently opened MsgBox using VBA?
Solution 1
Check out Randy Birch's response on this thread in microsoft.public.vb.general.discussion
He recommends creating a function in a .bas file called MsgBox. Doing so will cause VB to call your function rather than the built in one.
You'd then create your own MsgBox form and build in a timer to close your form after a set period of time. He provides links showing how to do this.
He also discusses a way to explicitly call the built in MsgBox function in case you need to do this.
Note: I've never done this but I've found that Randy Birch is a knowledgeable resource.
Solution 2
MsgBoxes are not intended to be programmatically closed, that's why it's difficult to do so. If you find yourself in a design where you must force close a MsgBox, you should probably re-evaluate your design.
Solution 3
I used to have an easy answer to this: use the Windows Scripting Shell object, which has a 'Popup' function - a Message Box, just like the VBA MsgBox() function, with a 'SecondsToWait' parameter that provides exactly the timeout you wanted.
With CreateObject("Scripting.WsShell")
.Popup "Watch me disappear in 5 seconds", 5, Application.Name & ": test", vbInformation + vbOkCancel
End With
If you include a 'Cancel' button, it might still work: the available parameters are vbOkCancel, vbYesNoCancel, and vbRetryCancel.
If you're trying to close a dialog box you didn't initiate with your own msgBox() function call, that's unhelpful: and, as I've hinted above, the 'SecondsToWait' parameter doesn't really work these days - someone in Redmond really does't like the idea of one thread closing another thread's helpful warnings and important interruptions to the user's workflow.
However, you can launch a delayed Message Box 'Close' command using the API Timer() function - not quite 'close the currently opened MsgBox', as this requires advance warning that you intended to open it - but it's the closest thing I have, it fits into a self-contained VBA module, and I posted the code in an answer to a very similar StackOverflow question to yours.
I should warn you that the answer in question is using a sledgehammer to crack a nut, with a side order of lengthy explanation.
Solution 4
I may be wrong but MsgBox is a blocking call creating a modal form so I don't think there is an easy way such as an option to do that. Do you have a specific use case for this ?
Solution 5
As MarkJ points out, could this could be a dialog generated by Access (rather than a VBA.MsgBox called in your own code)?
For example, when using table's 'dataview' in the Access UI to add a row you get a message, "You are about to append 1 record..." (or similar). Is this the kind of message you mean? If so, there are indeed ways to suppress them...
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on July 05, 2022Comments
-
Admin almost 2 years
Is there any option to close the currently opened MsgBox using any code in VBA access form application?
-
Admin almost 15 yearsi dont know where the message box is poping , i dont want to analyse where it comes from insted i want to KILLLLLL :-)
-
Sébastien Nussbaumer almost 15 yearsIf you want to know where the message box is poping, just hit "Ctrl + Break" to see what's causing that and comment the call. If it's a third party dll poping the box, then unless there is some property to make it "silent" you're probably not going to be able to do anything
-
MarkJ almost 15 yearsThis will only work if the MsgBox is being displayed from your own code. If it is an Access native MsgBox, you will need another approach.
-
MarkJ almost 15 yearsIt could also be an Access native MsgBox triggered by something you've done. If so, there might be an alternative way to make Access "silent"
-
Praesagus over 14 yearsYou can hook another app's msgbox from your app and close it. You will find out about hooks and msgbox's from the resource above. I have had to do it 1000 times and spent a few weeks living on his site. :) Avoid sendkeys like the plague. With a user on the system it is undepenable at the very least as to which app will end up getting the keystroke.
-
Nigel Heffernan over 8 yearsYes, you are correct: it's a blocking call and there's no easy way. There is, of course, a hard way.
-
Crazyd almost 5 yearsWin32 based answer is fairly easy to implement. I use dll for a few things in my dB such as locating a file for imports.
-
Crazyd almost 5 yearsThere is an easy Win32 based answer. I posted it for you.
-
Crazyd almost 5 yearsWin32.dll based answer which actually does include a timer function below.
-
Andre almost 5 yearsDoes this block the calling function? What is
Title
? -
Crazyd over 4 yearsTitle = Top of Message Box or Title bar the same as msgbox command in VBA. My code uses a Windows code library (Win32) to create the Message box and it does have a timer option. Using the Win32 you can accomplish exactly what was asked.