Does VBA run too fast, where it gets itself confused?

MackMan

Registered User.
Local time
Today, 05:31
Joined
Nov 25, 2014
Messages
174
An odd title.
However, I have an issue and was wondering if any others have had a similar problem.

When I'm running VBA not using a Break Point, to generate a random long number (based on date and number of entries) that needs to be inserted in a field in the table, using a loop statement, it sometimes gets muddled up and creates duplicates where none are allowed. other times it works perfectly.

However, if I use the break point, and step through it using [F8] all is good (every time). It creates all the necessary date and entry based numbers no problem, and no dupes. This has happened on a couple of occasions, and on other occasions has worked perfectly without the need of a break point.

Anyone else experienced this, where they run VBA, and all is good, and other times, it doesn't do what you wanted it to?

If so, what was your work to overcome this?

Is there a way to slow it down (even more odd)!
As always, thank you for your time.
 
Access is asynchronous, which is to say, if given a series of commands, it starts to execute one, moves on to the next one and starts executing it, and so forth. It doesn't wait for the first command to be completed before starting the second one, and this can cause timing problems...as you're apparently experiencing!

One example would be a button that runs a series of Queries where all but the first Query is dependent upon the previous Query being completed before it starts to execute. The following VBA code

Code:
DoCmd.OpenQuery "QueryA"
DoCmd.OpenQuery "QueryB"
DoCmd.OpenQuery "QueryC"
will immediately run all three, not waiting for one to finish executing before starting the next one. The answer to halting the code in this type of situation is to use DoEvents.

Code:
DoCmd.OpenQuery "QueryA"
DoEvents
DoCmd.OpenQuery "QueryB"
DoEvents
DoCmd.OpenQuery "QueryC"

DoEvents returns control to Windows, allowing QueryA to complete running before starting to run QueryB. It then allows QueryB to finish running before starting QueryC.

DoEvents is an easy, safe bet when encountering what seems to be timing issues.

Linq ;0)>
 
Linq. Thank you. This is exactly what's been happening. Now.. works perfect every time. thank you thank you!
 

Users who are viewing this thread

Back
Top Bottom