Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I delay code execution, with an optional message to the user?

How To

How can I delay code execution, with an optional message to the user?

by  RickSpr  Posted    (Edited  )
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"

=============
Instructions
=============
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:
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
    Else
        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.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top