ngreenleaf
Programmer
Below is a code segment from one of my forms. I have used this same logic elsewhere and it works fine. This is the only place I think I've done it with 2 recordsets open at the same time. The second loop (on Delete1) only executes once - even when there are three records in the recordset. I have validated the queries... they work fine. I have added a loop on Delete1 that occurs immediately after the recordset is opened and verified that all three records from the query are in the recordset. Can anyone advise me regarding this?
Set Delete1 = Dbs.OpenRecordset(SQLSel1Query)
Set Delete2 = Dbs.OpenRecordset(SQLSel2Query)
DoCmd.RunSQL (SQLDel3Query)
If (Not Delete2.BOF) Or (Not Delete2.EOF) Then
Delete2.MoveFirst
End If
Do While Not Delete2.EOF
SQLDel2Query = "DELETE FROM table2 " & _
"WHERE table2.pri_key = " & Delete2![del2] & ";"
DoCmd.RunSQL (SQLDel2Query)
Delete2.MoveNext
Loop
Delete2.Close
If (Not Delete1.BOF) Or (Not Delete1.EOF) Then
Delete1.MoveFirst
End If
Do While Not Delete1.EOF
SQLDel1Query = "DELETE FROM table1 " & _
"WHERE table1.pri_key = '" & Delete1![del1] & "';"
DoCmd.RunSQL (SQLDel1Query)
Delete1.MoveNext
Loop
DeletePublishers.Close
NOTES: SQLSel1Query and SQLSel2Query are SELECT queries that must be run before data is deleted in table3 (SQLDel3Query). But due to referential integrity it is necessary to delete from table3 first, then table2 and finally table1. The SELECT statements select the primary keys to delete AS del1 and del2, respectively.
The table3 delete (SQLDel3Query) works fine. The loop to delete from table2 works fine. But the loop to delete from table1 stops after one iteration. I did verify that the first iteration is looking at the value at the top of the recordset. I have also verified that SQLDel1Query and SQLDel2Query are fine -- if the loop would iterate.
As an FYI... I solved the problem using the following logic:
1) open Delete1 recordset
2) loop through Delete1 recordset building a query like
"Delete from table1 where pri_key in ('val1','val2','...);
3) close Delete1 recordset
4) open Delete2 recordset
5) loop through Delete2 recordset creating a query like
"Delete from table2 where pri_key in (val1,val2,...);
6) close Delete2 recordset
7) run the three delete queries in the proper order
This works fine. Thus I am questioning if my problem is because I had two recordsets open at one time.
Set Delete1 = Dbs.OpenRecordset(SQLSel1Query)
Set Delete2 = Dbs.OpenRecordset(SQLSel2Query)
DoCmd.RunSQL (SQLDel3Query)
If (Not Delete2.BOF) Or (Not Delete2.EOF) Then
Delete2.MoveFirst
End If
Do While Not Delete2.EOF
SQLDel2Query = "DELETE FROM table2 " & _
"WHERE table2.pri_key = " & Delete2![del2] & ";"
DoCmd.RunSQL (SQLDel2Query)
Delete2.MoveNext
Loop
Delete2.Close
If (Not Delete1.BOF) Or (Not Delete1.EOF) Then
Delete1.MoveFirst
End If
Do While Not Delete1.EOF
SQLDel1Query = "DELETE FROM table1 " & _
"WHERE table1.pri_key = '" & Delete1![del1] & "';"
DoCmd.RunSQL (SQLDel1Query)
Delete1.MoveNext
Loop
DeletePublishers.Close
NOTES: SQLSel1Query and SQLSel2Query are SELECT queries that must be run before data is deleted in table3 (SQLDel3Query). But due to referential integrity it is necessary to delete from table3 first, then table2 and finally table1. The SELECT statements select the primary keys to delete AS del1 and del2, respectively.
The table3 delete (SQLDel3Query) works fine. The loop to delete from table2 works fine. But the loop to delete from table1 stops after one iteration. I did verify that the first iteration is looking at the value at the top of the recordset. I have also verified that SQLDel1Query and SQLDel2Query are fine -- if the loop would iterate.
As an FYI... I solved the problem using the following logic:
1) open Delete1 recordset
2) loop through Delete1 recordset building a query like
"Delete from table1 where pri_key in ('val1','val2','...);
3) close Delete1 recordset
4) open Delete2 recordset
5) loop through Delete2 recordset creating a query like
"Delete from table2 where pri_key in (val1,val2,...);
6) close Delete2 recordset
7) run the three delete queries in the proper order
This works fine. Thus I am questioning if my problem is because I had two recordsets open at one time.