3

Is there any option to close the currently opened MsgBox using any code in VBA access form application?

TylerH
  • 20,799
  • 66
  • 75
  • 101

10 Answers10

6

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.

Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
  • 1
    This 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 Jul 17 '09 at 05:59
  • You 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. – Praesagus Feb 16 '10 at 21:48
3

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.

Rob Elliott
  • 1,998
  • 19
  • 25
1

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 ?

Sébastien Nussbaumer
  • 6,202
  • 5
  • 40
  • 58
  • i dont know where the message box is poping , i dont want to analyse where it comes from insted i want to KILLLLLL :-) –  Jul 16 '09 at 18:06
  • 4
    If 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 – Sébastien Nussbaumer Jul 16 '09 at 18:10
  • 1
    It 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" – MarkJ Jul 17 '09 at 06:00
  • Yes, you are correct: it's a blocking call and there's no easy way. There is, of course, a hard way. – Nigel Heffernan Feb 01 '16 at 22:21
  • There is an easy Win32 based answer. I posted it for you. – Crazyd Sep 20 '19 at 08:41
1

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...

onedaywhen
  • 55,269
  • 12
  • 100
  • 138
1

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.

Community
  • 1
  • 1
Nigel Heffernan
  • 4,636
  • 37
  • 41
0

Rather than writing an alternative to the Access MsgBox from scratch, you might consider using (or at least studying) Arvin Meyer's Custom MessageBox Creator (downloadable on this page).

David-W-Fenton
  • 22,871
  • 4
  • 45
  • 58
  • Win32 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 Sep 20 '19 at 08:40
0

I have had a similar problem; theoretically you can use the "SendKeys" function (see http://msdn.microsoft.com/en-us/library/8c6yea83%28VS.85%29.aspx). However, the MsgBox blocks the running so you cannot use the command. If you know when it going to pop up, you may run (from the script) external whshell that wait some time and then use the SendKeys. But if you know how to do that, tell me (here). Other similar possibility is to open different thread/process for it that will not be block, but I don't know if it is possible in VBA.

TylerH
  • 20,799
  • 66
  • 75
  • 101
0

Message Boxes are meant to depict some information to the user. Hence programmatically closing is not a good design, unless you are not automating some process.

You can use sendkeys or win APIs.

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • I used below for a quick pop up message which lets me know the database is going to be doing things in the background and might be unresponsive for a bit. – Crazyd Sep 20 '19 at 08:38
0

An easy to use Win32 based answer to actually be useful calling it. Using the below code you can easily do a timer-based PopUpBox. I like 1/2 second timer control so 2 = 1 second. Found this answer looking for the answer to this thread. Above answers don't seem to work. I want to use this when I want to show a quick message or quick answer default is normally right can be used for more.

Function Code usually in a Module:

Declare Function MessageBoxTimeout Lib "user32.dll" Alias "MessageBoxTimeoutA" ( _
    ByVal hwnd As Long, _
    ByVal lpText As String, _
    ByVal lpCaption As String, _
    ByVal uType As Long, _
    ByVal wLanguageID As Long, _
    ByVal lngMilliseconds As Long) As Long

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long

Public Function PopUpBox(Optional stMessage As String _
        = "Yes or No? leaving this window for 1 min is the same as clicking Yes.", _
        Optional stTitle As String = "PopUp Window", _
        Optional HalfSecTimer As Long = 120, Optional lgVBmsgType As Long = vbYesNo) As Long

    Dim RetVal As Long

    HalfSecTimer = HalfSecTimer * 500
    RetVal = MessageBoxTimeout(FindWindow(vbNullString, Title), stMessage, stTitle, lgVBmsgType, _ 
        0, HalfSecTimer)

    PopUpBox = RetVal
End Function

Call Function Code
Examples: Actual code from my database

PopUpBox "Re-Linking and Closing dB", "Closing dB", 3, vbOKOnly
intAnswer = PopUpBox("Software Lock Down Active?", "Security", 10, vbYesNo)
Crazyd
  • 406
  • 2
  • 7
  • 15
  • Does this block the calling function? What is `Title`? – Andre Sep 20 '19 at 08:46
  • Title = 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. – Crazyd Feb 28 '20 at 05:19
0

I know this is an old post, put it seems there is a new a easy way to do this:

    Sub MessageBoxTimer()
    Dim AckTime As Integer, InfoBox As Object
    Set InfoBox = CreateObject("WScript.Shell")
    'Set the message box to close after 10 seconds
    AckTime = 10
    Select Case InfoBox.Popup("Click OK (this window closes automatically after 10 seconds).", _
    AckTime, "This is your Message Box", 0)
        Case 1, -1
            Exit Sub
    End Select
End Sub

Sample code provided by: Tom Urtis

https://learn.microsoft.com/en-us/office/vba/excel/concepts/controls-dialogboxes-forms/automatically-dismiss-a-message-box