Solved

Efficient file access

Posted on 2014-02-20
5
254 Views
Last Modified: 2014-02-23
I have an app that is running slow over the clients network and I am looking for ways to make it faster.

I have one routine to summarize some records in a table based on a matching year and a payment date within a range of passed dates.  It works but I am certain it can be done more efficiently but looking for suggestion.

Here is the summary routine:

Public Sub getPaymentTotForThisCycle(passedTaxYear As Long, _
                                     passedBeginDate As Date, _
                                     passedEndDate As Date, _
                                     returnPrincipalPaid As Double, _
                                     returnPenaltyPaid As Double, _
                                     returnInterestPaid As Double, _
                                     returnLeinPaid As Double, _
                                     returnAttyFeesPaid As Double)
'
'- - - - - - - - - - - - - - - - - - - - - - - - - - G E N E R A T E D  E R R O R  C O D E ----------------
'                              If IsDeveloper Then
                               Else
                                 On Error GoTo getPaymentTotForThisCycle_Error
                               End If
'- - - - - - - - - - - - - - - - - - - - - - - - - - G E N E R A T E D  E R R O R  C O D E ----------------

returnPrincipalPaid = 0
returnPenaltyPaid = 0
returnInterestPaid = 0
returnLeinPaid = 0
returnAttyFeesPaid = 0
'
'
selectString = "Select * From qrytblzTmpWk_tblPayments_Year_Sub_Local "
'
Dim rsPayIn As ADODB.Recordset
Set rsPayIn = New ADODB.Recordset
rsPayIn.Open selectString, CurrentProject.Connection, adOpenKeyset, adLockOptimistic

If rsPayIn.EOF Then
    Exit Sub
Else
    If rsPayIn.RecordCount > 0 Then
        '
        rsPayIn.MoveFirst
        While Not rsPayIn.EOF
            '
            If rsPayIn!TaxRecYear = passedTaxYear And _
               (rsPayIn!PaymentDate >= passedBeginDate And rsPayIn!PaymentDate <= passedEndDate) Then
                '
                returnPrincipalPaid = returnPrincipalPaid + Nz(rsPayIn!TaxAmount, 0)
                returnPenaltyPaid = returnPenaltyPaid + Nz(rsPayIn!PenaltyAmount, 0)
                returnInterestPaid = returnInterestPaid + Nz(rsPayIn!InterestAmount, 0)
                returnLeinPaid = returnLeinPaid + Nz(rsPayIn!LienAmount, 0)
                returnAttyFeesPaid = returnAttyFeesPaid + Nz(rsPayIn!AttyFeesAmount, 0)
                '
            End If
            '
            rsPayIn.MoveNext
        Wend
        '
        ' reset for next cyear
        '
        rsPayIn.MoveFirst
        '
    End If
End If
'
rsPayIn.Close
Set rsPayIn = Nothing
'
'- - - - - - - - - - - - - - - - - - - - - - - - - - G E N E R A T E D  E R R O R  C O D E ----------------
                               On Error GoTo 0
                               Exit Sub
getPaymentTotForThisCycle_Error:
                               sysErrorHandler Err.Number, Err.Description, "getPaymentTotForThisCycle", "modBalanceDueAndPayStuff", "Module"
'- - - - - - - - - - - - - - - - - - - - - - - - - - G E N E R A T E D  E R R O R  C O D E ----------------



End Sub

Open in new window


Any suggestions?

Another example:  Which is quicker, or is there an even more efficient way to lookup and ID for a record.  The two examples work on differen tables but are for the same type of lookup

Example 1 using a Dlookup.  I know these functions are slow but it seems like it would have to be faster than the routine that opens the file then looks for the ID.
Public Function getTaxHeaderIDFromBRT(passedBRT As Long) As Long
'
getTaxHeaderIDFromBRT = 0
On Error Resume Next
getTaxHeaderIDFromBRT = DLookup("[ID]", "tblTaxHeader", " [BRT] = " & passedBRT)

End Function

Open in new window


