Results 1 to 4 of 4

Wait/Sleep Programming

  1. #1
    Perfac is offline Competent Performer
    Windows 7 64bit Access 2016
    Join Date
    Jan 2018
    Location
    Gauteng Johannesburg
    Posts
    274

    Wait/Sleep Programming

    I have been trying to create a wait/sleep function where other code executes at the same time a "Do while loop" is running to prevent a form from executing its remaining code.

    'more code above this
    DoCmd.OpenForm "f01Employee", acNormal
    Do While CurrentProject.AllForms("f01Employee").IsLoaded
    Pause (1)
    Loop
    'more code below this that i want to run after "f01Employee" is closed

    Where Pause():
    Public Function Pause(NumberOfSeconds As Variant)
    On Error GoTo Error_GoTo

    Dim PauseTime As Variant
    Dim Start As Variant
    Dim Elapsed As Variant

    PauseTime = NumberOfSeconds
    Start = Timer
    Elapsed = 0
    Do While Timer < Start + PauseTime
    Elapsed = Elapsed + 1
    If Timer = 0 Then
    'Crossing midnight
    PauseTime = PauseTime - Elapsed
    Start = 0
    Elapsed = 0
    End If
    DoEvents
    Loop


    Exit_GoTo:
    On Error GoTo 0
    Exit Function
    Error_GoTo:
    Debug.Print Err.Number, Err.Description, Erl
    GoTo Exit_GoTo
    End Function

    I put a step through debug in the vba on "f01Employee" Form, it stepped through the Form_Load() event, but when I click on any button no Vba or macro executes,


    I think the "Pause" function being called is preventing my buttons on the "f01Employee" form to run. When i run the "f01Employee" form without opening it along with the "Do While loop" all the buttons execute correctly, if there is no work around with the "do while loop" then is there a way I can open the "f01Employee" while i stop code on the previous form from running until the user is done on the "f01Employee" form?

  2. #2
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    1,248
    If you open the second form as a modal dialog all code on the calling form is suspended until that form is closed.

    No need to try and re-invent the wheel.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    isladogs's Avatar
    isladogs is offline Very intense programming
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    3,337
    Agree with Minty for this particular scenario. However in case its useful for the future, add this code to a standard module:

    Code:
    'Add PtrSafe - required for 64-bit Office (VBA7)
    #If VBA7 Then
        Private Declare PtrSafe Function SetCurrentDirectory Lib "kernel32" Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Long
        Declare PtrSafe Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
    #Else '32-bit Office
        Private Declare Function SetCurrentDirectory Lib "kernel32" Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Long
        Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
    #End If
    '###############################################
    
    Sub Wait(N As Integer)  'creates a delay while other programs execute
        For zCount = 1 To N
            Sleep 1000              'sleep function from api library file
        Next zCount
        DoEvents                    'allow screen to refresh
    End Sub
    Then just use e.g. Sleep 5 for a 5 second delay in all code processing
    If you just want time for the CPU to complete earlier code before proceeding, just use DoEvents
    Colin (Mendip Data Systems) : Website, email
    If this has helped, please click the star button and leave a comment

  4. #4
    Perfac is offline Competent Performer
    Windows 7 64bit Access 2016
    Join Date
    Jan 2018
    Location
    Gauteng Johannesburg
    Posts
    274
    Quote Originally Posted by Minty View Post
    If you open the second form as a modal dialog all code on the calling form is suspended until that form is closed.

    No need to try and re-invent the wheel.
    Thank you very much for this advice, I have set "f01Employee"'s form property "Modal" = yes and when opening from another form I used:

    DoCmd.OpenForm "f01Employee", , , , , acDialog

    to Open it in "modal dialog", this causes the form to not be fullscreen, wondering if there is a way to edit/change this?

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 07-03-2017, 11:23 PM
  2. Replies: 4
    Last Post: 05-29-2015, 08:36 AM
  3. Replies: 4
    Last Post: 04-08-2015, 02:05 AM
  4. Form goes to sleep
    By aytee111 in forum Forms
    Replies: 2
    Last Post: 04-23-2012, 04:23 PM
  5. wait and shell
    By maxbre in forum Programming
    Replies: 19
    Last Post: 11-03-2011, 10:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Tech Forums: Microsoft Office Forums