Must Run Queries Twice to Work Properly

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I originally posted this message on another forum to no avail, so I'm now
trying my luck here. I have a 2 Append Queries. The first query inserts a
Master record and the second query inserts detail records. I have a button
click event which fires the code below which runs both queries. The problem
is that if I click the button once I only get the Master Query to work, and
in order to get the Detail Query to work I need to click the button a 2nd
time. Therefore in order to properly execute both queries as shown below I
must always click the button twice. Any ideas why this is happening? Thanks!

DoCmd.SetWarnings False
DoCmd.OpenQuery "qryConvertPOToCM" 'Insert MASTER record
DoCmd.OpenQuery "qryConvertPOToCMDetail" 'Insert DETAIL records
DoCmd.SetWarnings True
 
Just a guess, but it may be that the second query starts running before the
first query's records have been fully written into the table by the Jet
engine; thus, the second query isn't finding any parent records yet.

Try inserting a DoEvents between the two queries:

DoCmd.SetWarnings False
DoCmd.OpenQuery "qryConvertPOToCM" 'Insert MASTER record
DoEvents
DoCmd.OpenQuery "qryConvertPOToCMDetail" 'Insert DETAIL records
DoCmd.SetWarnings True
 
Thanks Ken! I also figured out that I needed to requery a subform, as one of
it's controls was being referenced in the query before it contained data.
After implementing DoEvents along with this, it works. Thanks!
 
Back
Top