Solved

Efficient file access

Posted on 2014-02-20
5
263 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
ID: 39874080
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
ID: 39874132
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 36

Assisted Solution

by:PatHartman
PatHartman earned 100 total points
ID: 39874490
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
ID: 39875080
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
ID: 39881266
Thanks to all for your input and suggestions.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

733 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