Running very Slow

Posted on 2004-11-10
Last Modified: 2012-08-14
I have a very difficult solution(for me) need your help. I created the following code for my application. it run as wht i want but the problem is the process is very slow. it takes more than 10 seconds. Could you pls help me to make the code run much faster? here is my codes:

Dim rst As ADODB.Recordset
Dim query As String
Dim query1 As String
Dim x As Variant

x = Null
'Find Ident No
If Len(cbIdentNo) <> 0 Then
    x = "[IdentMaster].[IdentNo]=""" & cbIdentNo & """"
End If
'Find Machine Manufacturer ID
If Not IsNull(cbManufacturer) And cbManufacturer <> Empty Then
    If Not IsNull(x) Then
        x = x & " and " & "[MachineManufacturerID]=" & tbManufacturer.Text
        x = "[MachineManufacturerID]=" & tbManufacturer.Text  
    End If
End If

query = "SELECT IdentMaster.IdentNo, ArtMachineManufacturer.MachineManufacturerID , ArtMachineType.MachineTypeID" _
& " FROM IdentMaster, ArtMachineManufacturer" _
& " where " & x & " order by IdentMaster.IdentNo" _

Set rst = New ADODB.Recordset
rst.CursorType = adOpenStatic
rst.Open query, conn, , , adCmdText

conn.Execute "Delete * from Result"
'retrieve Result field.
Dim rst1 As ADODB.Recordset
Set rst1 = New ADODB.Recordset
rst1.Open "Select * from Result", conn, adOpenKeyset, adLockOptimistic, adCmdText
Dim intCount, i As Integer
    intCount = rst.RecordCount
    Do Until rst.EOF
        Dim rst2 As ADODB.Recordset
        Set rst2 = New ADODB.Recordset
        rst2.Open "select IdentNo from result where identno =""" & rst!IdentNo & """", conn, adOpenKeyset, adLockOptimistic, adCmdText
        If rst2.RecordCount = 0 Then
            With rst1
            !IdentNo = rst!IdentNo
            !MachineManufacturerID = rst!MachineManufacturerID
            !MachineTypeID = rst!MachineTypeID
            !GroupID = rst!GroupID
            !product = rst!product
            !Dimension = FindDimension(rst!IdentNo)
            !ToolApplicationID = rst!ToolApplicationID
            End With
        End If
End If

Function FindDimension(IdentNo) As String
Dim rst As New ADODB.Recordset
Dim connect_string As String
connect_string = Connection()

With rst
    .CursorLocation = adUseServer
    .CursorType = adOpenKeyset
    .Open "IdentMaster", connect_string, adOpenKeyset, adLockOptimistic, adCmdTableDirect
    .Index = "PrimaryKey"
End With
rst.Seek Array(IdentNo)
If Not rst.EOF Then
    Select Case rst!categorytype
        Case "KSN"
        FindDimension = rst!D & "x" & rst!B & "x" & rst!dSmall & " Z=" & rst!Z & " NL/DKN=" & rst!NL & "/" & rst!DKN
    End Select
End If

Set rst = Nothing
End Function

Thank you.
Question by:william43
    LVL 10

    Assisted Solution

    LVL 10

    Accepted Solution

    Also u can set indexes for the fields that are most often used in  a search criteria
    LVL 10

    Assisted Solution

    There seems to be a link between the tables IdentMaster and ArtMachineManufacturer missing in the "query" definition (used by rst).
    This can slow the query down significantly and may cause a Cartesian.
    Try opening rst2 as adOpenForwardOnly instead of adOpenKeyset (you'll need to replace "if rst2.recordcount=0 then" with "if rst2.EOF=False then") and closing it before repeating the loop.
    You've defined intCount as a variant, not an integer. Replace "Dim intCount, i As Integer" with "Dim intCount as Integer, i as Integer".
    Also, instead of using the movelast/recordcount/movefirst try incrementing intCount each loop. The movelast may take quite a bit of time. This would also allow you to change rst to adOpenForwardOnly, which is the fastest cursor type, as opposed to using adOpenStatic.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    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…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    737 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

    19 Experts available now in Live!

    Get 1:1 Help Now