• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 581
  • Last Modified:

Show a form for a few seconds

I'd like to show a form for 3 seconds and have it go away.  The form only has a picture control and text (kind of like a splash screen but launched during application execution). When I use MyForm.Show it sits and waits.
(This is an Excel VBA)

What's an easy way to do this?

Thanks.
0
JohnSantaFe
Asked:
JohnSantaFe
1 Solution
 
zorvek (Kevin Jones)ConsultantCommented:
Add this code to the user form:

Private Declare Sub Sleep Lib "kernel32" (ByVal Milliseconds As Long)

Private Sub Pause( _
      ByVal Seconds As Single, _
      Optional ByVal PreventVBEvents As Boolean _
   )

' Pauses for the number of seconds specified. Seconds can be specified down to
' 1/100 of a second. The Windows Sleep routine is called during each cycle to
' give other applications time because, while DoEvents does the same, it does
' not wait and hence the VB loop code consumes more CPU cycles.

   Const MaxSystemSleepInterval = 25 ' milliseconds
   Const MinSystemSleepInterval = 1 ' milliseconds
   
   Dim ResumeTime As Double
   Dim Factor As Long
   Dim SleepDuration As Double
   
   Factor = CLng(24) * 60 * 60
   
   ResumeTime = Int(Now) + (Timer + Seconds) / Factor
   
   Do
      SleepDuration = (ResumeTime - (Int(Now) + Timer / Factor)) * Factor * 1000
      If SleepDuration > MaxSystemSleepInterval Then SleepDuration = MaxSystemSleepInterval
      If SleepDuration < MinSystemSleepInterval Then SleepDuration = MinSystemSleepInterval
      Sleep SleepDuration
      If Not PreventVBEvents Then DoEvents
   Loop Until Int(Now) + Timer / Factor >= ResumeTime
   
End Sub

Private Sub UserForm_Activate()

   Pause 3
   Unload Me

End Sub

Kevin
0
 
JohnSantaFeAuthor Commented:
Thanks.
The DoEvents is key
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now