Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Microsoft: Access Modules (VBA Coding) FAQ

How To

How can I delay code execution, with an optional message to the user? by RickSpr
Posted: 20 Sep 04

Sometimes you need to delay code execution for a set amount of time. If that time is more than 2 or 3 seconds, it's a good idea to let the user know what's going on.

The following instructions will create a form you can use for this purpose. You have the option of making it invisible or having it display a message, centered in the Access main window.

To delay for a predetermined number of seconds with no message, open the form as follows:
    DoCmd.OpenForm "frmDelay", OpenArgs:="10"
where "10" is the number of seconds.

To delay with a user message displayed in the middle of the Access window, do this:
    DoCmd.OpenForm "frmDelay", OpenArgs:="10,Your message goes here"

Create a form and leave it blank. Set the following properties:
Caption = "One moment please..."
Pop Up = Yes
Modal = Yes
Border Style = Thin
Navigation Buttons = No
Record Selectors = No
Control Box = No
Min Max Buttons = None
Close Box = No
Width = 2.5"
On Open = "[Event Procedure]"
On Timer = "[Event Procedure]"

Select the Detail section and set the following property:
Height = 1"

Add a label control in the center of the form and set its properties:
Name = "lblMessage"
Caption = "Please wait"
Left = 0.0833"
Top = 0.4167"
Width = 2.3333"
Height = 0.1667"
Text Align = Center

Open the form module and insert the following code:


Private Sub Form_Open(Cancel As Integer)
    Dim intDelay As Integer
    Dim strMessage As String
    Dim i As Integer
    If IsNull(Me.OpenArgs) Then
        Cancel = True
        Exit Sub
    End If
    i = InStr(Me.OpenArgs & ",", ",")
    intDelay = Val(Left$(Me.OpenArgs, i - 1))
    strMessage = Mid$(Me.OpenArgs, i + 1)
    If intDelay <= 0 Then
        Cancel = True
        Exit Sub
    End If
    If Len(strMessage) = 0 Then
        DoCmd.MoveSize 0, 0, 0, 0
        lblMessage.Caption = strMessage
    End If
    Me.TimerInterval = Val(Me.OpenArgs) * 1000
    DoCmd.Hourglass True
End Sub

Private Sub Form_Timer()
    Me.TimerInterval = 0
    DoCmd.Hourglass False
    DoCmd.Close acForm, Me.Name
End Sub
Save the form as frmDelay.

Back to Microsoft: Access Modules (VBA Coding) FAQ Index
Back to Microsoft: Access Modules (VBA Coding) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close