timed msgbox?

T

theintern

I want to show a msgbox asking the user a question. if they don't respond
within 3 seconds, i want the answer to default to "No" and the code to
continue. is there a way to do this?

thanks
scott
 
J

John

theintern said:
I want to show a msgbox asking the user a question. if they don't respond
within 3 seconds, i want the answer to default to "No" and the code to
continue. is there a way to do this?

thanks
scott

Scott,
Sure, you can set up a simple loop using the Timer function, although
you probably want to use the InputBox function instead of MsgBox. A
MsgBox is primarily for information messages for users whereas an input
box provides for user entry. Design it to bail from the loop when the
user answers the question or the timer hits 3 seconds, whichever occurs
first.

Just a suggestion, 3 seconds isn't all that long. If a new user takes a
second or two to just read the message, they may not have time to enter
an answer before, oops! its too late.

John
Project MVP
 
T

theintern

i only want them to be able to select yes or no. I dont' want them to have
to type anything. and how do you code it to close that box after 3 seconds?
i'm also playing around with using a form. if i do that, how do i tell it
test to see if there was input, and if not to do a certain thing? big goal:
i want a box to flash up a question with yes/no; if the user does anything, i
need it to default to no and run ImportAll. below is my failed attempt with
the form. thanks John!

Sub StartUp()
Dim waitTime As Integer
Dim stayopen As Integer

stayopen = 3
waitTime = 1
Start = Timer
While Timer < Start + waitTime
frmLeaveOpen.Show
Wend
If frmLeaveOpen.Enabled = True Then
stayopen = 0
frmLeaveOpen.Hide
ImportAll (stayopen)
End If

End Sub
 
R

Rod Gill

Hi,

I'm not sure this is possible. When a form or a msgbox is displayed, control
is not returned to the next line of code until the form/dialog is closed.

There is no event that triggers itself after a time period, and VBA does not
support concurrent processing in different streams like VB or C#.

The only work around is to have a form that allows the user to enter
preferences before running the main macro.
--

Rod Gill
Microsoft MVP for Project

Author of the only book on Project VBA, see:
http://www.projectvbabook.com
 
T

theintern

Well, I just did it ;) see code below.
you know, on a more sentimental note, i'm finishing up this internship on
tuesday of next week. i've been on these forums for almost the entire
summer, posting questions and learning from you gurus. it just somehow seems
like a good ending for me to one up the infamous Rod Gill and achieve that
which isn't possible. On this high note, thank you to all you VBAs out there
who have put up with my questions. I have you to thank for so much. Keep up
the great work. Maybe I'll be back some day.

thanks
scott

first this code runs:
Sub StartUp()
frmLeaveOpen.Show
End Sub

then this is the code for the frmLeaveOpen:
Option Explicit

Private Sub Nobtn_Click()
Dim stayopen As Integer
stayopen = 0
frmLeaveOpen.Hide
ImportAll (stayopen)

End Sub

Private Sub UserForm_Activate()
Dim waitTime As Integer
Dim stayopen As Integer
Dim start As Long

stayopen = 0
waitTime = 3
start = Timer
While Timer < start + waitTime
DoEvents
Wend
If frmLeaveOpen.Visible = True Then
frmLeaveOpen.Hide
ImportAll (stayopen)
End If

End Sub


Private Sub Yesbtn_Click()
Dim stayopen As Integer
stayopen = 1
frmLeaveOpen.Hide
ImportAll (stayopen)

End Sub

If the user clicks Yes or No, it handles accordingly. But if the user does
nothing it defaults as if they were to say no, and runs ImportAll with that
in mind. Beautiful.
 
J

John

theintern said:
Well, I just did it ;) see code below.
you know, on a more sentimental note, i'm finishing up this internship on
tuesday of next week. i've been on these forums for almost the entire
summer, posting questions and learning from you gurus. it just somehow seems
like a good ending for me to one up the infamous Rod Gill and achieve that
which isn't possible. On this high note, thank you to all you VBAs out there
who have put up with my questions. I have you to thank for so much. Keep up
the great work. Maybe I'll be back some day.

thanks
scott

first this code runs:
Sub StartUp()
frmLeaveOpen.Show
End Sub

then this is the code for the frmLeaveOpen:
Option Explicit

Private Sub Nobtn_Click()
Dim stayopen As Integer
stayopen = 0
frmLeaveOpen.Hide
ImportAll (stayopen)

End Sub

Private Sub UserForm_Activate()
Dim waitTime As Integer
Dim stayopen As Integer
Dim start As Long

stayopen = 0
waitTime = 3
start = Timer
While Timer < start + waitTime
DoEvents
Wend
If frmLeaveOpen.Visible = True Then
frmLeaveOpen.Hide
ImportAll (stayopen)
End If

End Sub


Private Sub Yesbtn_Click()
Dim stayopen As Integer
stayopen = 1
frmLeaveOpen.Hide
ImportAll (stayopen)

End Sub

If the user clicks Yes or No, it handles accordingly. But if the user does
nothing it defaults as if they were to say no, and runs ImportAll with that
in mind. Beautiful.

Scott,
Thanks for the vote of confidence for what we all try to do to help, and
thanks for posting the code you developed.

John
Project MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top