walduxas
08-18-2009, 12:53 AM
Hi guys,
I'm using update query to update values in table. However it takes about an hour to finish updating. I found that there is possibility to use this code to increase the speed of the query:
Sub PMUpdate()
Dim dbs As DAO.Database
Dim rsWk as DAO.Recordset
Dim strSQL,i As String
Dim wksp As DAO.Workspace
Set dbs = CurrentDB()
Set wksp = DBEngine.Workspaces(0) ' set up a transaction buffer
wksp.BeginTrans ' all record set changes are buffered after this
On Error GoTo roll0
strSQL = "SELECT * from tblWk"
Set rsWk = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
i = rsWK(0) ' Explict Reference to the only field in the table (first)
strSQL = "UPDATE tblPMSales SET tblPMSales.i = tblPMInput.fldPMImportSalse "
strSQL = strSQL & "WHERE tblPMSales.fldContract = tblPMInput.fldContract"
DoCmd.RunSQL strSQL
wksp.CommitTrans
GoTo finish_it
roll0:
If Err.Number = 3022 Then
On Error GoTo roll0
Resume check_next
End If
MsgBox Err.Number & " " & Err.Description
wksp.Rollback ' cancel everything if unexpected error
finish_it:
Set rsWK = Nothing
Set dbs = Nothing
End Sub
However, I don't know where should I write this code. Should I write this into SQL statement, or as some VBA code? Currently my update query looks like this (See attachement).
I'm using update query to update values in table. However it takes about an hour to finish updating. I found that there is possibility to use this code to increase the speed of the query:
Sub PMUpdate()
Dim dbs As DAO.Database
Dim rsWk as DAO.Recordset
Dim strSQL,i As String
Dim wksp As DAO.Workspace
Set dbs = CurrentDB()
Set wksp = DBEngine.Workspaces(0) ' set up a transaction buffer
wksp.BeginTrans ' all record set changes are buffered after this
On Error GoTo roll0
strSQL = "SELECT * from tblWk"
Set rsWk = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
i = rsWK(0) ' Explict Reference to the only field in the table (first)
strSQL = "UPDATE tblPMSales SET tblPMSales.i = tblPMInput.fldPMImportSalse "
strSQL = strSQL & "WHERE tblPMSales.fldContract = tblPMInput.fldContract"
DoCmd.RunSQL strSQL
wksp.CommitTrans
GoTo finish_it
roll0:
If Err.Number = 3022 Then
On Error GoTo roll0
Resume check_next
End If
MsgBox Err.Number & " " & Err.Description
wksp.Rollback ' cancel everything if unexpected error
finish_it:
Set rsWK = Nothing
Set dbs = Nothing
End Sub
However, I don't know where should I write this code. Should I write this into SQL statement, or as some VBA code? Currently my update query looks like this (See attachement).