ADODB connection - Is this the way to do it?

Jaymond Flurrie

Well-known Member
Joined
Sep 22, 2008
Messages
919
Office Version
  1. 365
Platform
  1. Windows
I have established a connection like this
Code:
Sub InitializeMySQLConn()
    Set oConn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    strConn = ConnString()
End Sub


Function ConnString() As String
    ConnString = "DRIVER={MySQL ODBC 5.1 Driver};" & _
    "SERVER=myserver;" & _
    "DATABASE=mydatabase;" & _
    "USER=me;" & _
    "PASSWORD=mypassword;" & _
    "Option=3"
End Function




Then I use it like this


Code:
    oConn.Open strConn
    
        strsql = "SELECT * FROM mytable;"
        oConn.Execute strsql
        rs.Open strsql, oConn, adOpenDynamic, adLockOptimistic
        Set myrange = Range("A1")
        myrange.CopyFromRecordset rs


    rs.Close
    oConn.Close


this works, but my question is that am I doing the whole download operation here twice? Do I have to do first
Code:
oConn.Execute strsql
and then
Code:
rs.Open strsql, oConn, adOpenDynamic, adLockOptimistic
or how should I do it?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
If you're just doing one query, maybe just use a recordset. No connection object.

I'll have a quick look for some links that might help.
 
Upvote 0
If you're just doing one query, maybe just use a recordset. No connection object.

I'll have a quick look for some links that might help.


Thanks for the links and for the idea of not using connection object, I didn't even think that's possible.
 
Upvote 0
something like
Code:
Sub ADO_to_newWbk()

  'late bound

  Dim i As Long
  Dim strConn As String
  Dim strSQL As String
  Dim objRS As Object
  Dim wbkNew As Workbook

  'Excel 2003
  strConn = Join$(Array("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=", _
      your_workbook.FullName, ";Extended Properties=""Excel 8.0;"""), vbNullString)

  strSQL = "SELECT fields FROM your_data"

  Set wbkNew = Workbooks.Add(template:=xlWBATWorksheet)

  Set objRS = CreateObject("ADODB.Recordset")
  With objRS

    .Open strSQL, strConn

    wbkNew.Worksheets(1).Cells(2, 1).CopyFromRecordset objRS

    For i = 0 To .fields.Count - 1
      wbkNew.Worksheets(1).Cells(1, i + 1).Value = .fields(i).Name
    Next i

    .Close
  End With

  Set objRS = Nothing
  Set wbkNew = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,266
Members
448,558
Latest member
aivin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top