Solved

Speed up Data Access

Posted on 2002-07-18
11
189 Views
Last Modified: 2010-05-02
The code below populates treeview with data from an Access Db.

It is quite slow over a network. Is there a way of speeding up this process?

[Code]
If gcDB.CreateRS(oRsContacts, strSQLContacts) Then
    If oRsContacts.RecordCount Then
        Do While Not oRsContacts.EOF
            Set oTmpNode = TView.Nodes.add("Contacts", tvwChild, "Contact_" & oRsContacts.Fields("ConNo"), oRsContacts.Fields("ConCompany"))
            TView.Nodes("Contact_" & oRsContacts.Fields("ConNo")).Tag = "Contact"
            If gcDB.CreateRS(oRsProjectsData, strSQLContactData) Then
                Do While Not oRsProjectsData.EOF
                    Set oTmpNode = TView.Nodes.add("Contact_" & oRsContacts.Fields("ConNo"), tvwChild, "ProjectData_" & oRsProjectsData.Fields("ID"), oRsProjectsData.Fields("ProjectName"))
                    TView.Nodes("ProjectData_" & oRsProjectsData.Fields("ID")).Tag = "Project"
                    oRsProjectsData.MoveNext
                Loop
            End If
            oRsContacts.MoveNext
        Loop
    End If
End If
0
Comment
Question by:mutrus
  • 6
  • 3
  • 2
11 Comments
 
LVL 4

Expert Comment

by:gencross
ID: 7163272
It looks like you are setting a node object then not using it...

You can also use 'With...End With'.

Here is your code with the above changes...

If gcDB.CreateRS(oRsContacts, strSQLContacts) Then
    With oRsContacts
        If .RecordCount Then
            Do While Not oRsContacts.EOF
                Set oTmpNode = TView.Nodes.Add("Contacts", tvwChild, "Contact_" & .Fields("ConNo"), .Fields("ConCompany"))
                oTmpNode.Tag = "Contact"
                If gcDB.CreateRS(oRsProjectsData, strSQLContactData) Then
                    Do While Not oRsProjectsData.EOF
                        Set oTmpNode = TView.Nodes.Add("Contact_" & .Fields("ConNo"), tvwChild, "ProjectData_" & oRsProjectsData.Fields("ID"), oRsProjectsData.Fields("ProjectName"))
                        oTmpNode.Tag = "Project"
                        oRsProjectsData.MoveNext
                    Loop
                End If
                oRsContacts.MoveNext
            Loop
        End If
    End With
End If

NOTE: Be sure to open the recordsets forward only, read only.
0
 
LVL 4

Expert Comment

by:gencross
ID: 7163283
I forgot to take out a couple of references to oRsContacts...

Do While Not oRsContacts.EOF

Should be: Do While Not .EOF

oRsContacts.MoveNext

Should be: .MoveNext

Remove any other references to oRsContacts that I missed.


0
 

Author Comment

by:mutrus
ID: 7163441
>>NOTE: Be sure to open the recordsets forward only, read only

This is the class code for CreateRS. Is this what you mean by your statement?

    Dim oCmd As ADODB.Command

    Set goRs = New ADODB.Recordset
    Set oCmd = New ADODB.Command
    Set oCmd.ActiveConnection = moCon
    oCmd.CommandText = SQL
    With goRs
        .CursorLocation = ADODB.adUseClient
        .StayInSync = True
        .CacheSize = 1
        .Open oCmd, , ADODB.adOpenDynamic, ADODB.adLockBatchOptimistic
    End With
    CreateRS = True
0
 

Author Comment

by:mutrus
ID: 7163483
Tried your changes - not much of a difference

improvement of only 1 second with 30 records in outer loop and two records each in inner loop. Total time to load 7 seconds



0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7163497
What is the variable "SQL" in you code?

If all it is, is a SQL statement then try this:
Set goRs = New ADODB.Recordset
With goRs
   .Source = SQL
   Set ActiveConnection = moCon
   .CursorType = adOpenForwardOnly
   .LockType = adLockReadOnly
   .Open Options:=adCmdText
End With

Anthony
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:mutrus
ID: 7163582
acperkins - thanks for suggestion but this would involve changing a huge amount of code due to me using a ADO class wrapper. If I still cannot solve the speed problem using some other means (I tend to think its the network) then I'll come back to your suggestion
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 100 total points
ID: 7163622
Ok. But can you not even change this one line:
.Open oCmd, ,ADODB.adOpenDynamic, ADODB.adLockBatchOptimistic

To this:
.Open oCmd, , adOpenForwardOnly, adLockReadOnly

Anthony
0
 

Author Comment

by:mutrus
ID: 7163698
Unbelieveable!!

Thanks
0
 

Author Comment

by:mutrus
ID: 7163711
acperkins

Can you expalain in laymans terms if possible the difference between my original code and your solutions

Thanks
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7163739
First of all I do not believe I deserve the credit, if you notice gencross had previously suggested it.

But to answer your question a cursor type of adOpenDynamic is the most expensive type of cursor available.  It supports bookmarking (this allows you to go back and forward in the cursor) and updates. Not all providers support it, but they will degrade to the next option.

Here is an article from MSDN that goes into more length:
Understanding ADO's Default Cursor Type
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnima01/html/ima0501.asp

Also, depending on your provider you may see improvements if you change:
.CursorLocation = adUseClient
to
.CursorLocation = adUseServer

Anthony
0
 

Author Comment

by:mutrus
ID: 7170551
acperkins - thanks for the article. and yes you are right, I see gencross's note now


gencross - have posted some points for you - thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

920 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

12 Experts available now in Live!

Get 1:1 Help Now