Results 1 to 3 of 3

ADODB Connections are veeeery slow

Threaded View

  1. #1
    Registered User
    Join Date
    10-21-2013
    Location
    Norway
    MS-Off Ver
    Excel 2010
    Posts
    21

    ADODB Connections are veeeery slow

    I wonder if it anyone know any good tips & tricks to improve performance on ADODB Connections.
    Sometimes it goes veeery slow to open the connection and running the query takes under a second and other times opening the connection goes fast but running the query takes ages.

    I have noticed that its usually the first time the query is runned its taking the longest. If the same query is beeing run again it goes ALOT faster.
    I have tried running a simpler query to the DB when opening the worksheet to improve the time it takes to open the connection, but it doesnt seem to help that much for some reason.

    The SQL statements are also pretty huge, around 2000 characters (no spaces)
    I read somewhere else that adding:
    rec1.CursorLocation = adUseClient
    rec1.CursorType = adOpenStatic
    might help. But didn't see much difference.

    Sub GetData(dato_from As String, dato_to As String, x As String)
    On Error GoTo ErrorHandler
    Dim conn As ADODB.Connection
    Dim rec1 As ADODB.Recordset
    Dim SQLquery As String
    
    Set conn = New ADODB.Connection
    
    If (x = "input_a") Then   
        conn.Open "DSN=<server1>;UID=<userID>;SRVR=<server1>;DB=<DB>;PWD=<PWD>"
        SQLquery = "Huge SQL query"
    ElseIf (x = "input_a_b") Then
        conn.Open "DSN=<server2>;UID=<userID>;SRVR=<server2>;DB=<DB>;PWD=<PWD>"
        SQLquery = "Huge SQL query"
    ElseIf (x = "input_c") Then   
        conn.Open "DSN=<server2>;UID=<userID>;SRVR=<server2>;DB=<DB>;PWD=<PWD>"
        SQLquery = "Huge SQL query"
    ElseIf (x = "input_e") Then
        conn.Open "DSN=<server2>;UID=<userID>;SRVR=<server2>;DB=<DB>;PWD=<PWD>"
        SQLquery = "Huge SQL query"
    End If
        Set rec1 = New ADODB.Recordset
        rec1.CursorLocation = adUseClient
        rec1.CursorType = adOpenStatic
        rec1.Open SQLquery, conn
        count = rec1.RecordCount
        
    If (count > 0) Then
        Sheet1.Shapes("TextBox 9").Line.ForeColor.ObjectThemeColor = msoThemeColorAccent3
        Sheet1.Shapes("TextBox 9").Line.ForeColor.Brightness = 0
        Sheet1.Shapes("TextBox 26").Line.ForeColor.ObjectThemeColor = msoThemeColorAccent3
        Sheet1.Shapes("TextBox 26").Line.ForeColor.Brightness = 0
        Sheet1.Shapes("TextBox 38").Line.ForeColor.ObjectThemeColor = msoThemeColorAccent3
        Sheet1.Shapes("TextBox 38").Line.ForeColor.Brightness = 0
    
        If (count = 1) Then
            Sheet1.Shapes("TextBox 26").TextFrame.Characters.Text = "Found" & count & " row"
        Else
            Sheet1.Shapes("TextBox 26").TextFrame.Characters.Text = "Found" & count & " rows"
        End If
    
        With Sheet1.QueryTables.Add(Connection:=rec1, Destination:=Sheet1.Range("A13"))
            .Name = "data"
            .FieldNames = True
            .Refresh BackgroundQuery:=False
        End With
    Else
        Sheet1.Shapes("TextBox 9").Line.ForeColor.RGB = RGB(192, 0, 0)
        Sheet1.Shapes("TextBox 26").Line.ForeColor.RGB = RGB(192, 0, 0)
        Sheet1.Shapes("TextBox 38").Line.ForeColor.RGB = RGB(192, 0, 0)
        Sheet1.Shapes("TextBox 26").TextFrame.Characters.Text = "No data found"
        Exit Sub
    End If
    
    ErrorExit:
        Set conn = Nothing
        Set rec1 = Nothing
        Exit Sub
    ErrorHandler:
        MsgBox Err.Description, vbCritical
       Resume ErrorExit
       cnt.Close
        If CBool(cnt.State And adStateOpen) Then
        Set conn = Nothing
        Set rec1 = Nothing
        End If
    End Sub
    As you can see from the code its 2 different servers that the querys can go to. So one question is that if I run a query on Server 1 does the connection to server 2 get terminated?
    Is there a way to keep both connections open?

    Here are the methods I run when the sheet is opened. Theese query goes very fast to open connection and run. So question again is if this actually should help? Are the connections beeing kept alive?

    Sub UpdateDB()
    On Error GoTo ErrorHandler
    Dim conn As ADODB.Connection
    Dim rec1 As ADODB.Recordset
    Dim thisSql As String
    
    Set conn = New ADODB.Connection
    conn.Open "DSN=<server2>;UID=<userID>;SRVR=<server2>;DB=<DB>;PWD=<Password>"
    thisSql = "select top 1 ...."
    
    Set rec1 = New ADODB.Recordset
    rec1.Open thisSql, conn
    
    With Sheet1.QueryTables.Add(Connection:=rec1, Destination:=Sheet1.Range("A1"))
        .Name = "data"
        .FieldNames = True
        .Refresh BackgroundQuery:=False
    End With
    Range("A2").HorizontalAlignment = xlCenter
    ErrorExit:
        Set conn = Nothing
        Set rec1 = Nothing
        Exit Sub
    ErrorHandler:
        MsgBox Err.Description, vbCritical
        Resume ErrorExit
        cnt.Close
        If CBool(cnt.State And adStateOpen) Then
            Set conn = Nothing
            Set rec1 = Nothing
        End If
    End Sub
    Sub UpdateDBA()
    On Error GoTo ErrorHandler
    Dim conn As ADODB.Connection
    Dim rec1 As ADODB.Recordset
    Dim thisSql As String
    
    Set conn = New ADODB.Connection
    conn.Open "DSN=<server1>;UID=<userID>;SRVR=<server1>;DB=<DB>;PWD=<Password>"
    thisSql = "select top 1..."
    
    Set rec1 = New ADODB.Recordset
    rec1.Open thisSql, conn
    
    ErrorExit:
        Set conn = Nothing
        Set rec1 = Nothing
        Exit Sub
    ErrorHandler:
        MsgBox Err.Description, vbCritical
        Resume ErrorExit
        cnt.Close
        If CBool(cnt.State And adStateOpen) Then
            Set conn = Nothing
            Set rec1 = Nothing
        End If
    End Sub
    Last edited by stibay; 01-08-2014 at 05:10 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Macro is running real slow and makes navigating the worksheet really slow after execution.
    By MichWolverines in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-03-2013, 04:29 PM
  2. [SOLVED] slow computer (slow clipboard) breaks my macro
    By twilsonco in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-05-2013, 09:16 PM
  3. Replies: 2
    Last Post: 05-24-2012, 08:43 AM
  4. push:ADODB Connections
    By Slyone2 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-27-2010, 01:11 PM
  5. [SOLVED] ADODB Connections
    By MChrist in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-07-2005, 04:45 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1