?
Solved

Efficient file access

Posted on 2014-02-20
5
Medium Priority
?
312 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 15

Accepted Solution

by:
JimFive earned 1200 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 38

Assisted Solution

by:PatHartman
PatHartman earned 400 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 400 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses

743 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