Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 24th, 2005, 01:32 PM
Authorized User
 
Join Date: Oct 2003
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
Default "Wait" function in VBA

Hi Everyone,

Does anyone know an API or other function to run the
equivalent of the VB Wait() function using VBA?

The code runs within an MS Access module, so I can't
use the TimerInterval available with forms.

Any help would be SUPER appreciated!

Thank You,

Warren
:)
 
Old January 24th, 2005, 02:35 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Far as I know, the only way to pause processing in VBA is through work-around error handling solutions. VBA doesn't give you any control over process or thread execution with methods like Thread.Sleep() or Wait(), etc, that I'm aware of. You could try something like the following. Here I'm trying to write to a file that some other process is hypothetically creating, and need to pause processing if the file doesn't exist yet:

Sub WriteToFile()

    Dim intWait As Integer

    Set oFso = CreateObject("Scripting.FileSystemObject")

    On Error Resume Next

    Set oFile = oFso.OpenTextFile("C:\SomeFile.txt", ForAppending, True)

    ' Error opening file(e.g. doesn't exist yet).
    Do While Err.Number <> 0
        ' Slow the processing down a bit before trying again
        For intWait = 1 To 100
            DoEvents
        Next
        Err.Clear
    ' Try opening file again.
    Set oFile = fso.OpenTextFile("C:\LogCalls.txt", ForAppending, True)
    Loop

    'Write to text file.........................

    oFile.Close
    Set oFile = Nothing
    Set oFso = Nothing

End Sub

Don't know if thats at all helpful, but might give you some ideas.

HTH,

Bob

 
Old January 24th, 2005, 02:38 PM
Authorized User
 
Join Date: Oct 2003
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you, Bob! :)
 
Old January 24th, 2005, 04:01 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep(600000)

This sleeps for 10 minutes, I think. I can't remember how long 600K is. It's either 5 or 10 minutes.





mmcdonal
 
Old January 24th, 2005, 04:11 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hi mmcdinal,

Which object library is WScipt in? Can it be referenced in a VBA project, or is it just a VBScript thing? I tried referncing the Windows Script Host Object Model in VBA, but can't instantiate an instance of WScript.Shell.

- Bob

 
Old February 3rd, 2005, 08:48 PM
Registered User
 
Join Date: Feb 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Something I've used as a workaround before is the wait option in a SendKeys statement. The line below will send a <Shift> and wait until the keystroke is processed before returning control to the procedure.

SendKeys "+", True

Regards,

Bonnie

 
Old February 4th, 2005, 05:57 PM
Authorized User
 
Join Date: Oct 2003
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
Default

And I have returned with good news, everyone. Put this
into your Declarations section in either your module
or form/report source code:

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Here's a sample call:

Sleep (5000) ' Waits for 5 seconds

Rescued by an API!

Thank you all for your help!
 
Old May 30th, 2006, 12:19 PM
Registered User
 
Join Date: May 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello everyone,

Just wondering if someone has a way to pause a macro in VBA? My macro is running a calculation that needs to be updated frequently, but by pausing, I am unable to work in Excel, and I need to work while the macro is on pause. The macro I have used is as follow:

...
For a = 0 To 100

'My calcs are all here...

newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 10
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime
If Application.Wait(Now + TimeValue("0:00:10")) = true Then
    End If
Next a

'Thanks
 
Old May 30th, 2006, 12:23 PM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Hi Sebastien,

If you are wanting to update something frequently, a better way of doing it would be to add a timer control to the form, and set the update process to run on it. I haven't done one of these for a while, but let me know if you can't find any examples of how to do it, and I'll see if I can walk you through it.

Mike

Mike
EchoVue.com
 
Old May 31st, 2006, 01:21 AM
Registered User
 
Join Date: May 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I use the following function:

Public Function fnWait(intNrOfSeconds As Integer)
Dim varStart As Variant
  varStart = Timer
  Do While Timer < varStart + intNrOfSeconds
  Loop
End Function






Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA function help carbon_13 Excel VBA 2 March 5th, 2008 05:17 PM
Wait In VB ashes0000 Beginning VB 6 5 February 6th, 2007 11:47 AM
Should I wait for .net 2 androoo ADO.NET 2 November 3rd, 2004 09:07 AM
help me! wait online EdmondWang All Other Wrox Books 1 December 18th, 2003 06:16 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.