# VBA MsgBox timer?



## Andrew Fergus

Hello Everyone
Is it possible to have a timer on a VBA message box such that it disappears after a set interval?  I have a standard VBA MsgBox informing the user that an action was successful - how can I make the message box disappear (as if Ok was pressed) after 3 seconds?  Is this possible?  I'm new with VB and am using it in conjuntion with Access 2000.
TIA, Andrew


----------



## Tom Urtis

You probably would have received a response sooner if this was asked on the Excel Questions board, but at any rate here's one way to do what you want:

Sub Test1()
Dim AckTime As Integer, InfoBox As Object
Set InfoBox = CreateObject("WScript.Shell")
AckTime = 3
Select Case InfoBox.Popup("Click OK or do nothing within 3 seconds.", _
AckTime, "This is your Message Box", 0)
Case 1, -1
Exit Sub
End Select
End Sub

Keep in mind that the first time code compiles, it can take a few extra moments, so on the first execution of this macro, it will take longer than 3 seconds for 3 seconds to elapse.  Run the code twice and see if the second time around is closer to 3 seconds than the first execution, which it was for me.


----------



## Andrew Fergus

Hi Tom
Thanks for your response.  I got your code working if I created a new button that didn't do anything.  If I added it to my code then I can't get it to work - the message box opens but it doesn't disappear automatically.  I only made a couple of minor modifications to the message box text.  I'm not sure what I have done wrong.  My code is below if it is any help, and your suggestion has been incorporated towards the very bottom.


		Code:
__


Private Sub DelClass_Click()
Dim Filler1, Filler2, Message, NumRecords, Response     'Temp variables
Filler1 = "are "                                        'Constant for MsgBox text
Filler2 = "s"                                           'ditto
If (IsNull([DelClassCombo])) Then                       'Nothing entered so do nothing
    DoCmd.GoToControl "DelClassCombo"
Else
    If (DCount("[Classification_Code]", "T_Clients", "[Classification_Code] = '" & Forms![frmAdministration]![DelClassCombo] & "'")) = 0 Then
        DoCmd.SetWarnings (No)                          'No matching records so safely delete the classification
        DoCmd.OpenQuery "qryDeleteClass"
        DoCmd.SetWarnings (Yes)
    Else
        NumRecords = DCount("[Classification_Code]", "T_Clients", "[Classification_Code] = '" & Forms![frmAdministration]![DelClassCombo] & "'")
        If NumRecords = 1 Then                          'Just to get a meaningful message in the message box
            Filler1 = "is "
            Filler2 = Null
        End If
        Message = "There " & Filler1 & NumRecords & " client record" & Filler2 & " with the classification value '" & Forms![frmAdministration]![DelClassCombo] & "'.  Press the OK button to change the classification value for the affected client record" & Filler2 & " to the default classification value of '" & DLookup("[Description]", "T_Classification", "[Permanent] = Yes") & "', or press the Cancel button to exit."
        Response = MsgBox(Message, vbOKCancel + vbCriticial + vbDefaultButton2, "Are you sure you wish to proceed?")
        If Response = vbOK Then
          DoCmd.SetWarnings (No)
          DoCmd.OpenQuery "qryDeleteClass2"             'Updates selected records to the default classification
          DoCmd.OpenQuery "qryDeleteClass"
          DoCmd.SetWarnings (Yes)
        Else
            Me!DelClassCombo = Null
            Exit Sub
        End If
    End If
    Message = "The classification value of '" & Forms![frmAdministration]![DelClassCombo] & "' has been deleted."   ' changed from a MsgBox to accomodate timer per post from Tom
    Me!DelClassCombo = Null
    DoCmd.Requery ("DelClassCombo")
    DoCmd.GoToControl "DelIndustryCombo"
    Dim AckTime As Integer, InfoBox As Object               'for the auto-close MsgBox per post from Tom
    Set InfoBox = CreateObject("WScript.Shell")             'ditto
    AckTime = 3                                             'ditto
    Select Case InfoBox.PopUp(Message, AckTime, "FYI", 0)   'Tom's timer MsgBox
    Case 1, -1                                              'for Tom's timer MsgBox
    Exit Sub
    End Select