Example 2 opening and closing a table:
Public Function GetPropertyIDFromBRT(passedBRT As Long) As Long
'
'- - - - - - - - - - - - - - - - - - - - - - - - - - G E N E R A T E D  E R R O R  C O D E ----------------
'                               If IsDeveloper Then
                               Else
                                 On Error GoTo GetPropertyIDFromBRT_Error
                               End If
'- - - - - - - - - - - - - - - - - - - - - - - - - - G E N E R A T E D  E R R O R  C O D E ----------------

GetPropertyIDFromBRT = 0
'
selectString = "Select [ID] From tblProperty Where [BRT] = " & passedBRT

Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
rs.Open selectString, CurrentProject.Connection, adOpenKeyset, adLockOptimistic

If rs.EOF Then
Else
    rs.MoveLast
    rs.MoveFirst
    If rs.RecordCount > 0 Then
        '
        GetPropertyIDFromBRT = Nz(rs!ID, 0)
        '
    End If
End If
'
rs.Close
Set rs = Nothing
'

'- - - - - - - - - - - - - - - - - - - - - - - - - - G E N E R A T E D  E R R O R  C O D E ----------------
                               On Error GoTo 0
                               Exit Function
GetPropertyIDFromBRT_Error:
                               sysErrorHandler Err.Number, Err.Description, "GetPropertyIDFromBRT", "FileHandlingAndLookups", "Module"
'- - - - - - - - - - - - - - - - - - - - - - - - - - G E N E R A T E D  E R R O R  C O D E ----------------


End Function

Open in new window

0
Comment
Question by:mlcktmguy
5 Comments
 
LVL 15

Accepted Solution

by:
JimFive earned 300 total points
Comment Utility
For the first part, write a query like:
SELECT Sum(TaxAmount) as PrincipalPaid, Sum(PenaltyAmount) as PenaltyPaid,
             Sum(InterestAmount) as InterestPaid, Sum(LienAmount) as LienPaid,
             Sum(AttyFeesAmount) as AttyFeesPaid
FROM qrytblzTmpWk_tblPayments_Year_Sub_Local
WHERE TaxYear = [paramPassedTaxYear]
AND PaymentDate >= [parampassedBeginDate]
 And PaymentDate <= [parampassedEndDate]

Open in new window


Then in your Code you can use:
' Note this is untested and may need minor changes
Set qd = CurrentDB.QueryDef("QueryName")
qd.Parameters("paramPasssedTaxYear") = passedTaxYear;
qd.Parameters("paramPassedBeginDate") = passedBeginDate;
qd.Parameters("paramPassedEndDate") = passedEndDate;

Set rs = qd.open()

Open in new window

And all of your data is in one row.
0
 
LVL 15

Expert Comment

by:JimFive
Comment Utility
I meant to add:  You should never do SELECT * and  no WHERE clause in VBA, you will get the entire table and that is going to be slow.

For the second part of your question:
DLookup is slow, really slow.  The second is probably faster.  In addition a parameterized query that always returns 1 row with the data you need is almost always going to be faster.
0
 
LVL 34

Assisted Solution

by:PatHartman
PatHartman earned 100 total points
Comment Utility
Is the BE SQL Server?  I believe that ADO might have been faster for accessing non-Jet databases but DAO is the native engine for ACE (.accdb) and so if milliseconds count, you might want to try DAO.
0
 
LVL 26

Assisted Solution

by:Nick67
Nick67 earned 100 total points
Comment Utility
You are doing the summing in code in a loop.
That's ok up until you have a performance issue.

You really should transform
           If rsPayIn!TaxRecYear = passedTaxYear And _
               (rsPayIn!PaymentDate >= passedBeginDate And rsPayIn!PaymentDate <= passedEndDate) Then


into the WHERE parameter of the SQL.

Moreover, if you can craft a SELECT query that will ONLY select applicable records, you can dispense with looping altogether, and do the summing right within the query using the 'totals' function of the query editor.  The resulting saved query will be optimized by the query engine and run much more efficiently.

Rule of thumb: when performance matters use, set logic instead of looping logic.
Intellectually, a loop is easier to craft, test and perfect than the logic to get the correct set of records -- but the set logic performs much, much better.
0
 
LVL 1

Author Closing Comment

by:mlcktmguy
Comment Utility
Thanks to all for your input and suggestions.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now