Solved

Efficient file access

Posted on 2014-02-20
5
262 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Import MySQL data into MS Access using VB.Net interface 5 30
Dot leaders between two labels in report 10 33
UK Date format in MS Access 4 22
Run Stored Procedure uisng ADO 5 21
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
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…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

831 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