End If
End Sub


Why won't this work? I initially tried setting the variables with my other variables, but that didn;t work so I then put all of your suggestion together to see if that made any difference, without success.  Any thoughts?

Thanks, Andrew

P.S. Sorry about the code formatting - it is wrapping to the next line for longer lines and my indenting has been lost.


----------



## Tom Urtis

The code I posted was meant to answer the immediate question you asked about how to compose a message box that would acknowledge itself.  Given all this code now, it looks like (and I could be wrong) you have a command button named DelClass that, when clicked, does a bunch of evaluations.  The first evaluation is this...

If (IsNull([DelClassCombo])) Then
DoCmd.GoToControl "DelClassCombo"

...which looks like maybe you are checking to see if a combo box named DelClassCombo has had any item selected.  If not, then among other things, you want the magical message box to appear and disappear 3 seconds later.

What I cannot determine is the possible interference given by this:
DoCmd.GoToControl "DelClassCombo"

Maybe the GoTo command involves something else that you either are not saying or don't know about, and maybe if a control is selected then the message box won't appear for some reason.  I just cannot tell from a distance, but if you explain in words instead of code the general logic of what you are doing, a suggestion from me or someone else might be possible.


----------



## Andrew Fergus

Hi Tom

The disappearing message box was applied to this situation as a 'proof of concept' - in other words if I can do it here, then I can use the logic in other subs, and my users can be presented with 'status updates' (i.e. informed that an action was successful) without necessarily having to click Ok all the time.  I envisage changing the timer to periods both longer and shorter than 3 seconds.  But firstly I want to see if it can be done (so far yes, but not for my situation).

What does the sub do, in plain English?

On an Access form, the user selects a 'classification' in the combo box 'DelClassCombo' and then clicks a button 'Del_Class' - to try and delete a 'classification' from a table.

If the 'DelClassCombo' combo box is null (i.e. nothing was selected) then the sub just returns the cursor to the combo box and then does nothing - the code at the bottom is still inside the first Else statement.  The message box will not appear if the first 'If' condition was met (i.e. the Null part).  There are no procedures or subs attached to 'DelClassCombo' - it is just a combo box where the user selects a 'classification' from the combo box list.  I could put an Exit Sub line after the
DoCmd.GoToControl "DelClassCombo"
line and it won't make any difference.  There are no hidden subs or procedures.

If the DelClassCombo is not null (the else condition), then it tests to see if there are any records in the many side of a one-to-many relationship with the T_Clients table (i.e. how many clients in T_Clients have the selected 'classification').  If there are no matching records then it deletes the selected 'classification' and jumps to the
Message = "The classification value of...
part towards the bottom, and the disappearing message box is invoked.

If there are matching records, then the user can either do nothing (the else part where vbOK was not pressed and the sub exits), or they can delete the classification by pressing ok (the VbOk part).  However, in this instance the code changes the T_Client records that were using the selected classification to a default classification value (i.e. the Dlookup...permanent = yes part).  Again it proceeds to the
Message = "The classification value of...
part where the disappearing message box 

Everything works perfectly and I replaced a standard MsgBox with your code - whilst the sub does everything it should, the new message box deosn't disappear after 3 (or more) seconds.

Hopefully that make sense.  Thanks for looking at this.

Andrew


----------



## Future_MVP

Andrew Fergus said:


