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!

Code running too fast

Status
Not open for further replies.

awhitsel

Programmer
Feb 22, 2001
80
0
0
US
I have a problem with a piece of VB coding in one of my programs.

The coding is supposed to run a DOS batch file, and then run a two reports and a query.

The problem is that the first report wants to start printing before the batch file is finished running and I end getting an error that I cannot access the file.

Is there any way that I can get the program to wait until the batch file is finished running before running the reports?

 
Access tends to get ahead of itself.
Try:
DoEvents: DoEvents: DoEvents

If that doesn't work try a wait function:
Public Function Do_Wait(iSec As Integer)

Dim newHour As Integer
Dim newMinute As Integer
Dim newSecond As Integer
Dim waitTime As Date

newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + iSec
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime
End Function
 
This is quite a handy Kernel call that you can use.

Add the following line to the top of a module:

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

then in your code you can tell the system to wait for a period set in milliseconds.

i.e

msgbox "I will not respond for 5 seconds"

Sleep 5000

msgbox "hello again"

Give it a go, I use it when I want to show splash screens. Avoids the use of tricky timers sometimes.
 
You can also run the batch file from a system command and tell the system not to return until it is done...

I have placed the code on the net at:


Let me know if you need anymore details...
 
Waiting for a set period of time works in some situations, but if the length of time for the batch file changes, I've used this technique...

After the file is created/updated/whatever by the batch file, have the batch file create a second file to be used as a signal to Access that the first file is ready.

Example:
Code:
if exist del signal.txt
<code to create/update your file>
echo FINISHED>signal.txt

This will delete the signal file, run your regular code, then create the file again. Meanwhile in Access, check for the exixtance of the signal.txt file periodically (use one of the timer events above, or a bunch of doevents, then do a dir() function to check if the file exists).
 
You could keep looping until the file appears? I have used this technique when waiting to connect to an external database.

I'll post the code when I can retrieve it. Sandy
 
Try something like this, which should wait until file is found or a terminate manual or automatic is triggered.

You'll have to put in your own files and conditions

With Application.FileSearch
Do While
.LookIn = FileLoc
.SearchSubFolders = False
.FileName = &quot;name.sfx&quot; 'name of file being created
If .Execute() > 0 Then
'Do rest of operations here
' then set end loop condition
Else
'Ask if want to continue here
'count number of events between questions or set a fixed number of loops
'if automatic
'set end loop if terminate initiated
End If
loop
End With
Sandy
 
hi n2ckb...

I'm intrigued as to how i would implement the sleep kernel to display my custom splash.

What code can I use to disply the splash ?
autoexec macro (runcode)?

not sure how to do this.

Thx DC Hub: gameheads.dns2go.com
siglavey.jpg
 
Greetings!

This is how I force vb to wait for a DOS batch file to finish for me:

Private Declare Function WaitForSingleObject Lib &quot;kernel32&quot; _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Declare Function CloseHandle Lib &quot;kernel32&quot; _
(ByVal hObject As Long) As Long

Private Declare Function OpenProcess Lib &quot;kernel32&quot; _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long


Dim iTask As Long, ret As Long, pHandle As Long

iTask = Shell(NAMEOFBATCHFILE, vbNormalFocus)
pHandle = OpenProcess(SYNCHRONIZE, False, iTask)
ret = WaitForSingleObject(pHandle, INFINITE)
ret = CloseHandle(pHandle)

Don't forget to set the properties for the DOS batch file to close the DOS window on exit - otherwise the window will stay open on the desktop until the user closes it with the mouse!!!

Good Luck!!
[spidey] [americanflag] [unclesam]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top