HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

DAO recordset effiency

Submitted by: @import:stackexchange-codereview··
0
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:

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 Sub


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?

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 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 True


You're turning the hourglass on, but you never turn it back off. I would expect this:

Exit_ClientSelection_AfterUpdate:
    DoCmd.Hourglass False        
    Exit Sub


The 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 Sub


Module2

Public Sub DoSomething()
    Module1.Boom
End Sub


If 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 True
Exit_ClientSelection_AfterUpdate:
    DoCmd.Hourglass False        
    Exit Sub
MsgBox "Error " & Err.Number & ": " & Err.Description & " in " & _
    VBE.ActiveCodePane.CodeModule, vbOKOnly, "Error"
Public Sub Boom()
    MsgBox VBE.ActiveCodePane.CodeModule
End Sub
Public Sub DoSomething()
    Module1.Boom
End Sub

Context

StackExchange Code Review Q#91193, answer score: 4

Revisions (0)

No revisions yet.