Results 1 to 7 of 7
  1. #1
    Perfac's Avatar
    Perfac is offline Expert
    Windows 7 64bit Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618

    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
    3,001
    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 MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    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, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  4. #4
    Perfac's Avatar
    Perfac is offline Expert
    Windows 7 64bit Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    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?

  5. #5
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    You don't need both modal= yes and the acDialog property.to prevent users clicking elsewhere.
    If you only use modal and add DoCmd.Maximize to the Form_Load event it will work fine whether you use Tabbed Documents or Overlapping Windows display mode

    BTW my last reply should have said use Wait=5 for a 5 second delay … OR use Sleep = 5000
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  6. #6
    Perfac's Avatar
    Perfac is offline Expert
    Windows 10 Access 2016
    Join Date
    May 2016
    Location
    Centurion Pretoria
    Posts
    618
    Hi, thank you. Ruben(22) my son and partner placed this thread a year ago on my profile, he has his own. I just marked it as solved today since I missed that last year.

  7. #7
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    Hi
    Not sure what brought this to my attention today. Perhaps because you clicked the star button??? Anyway, I'd completely missed the fact that it was an old thread.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

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

Similar Threads

  1. Replies: 4
    Last Post: 07-04-2017, 12:23 AM
  2. Replies: 4
    Last Post: 05-29-2015, 09:36 AM
  3. Replies: 4
    Last Post: 04-08-2015, 03:05 AM
  4. Form goes to sleep
    By aytee111 in forum Forms
    Replies: 2
    Last Post: 04-23-2012, 05:23 PM
  5. wait and shell
    By maxbre in forum Programming
    Replies: 19
    Last Post: 11-03-2011, 11: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
  •  
Other Forums: Microsoft Office Forums