G
Guest
I have a bit of code that is giving me problems of migraine proportion, and
hoping for some assistance here. I am runnning a procedure to update a table
with data gathered from an Excel file. This procedure loops through each
record from the external source, updates the table in the database, and sets
a flag to mark that the record was processed. After this is done, all records
that were not flagged are removed from the table. Each run is between 5 and
20 records, updating a table of 200+.
This is where the problem occurs. Anytime a record already exists in the
table, and is updated from the existing table, it gets deleted. If I step
through the code there are 0 problems - all existing records that are
supposed to stay are fine, all new record are fine, all existing records that
are supposed to go away are deleted. However, if I let the code run without
any breaks or manual stepping, then the unintended deletions start. I believe
it's a way I'm using the recordset that the table does not register the
changes before making the deletion, but I can't trace it down. Any help would
be appreciated.
The table that is being updated is set up as follows (shorthand)
ProjectNumber (Text(6), PrimaryKey)
Bunch of Data fields
Check (Integer)
The "Check" field is the flag to determine which records to delete after the
procedure executes.
The code that is executed is as follows (hybrid skeleton code and VBA, bear
with me, I'm in a rush):
Private Sub UpdateProjects (ByRef conDB As ADODB.Connection)
Dim rsProjects As ADODB.Recordset 'Recordset of all records in tblProjects
Dim rsSourceProjects As ADODB.Recordset 'Recordset of all records in
tblSource-ProjectList
Dim strProjectNumber as String
Import Excel file into tblSource-ProjectList
'Set the check flag of all records to 0
CurrentDB.Execute "Update tblProjects SET Check = 0", dbFailOnError
rsProjects.Open "tblProjects", conDB, adOpenKeyset, adLockOptimistic,
adCmdTableDirect
rsSourceProjects.Open "tblSource-ProjectList", conDB, adOpenStatic,
adLockReadOnly, adCmdTable
rsSourceProjects.MoveFirst
'All records in rsSourceProjects will be for the same project, so record it
for later use
strProjectNumber = rsSourceProjects("ProjectNumber")
For all records in rsSourceProjects
Search rsProjects for the current record of rsSourceProjects
If Not Found Then
rsProjects.AddNew
rsProjects("ProjectNumber") = strProjectNumber
End If
Update each field in rsProjects with data from rsSourceProjects
rsProjects("Check") = 1
rsProjects.Update
Next
rsProjects.Close
rsSourceProjects.Close
'The following command will delete all records in the Project table for the
project that was processed (tracked in strProjectNumber) that was not
processed (Check was left at 0)
CurrentDB.Execute "DELETE * FROM Projects WHERE ProjectNumber = """ &
strProjectNumber & """ AND Check = 0", dbFailOnError
End Sub
Again, there's more to it then just that, but short and sweet, that's what
I'm working with. I know there are different ways to do this procedure, but
this is what I am working with.
I believe the problem occurs when the Delete query is executed. The table,
for one reason or another, is not getting the updates within the loop. The
"Check" field is not being set to 1 for the records that exist in both
tblProjects and tblSource-ProjectList, and they get deleted. Is this a
problem with ADO interacting with the tables, or something more underlying?
hoping for some assistance here. I am runnning a procedure to update a table
with data gathered from an Excel file. This procedure loops through each
record from the external source, updates the table in the database, and sets
a flag to mark that the record was processed. After this is done, all records
that were not flagged are removed from the table. Each run is between 5 and
20 records, updating a table of 200+.
This is where the problem occurs. Anytime a record already exists in the
table, and is updated from the existing table, it gets deleted. If I step
through the code there are 0 problems - all existing records that are
supposed to stay are fine, all new record are fine, all existing records that
are supposed to go away are deleted. However, if I let the code run without
any breaks or manual stepping, then the unintended deletions start. I believe
it's a way I'm using the recordset that the table does not register the
changes before making the deletion, but I can't trace it down. Any help would
be appreciated.
The table that is being updated is set up as follows (shorthand)
ProjectNumber (Text(6), PrimaryKey)
Bunch of Data fields
Check (Integer)
The "Check" field is the flag to determine which records to delete after the
procedure executes.
The code that is executed is as follows (hybrid skeleton code and VBA, bear
with me, I'm in a rush):
Private Sub UpdateProjects (ByRef conDB As ADODB.Connection)
Dim rsProjects As ADODB.Recordset 'Recordset of all records in tblProjects
Dim rsSourceProjects As ADODB.Recordset 'Recordset of all records in
tblSource-ProjectList
Dim strProjectNumber as String
Import Excel file into tblSource-ProjectList
'Set the check flag of all records to 0
CurrentDB.Execute "Update tblProjects SET Check = 0", dbFailOnError
rsProjects.Open "tblProjects", conDB, adOpenKeyset, adLockOptimistic,
adCmdTableDirect
rsSourceProjects.Open "tblSource-ProjectList", conDB, adOpenStatic,
adLockReadOnly, adCmdTable
rsSourceProjects.MoveFirst
'All records in rsSourceProjects will be for the same project, so record it
for later use
strProjectNumber = rsSourceProjects("ProjectNumber")
For all records in rsSourceProjects
Search rsProjects for the current record of rsSourceProjects
If Not Found Then
rsProjects.AddNew
rsProjects("ProjectNumber") = strProjectNumber
End If
Update each field in rsProjects with data from rsSourceProjects
rsProjects("Check") = 1
rsProjects.Update
Next
rsProjects.Close
rsSourceProjects.Close
'The following command will delete all records in the Project table for the
project that was processed (tracked in strProjectNumber) that was not
processed (Check was left at 0)
CurrentDB.Execute "DELETE * FROM Projects WHERE ProjectNumber = """ &
strProjectNumber & """ AND Check = 0", dbFailOnError
End Sub
Again, there's more to it then just that, but short and sweet, that's what
I'm working with. I know there are different ways to do this procedure, but
this is what I am working with.
I believe the problem occurs when the Delete query is executed. The table,
for one reason or another, is not getting the updates within the loop. The
"Check" field is not being set to 1 for the records that exist in both
tblProjects and tblSource-ProjectList, and they get deleted. Is this a
problem with ADO interacting with the tables, or something more underlying?