patternMinor
DAO recordset effiency
Viewed 0 times
daoeffiencyrecordset
Problem
I have a form that consists of about 60 fields that all update when choosing a different customer from a drop down. I feel like my current way is not the best way as it takes about 30 seconds to update when changing customers. Currently I have:
Both of these commands basically happen 60 times (5 categories consisting of 12 fields(months)). Then even further down I close all the recordsets and set them to Nothing. All of this works just takes a little longer than I want. What is a more efficient to accomplish this?
Private Sub ClientSelection_AfterUpdate()
On Error GoTo errhandler
Dim dbTemp As DAO.Database
Dim rsTemp As DAO.Recordset
Dim strSQL As String
DoCmd.Hourglass True
sYear = Me.YearSelection.Value
Me.CustomerID = Me.CustomerSelection.Column(0)
CustomerID = Mid(Me.CustomerID.Value, 2, 36)
Set dbTemp = CurrentDb()
Set rsTemp = dbTemp.OpenRecordset("SELECT Field1 FROM Table where CustomerID = '" & CustomerID & "' and MonthYear = #01/01/" & sYear & "#")
Set rsTemp2 = dbTemp.OpenRecordset("SELECT Field1 FROM Table where CustomerID = '" & CustomerID & "' and MonthYear = #02/01/" & sYear & "#")
Me.JanCharges.Value = rsTemp("Charges")
Me.FebCharges.Value = rsTemp2("Charges")
rsTemp.Close
rsTemp2.Close
Set rsTemp = Nothing
Set rsTemp2 = Nothing
Set dbTemp = Nothing
Exit_ClientSelection_AfterUpdate:
Exit Sub
errhandler:
MsgBox "Error " & Err.Number & ": " & Err.Description & " in " & _
VBE.ActiveCodePane.CodeModule, vbOKOnly, "Error"
Resume Exit_ClientSelection_AfterUpdate
End SubBoth of these commands basically happen 60 times (5 categories consisting of 12 fields(months)). Then even further down I close all the recordsets and set them to Nothing. All of this works just takes a little longer than I want. What is a more efficient to accomplish this?
Solution
I'm going to leave the ms-access specifics to more proficient ms-access reviewers, as I'm not all that familiar with DAO and Access VBA.
If possible, switch to SQL Server ADODB.
DEADC0DE
Unless I'm missing something, you're inlining the SQL statements and
These can be hard to spot without an add-in though. MZ-Tools has a review source code feature that can find unused variables and procedures; Rubberduck has a code inspections feature that can find that too, and a number of other code issues (disclaimer: I own and maintain rubberduck, see tag info).
UX Issue?
You're turning the hourglass on, but you never turn it back off. I would expect this:
The error handler is problematic:
This cannot reliably be reporting the source of the error, unless there's only 1 module in your VBA project. Proof:
Module1
Module2
If
And it requires adding a reference to the VBIDE extensibility library in your VBAProject, which requires special security handling. Not ideal.
If possible, switch to SQL Server ADODB.
DEADC0DE
Unless I'm missing something, you're inlining the SQL statements and
Dim strSQL As String is an unused declaration.These can be hard to spot without an add-in though. MZ-Tools has a review source code feature that can find unused variables and procedures; Rubberduck has a code inspections feature that can find that too, and a number of other code issues (disclaimer: I own and maintain rubberduck, see tag info).
UX Issue?
DoCmd.Hourglass TrueYou're turning the hourglass on, but you never turn it back off. I would expect this:
Exit_ClientSelection_AfterUpdate:
DoCmd.Hourglass False
Exit SubThe error handler is problematic:
MsgBox "Error " & Err.Number & ": " & Err.Description & " in " & _
VBE.ActiveCodePane.CodeModule, vbOKOnly, "Error"This cannot reliably be reporting the source of the error, unless there's only 1 module in your VBA project. Proof:
Module1
Public Sub Boom()
MsgBox VBE.ActiveCodePane.CodeModule
End SubModule2
Public Sub DoSomething()
Module1.Boom
End SubIf
Module2 is the opened module when you run DoSomething, the message will say Module2, not Module1 where the "error" would have happened.And it requires adding a reference to the VBIDE extensibility library in your VBAProject, which requires special security handling. Not ideal.
Code Snippets
DoCmd.Hourglass TrueExit_ClientSelection_AfterUpdate:
DoCmd.Hourglass False
Exit SubMsgBox "Error " & Err.Number & ": " & Err.Description & " in " & _
VBE.ActiveCodePane.CodeModule, vbOKOnly, "Error"Public Sub Boom()
MsgBox VBE.ActiveCodePane.CodeModule
End SubPublic Sub DoSomething()
Module1.Boom
End SubContext
StackExchange Code Review Q#91193, answer score: 4
Revisions (0)
No revisions yet.