> Hi Tom
> 
> The disappearing message box was applied to this situation as a 'proof of concept' - in other words if I can do it here, then I can use the logic in other subs, and my users can be presented with 'status updates' (i.e. informed that an action was successful) without necessarily having to click Ok all the time.  I envisage changing the timer to periods both longer and shorter than 3 seconds.  But firstly I want to see if it can be done (so far yes, but not for my situation).
> 
> What does the sub do, in plain English?
> 
> On an Access form, the user selects a 'classification' in the combo box 'DelClassCombo' and then clicks a button 'Del_Class' - to try and delete a 'classification' from a table.
> 
> If the 'DelClassCombo' combo box is null (i.e. nothing was selected) then the sub just returns the cursor to the combo box and then does nothing - the code at the bottom is still inside the first Else statement.  The message box will not appear if the first 'If' condition was met (i.e. the Null part).  There are no procedures or subs attached to 'DelClassCombo' - it is just a combo box where the user selects a 'classification' from the combo box list.  I could put an Exit Sub line after the
> DoCmd.GoToControl "DelClassCombo"
> line and it won't make any difference.  There are no hidden subs or procedures.
> 
> If the DelClassCombo is not null (the else condition), then it tests to see if there are any records in the many side of a one-to-many relationship with the T_Clients table (i.e. how many clients in T_Clients have the selected 'classification').  If there are no matching records then it deletes the selected 'classification' and jumps to the
> Message = "The classification value of...
> part towards the bottom, and the disappearing message box is invoked.
> 
> If there are matching records, then the user can either do nothing (the else part where vbOK was not pressed and the sub exits), or they can delete the classification by pressing ok (the VbOk part).  However, in this instance the code changes the T_Client records that were using the selected classification to a default classification value (i.e. the Dlookup...permanent = yes part).  Again it proceeds to the
> Message = "The classification value of...
> part where the disappearing message box
> 
> Everything works perfectly and I replaced a standard MsgBox with your code - whilst the sub does everything it should, the new message box deosn't disappear after 3 (or more) seconds.
> 
> Hopefully that make sense.  Thanks for looking at this.
> 
> Andrew


Andrew,

Would you mind sending me the finished code to this question?
I am trying to create a countdown timer in excel that is displayed in a message box and is activated by a command button.
Thanks,


----------



## TLA

I recently tried this code and the msgbox popped up fine, but never disappeared unless i clicked the button. The time part doesn't seem to work for me. I pasted teh code into a Sub in excel without adding or deleting anything.


----------



## KPMirror

Future_MVP said:


> Andrew,
> 
> I have same problem. Any resolution to this issue?


----------



## sous2817

Rather than use a message box, couldn't you create your own user form that looks just like a message box and then close the user form after X number of seconds?  I don't think you'll be able to do what you want with a standard message box.


----------



## Lazi

I tried to close a Form after time_Interval , as suggested, but it would Not close either.


----------



## Andrew Fergus

Hello Everyone
Is it possible to have a timer on a VBA message box such that it disappears after a set interval?  I have a standard VBA MsgBox informing the user that an action was successful - how can I make the message box disappear (as if Ok was pressed) after 3 seconds?  Is this possible?  I'm new with VB and am using it in conjuntion with Access 2000.
TIA, Andrew


----------



## sous2817

I can get the form to close automatically after X seconds, but I can't get it to do either a button click to close the form OR after a certain time period.  Seems it's either one or the other...


----------



## Lazi

I will settle with the automatic time out exit.
Can you send the participants how you managed to do that ?

Thanx


----------



## sous2817

This is one way to do it.  I attached the code to an button click event:



		Code:
__


Private Sub CommandButton1_Click()
UserForm1.Show
DoEvents
If Application.Wait(Now + TimeValue("0:00:10")) Then Unload UserForm1
End Sub


This will display the userform for 10 seconds.  Obviously, change the 10 above to be however many second (minutes, or hours) appropriate for your application.


----------



## Jaafar Tribak

sous2817 said:


> I can get the form to close automatically after X seconds, but I can't get it to do either a button click to close the form OR after a certain time period.  Seems it's either one or the other...



One way :



		Code:
__


Sub LoadForm()
    Application.OnTime Now + TimeSerial(0, 0, 10), "UnloadForm"
    UserForm1.Show
