L
Leif
I'm try to update some VBA code to optimize the update of records in a table.
This is being done from within Excel (VBA) to an Access database.
Originally the updates were being done using ADO and SQL statementsby using a
delete and an insert. For example.
strSQL = "Delete from COMP_RD where ID = '" & Product & "' AND PROP=
'" & Property & "' AND Date_= #" & newDate & "# AND [CASE]='" & MBOCase & "'"
adoConn.Execute strSQL
strSQL = "Insert into COMP_RD (ID, PROP, DATE_, VALUE_, [CASE],
X_UPDATED) Values (" _
& "'" & Product & "', '" & Property & "','" & newDate & "'," &
newValue & ",'" & MBOCase & "', '" & Now & "')"
adoConn.Execute strSQL
So I though if I use a recordset instead, and use a seek to locate the
record, and only do an update instead of a delete and insert, it should run
much faster. My reasoning is that compile and executing the SQL statement
for each update is avoided. My new code looks like the following:
Set adoRst = New ADODB.Recordset 'NEW
adoRst.Index = "DATAKEY" 'NEW
adoRst.Open "COMP_RD", adoConn, adOpenDynamic, adLockOptimistic,
adCmdTableDirect 'NEW
'Looping code and picking up Excel values here. Left out for clarity.
adoRst.Seek Array(sProduct, sProperty, dDate, MBOCase), adSeekFirstEQ
If adoRst.EOF Then adoRst.AddNew ' Record does not exist
adoRst!ID = sProduct
adoRst!Prop = sProperty
adoRst!date_ = dDate
adoRst!value_ = vValue
adoRst!case = MBOCase
adoRst!x_updated = Now
adoRst.Update
I'm very surprised that seek is no faster than executing SQL. In past
experience I found seek to be about 20x faster than find. Find will not work
here since it only accept 1 criteria, and I have 4.
The run times are basically the same. Any suggestions?
Thanks,
Leif
This is being done from within Excel (VBA) to an Access database.
Originally the updates were being done using ADO and SQL statementsby using a
delete and an insert. For example.
strSQL = "Delete from COMP_RD where ID = '" & Product & "' AND PROP=
'" & Property & "' AND Date_= #" & newDate & "# AND [CASE]='" & MBOCase & "'"
adoConn.Execute strSQL
strSQL = "Insert into COMP_RD (ID, PROP, DATE_, VALUE_, [CASE],
X_UPDATED) Values (" _
& "'" & Product & "', '" & Property & "','" & newDate & "'," &
newValue & ",'" & MBOCase & "', '" & Now & "')"
adoConn.Execute strSQL
So I though if I use a recordset instead, and use a seek to locate the
record, and only do an update instead of a delete and insert, it should run
much faster. My reasoning is that compile and executing the SQL statement
for each update is avoided. My new code looks like the following:
Set adoRst = New ADODB.Recordset 'NEW
adoRst.Index = "DATAKEY" 'NEW
adoRst.Open "COMP_RD", adoConn, adOpenDynamic, adLockOptimistic,
adCmdTableDirect 'NEW
'Looping code and picking up Excel values here. Left out for clarity.
adoRst.Seek Array(sProduct, sProperty, dDate, MBOCase), adSeekFirstEQ
If adoRst.EOF Then adoRst.AddNew ' Record does not exist
adoRst!ID = sProduct
adoRst!Prop = sProperty
adoRst!date_ = dDate
adoRst!value_ = vValue
adoRst!case = MBOCase
adoRst!x_updated = Now
adoRst.Update
I'm very surprised that seek is no faster than executing SQL. In past
experience I found seek to be about 20x faster than find. Find will not work
here since it only accept 1 criteria, and I have 4.
The run times are basically the same. Any suggestions?
Thanks,
Leif