[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

Running very Slow

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.
  • 2
3 Solutions
Also u can set indexes for the fields that are most often used in  a search criteria
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.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now