End Sub

Sub UnloadForm()
    Unload UserForm1
End Sub


----------



## tusharm

For several options...
Display a message for a specific duration
http://www.tushar-mehta.com/publish...display_message_for_a_specific_duration.shtml


----------



## ISY

Hallo

It's been 2 months ..
Is there a solution to the request to close after msgbox n. second



		Code:
__


'To display a timed Msgbox use the Msgbox2 routine given below. Note, a demonstration routine can be found at the bottom of this post:

'------------API calls for Msgbox------------------------
'------------MUST BE PLACED IN A STANDARD MODULE----------
Option Explicit

'API calls for Msgbox2. Must be placed in a standard module
Private Declare Function SetTimer Lib "user32" (ByVal Hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal Hwnd As Long, ByVal nIDEvent As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
Private zsMessageTitle As String, lTimerId As Long


'Purpose     :  Stops the timer routine
'Inputs      :  N/A
'Outputs     :  Returns True if the timer routine was stopped
'Author      :  Andrew Baker
'Date        :  15/10/2000 15:24
'Notes       :  Code must be placed in a module
'Revisions   :

Function EndTimer() As Boolean
    If lTimerId Then
        lTimerId = KillTimer(0&, lTimerId)
        lTimerId = 0
        EndTimer = True
    End If
End Function

'Purpose     :  Starts the continuous calling of a private routine at a specific time interval.
'Inputs      :  lInterval           The interval (in ms) at which to call the routine
'Outputs     :  N/A
'Author      :  Andrew Baker
'Date        :  15/10/2000 15:30
'Notes       :  Code must be placed in a module
'Revisions   :

Sub StartTimer(lInterval As Long)
    If lTimerId Then
        'End Current Timer
        EndTimer
    End If
    lTimerId = SetTimer(0&, 0&, ByVal lInterval, AddressOf TimerRoutine)
End Sub


'Purpose     :  Routine which is called repeatedly by the timer API.
'Inputs      :  Inputs are automatically generated.
'Outputs     :
'Author      :  Andrew Baker
'Date        :  15/10/2000 15:32
'Notes       :
'Revisions   :

Private Sub TimerRoutine(ByVal lHwnd As Long, ByVal lMsg As Long, ByVal lIDEvent As Long, ByVal lTime As Long)
    Const WM_CLOSE = &H10
    Dim lHwndMsgbox As Long

    'Find the Msgbox
    lHwndMsgbox = FindWindow(vbNullString, zsMessageTitle)
    'Close Msgbox
    Call SendMessage(lHwndMsgbox, WM_CLOSE, 0, ByVal 0&)
End Sub



'Purpose     :  Extended version of Msgbox, has extra parameter to set time msgbox is displayed for
'Inputs      :  As per Msgbox
'               [DisplayTime]               The time in MS to display the message.
'Outputs     :  As per Msgbox
'Author      :  Andrew Baker
'Date        :  03/01/2001 13:23
'Notes       :
'Revisions   :

Function Msgbox2(Prompt As String, Buttons As VbMsgBoxStyle, Title As String, Optional DisplayTime As Long) As VbMsgBoxResult
    If DisplayTime > 0 Then
        'Enable the timer
        StartTimer DisplayTime
        zsMessageTitle = Title
    End If
    Msgbox2 = MsgBox(Prompt, Buttons, Title)
    'Stop the timer
    EndTimer
End Function

''Demonstration routine
Sub TestMessage()
    Dim lRetVal As VbMsgBoxResult
    lRetVal = Msgbox2("hello .. the program is fully functional." & vbCrLf & _
    "Per la verifica delle vincite andare indietro col cursore ogni 5° del mese.", vbOKOnly + vbInformation, "AVVISO!!!", 6000)
    Debug.Print lRetVal
End Sub


Also likely to concern.


----------



## tusharm

Did you look at the link I shared?


ISY said:


> Hallo
> 
> It's been 2 months ..
> Is there a solution to the request to close after msgbox n. second
> 
> 
> 
> Code:
> __
> 
> 
> 'To display a timed Msgbox use the Msgbox2 routine given below. Note, a demonstration routine can be found at the bottom of this post:
> 
> '------------API calls for Msgbox------------------------
> '------------MUST BE PLACED IN A STANDARD MODULE----------
> Option Explicit
> 
> 'API calls for Msgbox2. Must be placed in a standard module
> Private Declare Function SetTimer Lib "user32" (ByVal Hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
> Private Declare Function KillTimer Lib "user32" (ByVal Hwnd As Long, ByVal nIDEvent As Long) As Long
> Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
> Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
> Private zsMessageTitle As String, lTimerId As Long
> 
> 
> 'Purpose     :  Stops the timer routine
> 'Inputs      :  N/A
> 'Outputs     :  Returns True if the timer routine was stopped
> 'Author      :  Andrew Baker
> 'Date        :  15/10/2000 15:24
> 'Notes       :  Code must be placed in a module
> 'Revisions   :
> 
> Function EndTimer() As Boolean
> If lTimerId Then
> lTimerId = KillTimer(0&, lTimerId)
> lTimerId = 0
> EndTimer = True
> End If
> End Function
> 
> 'Purpose     :  Starts the continuous calling of a private routine at a specific time interval.
> 'Inputs      :  lInterval           The interval (in ms) at which to call the routine
> 'Outputs     :  N/A
> 'Author      :  Andrew Baker
> 'Date        :  15/10/2000 15:30
> 'Notes       :  Code must be placed in a module
> 'Revisions   :
> 
> Sub StartTimer(lInterval As Long)
> If lTimerId Then
> 'End Current Timer
> EndTimer
> End If
> lTimerId = SetTimer(0&, 0&, ByVal lInterval, AddressOf TimerRoutine)
> End Sub
> 
> 
> 'Purpose     :  Routine which is called repeatedly by the timer API.
> 'Inputs      :  Inputs are automatically generated.
> 'Outputs     :
> 'Author      :  Andrew Baker
> 'Date        :  15/10/2000 15:32
> 'Notes       :
> 'Revisions   :
> 
> Private Sub TimerRoutine(ByVal lHwnd As Long, ByVal lMsg As Long, ByVal lIDEvent As Long, ByVal lTime As Long)
> Const WM_CLOSE = &H10
> Dim lHwndMsgbox As Long
> 
> 'Find the Msgbox
> lHwndMsgbox = FindWindow(vbNullString, zsMessageTitle)
> 'Close Msgbox
> Call SendMessage(lHwndMsgbox, WM_CLOSE, 0, ByVal 0&)
> End Sub
> 
> 
> 
> 'Purpose     :  Extended version of Msgbox, has extra parameter to set time msgbox is displayed for
> 'Inputs      :  As per Msgbox
> '               [DisplayTime]               The time in MS to display the message.
> 'Outputs     :  As per Msgbox
> 'Author      :  Andrew Baker
> 'Date        :  03/01/2001 13:23
> 'Notes       :
> 'Revisions   :
> 
> Function Msgbox2(Prompt As String, Buttons As VbMsgBoxStyle, Title As String, Optional DisplayTime As Long) As VbMsgBoxResult
> If DisplayTime > 0 Then
> 'Enable the timer
> StartTimer DisplayTime
> zsMessageTitle = Title
> End If
> Msgbox2 = MsgBox(Prompt, Buttons, Title)
> 'Stop the timer
> EndTimer
> End Function
> 
> ''Demonstration routine
> Sub TestMessage()
> Dim lRetVal As VbMsgBoxResult
> lRetVal = Msgbox2("hello .. the program is fully functional." & vbCrLf & _
> "Per la verifica delle vincite andare indietro col cursore ogni 5° del mese.", vbOKOnly + vbInformation, "AVVISO!!!", 6000)
> Debug.Print lRetVal
> End Sub
> 
> 
> Also likely to concern.


----------



## darkrid3r

is there a way to set a timer with the WScript.Shell popup to be less than 1 second??

Thanks


----------



## Neologism

getting back onto the topic of a Self Closing Message Box:
/
Const Title As String = "Self closing message box"
Const Delay As Byte = 3 'this isn't exactly seconds, any value over 5 will be TOOO long
Const wButtons As Integer = 16 ' Boutons + icon
Dim wsh As Object, msg As String
Set wsh = CreateObject("WScript.Shell")
msg = Space(0) & "Insert your message here." & vbLf & vbLf & "And here if you wish."
wsh.Popup msg, Delay, Title, wButtons
Set wsh = Nothing
/
Sticking with getting off topic of a Self Closing Message Box, if you are intersted in a potentially missed message box, have you thought of using vbspeak? Its a fun way to catch someone off guard and actually pay attention to a file that they may become bored and careless with.
/
Application.Speech.Speak "Action is complete!"
/
or my favorite:
/
Application.Speech.Speak "do not push the button so hard. I have feelings too!"
/

Regards,
-=me=-


----------



## Marten

It's been more than 3 years. 
It's a beautifull Msgbox with timer you / Andrew Baker made. But after trying for a few hours to make it work with vbYesNo, I give up. I can not find the Msg or wParam or lParam to send a vbYes or vbNo with messagesendA (or anything else) to the Msgbox.
Anybody?



ISY said:


> Hallo
> 
> It's been 2 months ..
> Is there a solution to the request to close after msgbox n. second
> 
> 
> 
> Code:
> __
> 
> 
> 'To display a timed Msgbox use the Msgbox2 routine given below. Note, a demonstration routine can be found at the bottom of this post:
> 
> '------------API calls for Msgbox------------------------
> '------------MUST BE PLACED IN A STANDARD MODULE----------
> Option Explicit
> 
> 'API calls for Msgbox2. Must be placed in a standard module
> Private Declare Function SetTimer Lib "user32" (ByVal Hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
> Private Declare Function KillTimer Lib "user32" (ByVal Hwnd As Long, ByVal nIDEvent As Long) As Long
> Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
> Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
> Private zsMessageTitle As String, lTimerId As Long
> 
> 
> 'Purpose     :  Stops the timer routine
> 'Inputs      :  N/A
> 'Outputs     :  Returns True if the timer routine was stopped
> 'Author      :  Andrew Baker
> 'Date        :  15/10/2000 15:24
> 'Notes       :  Code must be placed in a module
> 'Revisions   :
> 
> Function EndTimer() As Boolean
> If lTimerId Then
> lTimerId = KillTimer(0&, lTimerId)
> lTimerId = 0
> EndTimer = True
> End If
> End Function
> 
> 'Purpose     :  Starts the continuous calling of a private routine at a specific time interval.
> 'Inputs      :  lInterval           The interval (in ms) at which to call the routine
> 'Outputs     :  N/A
> 'Author      :  Andrew Baker
> 'Date        :  15/10/2000 15:30
> 'Notes       :  Code must be placed in a module
> 'Revisions   :
> 
> Sub StartTimer(lInterval As Long)
> If lTimerId Then
> 'End Current Timer
> EndTimer
> End If
> lTimerId = SetTimer(0&, 0&, ByVal lInterval, AddressOf TimerRoutine)
> End Sub
> 
> 
> 'Purpose     :  Routine which is called repeatedly by the timer API.
> 'Inputs      :  Inputs are automatically generated.
> 'Outputs     :
> 'Author      :  Andrew Baker
> 'Date        :  15/10/2000 15:32
> 'Notes       :
> 'Revisions   :
> 
> Private Sub TimerRoutine(ByVal lHwnd As Long, ByVal lMsg As Long, ByVal lIDEvent As Long, ByVal lTime As Long)
> Const WM_CLOSE = &H10
> Dim lHwndMsgbox As Long
> 
> 'Find the Msgbox
> lHwndMsgbox = FindWindow(vbNullString, zsMessageTitle)
> 'Close Msgbox
> Call SendMessage(lHwndMsgbox, WM_CLOSE, 0, ByVal 0&)
> End Sub
> 
> 
> 
> 'Purpose     :  Extended version of Msgbox, has extra parameter to set time msgbox is displayed for
> 'Inputs      :  As per Msgbox
> '               [DisplayTime]               The time in MS to display the message.
> 'Outputs     :  As per Msgbox
> 'Author      :  Andrew Baker
> 'Date        :  03/01/2001 13:23
> 'Notes       :
> 'Revisions   :
> 
> Function Msgbox2(Prompt As String, Buttons As VbMsgBoxStyle, Title As String, Optional DisplayTime As Long) As VbMsgBoxResult
> If DisplayTime > 0 Then
> 'Enable the timer
> StartTimer DisplayTime
> zsMessageTitle = Title
> End If
> Msgbox2 = MsgBox(Prompt, Buttons, Title)
> 'Stop the timer
> EndTimer
> End Function
> 
> ''Demonstration routine
> Sub TestMessage()
> Dim lRetVal As VbMsgBoxResult
> lRetVal = Msgbox2("hello .. the program is fully functional." & vbCrLf & _
> "Per la verifica delle vincite andare indietro col cursore ogni 5° del mese.", vbOKOnly + vbInformation, "AVVISO!!!", 6000)
> Debug.Print lRetVal
> End Sub
> 
> 
> Also likely to concern.


----------



## Andrew Fergus

Hello Everyone
Is it possible to have a timer on a VBA message box such that it disappears after a set interval?  I have a standard VBA MsgBox informing the user that an action was successful - how can I make the message box disappear (as if Ok was pressed) after 3 seconds?  Is this possible?  I'm new with VB and am using it in conjuntion with Access 2000.
TIA, Andrew


----------



## Anoniempje

I know this reply is 10 years later than when the question, above, was asked, but I'm sure some people might be looking of any solution on implementing a timed message box in Excel, as was I, today. Using a couple of sources (which I'll mention, below) I made a combinated VBA code. 

What the code does, it makes the message box appear some seconds after you open your Excel file and it makes the message box automatically close after some seconds. Ideal if you want to show a welcome message. Well, here goes:

*Put this code inside a module*


		VBA Code:
__


Option Explicit
Const PopupDurationSecs As Integer = 3
Sub startTimer()
Application.OnTime Now + TimeValue("00:00:03"), "myShellMessageBox"
End Sub

Sub myShellMessageBox()
    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


*Copy this code in "ThisWorkbook":


		VBA Code:



[/B]
Private Sub Workbook_Open()
startTimer
End Sub
[B]


Sources:*
Codes for automatically starting the VBA message box: from: Dinesh Kumar Takyar.
Code for automatically closing the VBA message box: from: Microsoft

Hope it helps you guys!


----------



## 2Took

Tom Urtis said:


> You probably would have received a response sooner if this was asked on the Excel Questions board, but at any rate here's one way to do what you want:
> 
> Sub Test1()
> Dim AckTime As Integer, InfoBox As Object
> Set InfoBox = CreateObject("WScript.Shell")
> AckTime = 3
> Select Case InfoBox.Popup("Click OK or do nothing within 3 seconds.", _
> AckTime, "This is your Message Box", 0)
> Case 1, -1
> Exit Sub
> End Select
> End Sub
> 
> Keep in mind that the first time code compiles, it can take a few extra moments, so on the first execution of this macro, it will take longer than 3 seconds for 3 seconds to elapse.  Run the code twice and see if the second time around is closer to 3 seconds than the first execution, which it was for me.


17 years later... 
That box does not go away by itself after many seconds have passed and running the code more than once. 
Is there maybe something that needs to be added in Tools/References?


----------

