Link to home
Start Free TrialLog in
Avatar of mutrus
mutrus

asked on

Speed up Data Access

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
Avatar of gencross
gencross

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.
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.


Avatar of mutrus

ASKER

>>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
Avatar of mutrus

ASKER

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



Avatar of Anthony Perkins
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
Avatar of mutrus

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mutrus

ASKER

Unbelieveable!!

Thanks
Avatar of mutrus

ASKER

acperkins

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

Thanks
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
Avatar of mutrus

ASKER

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


gencross - have posted some points for you - thanks