Home > front end >  Excel VBA connect to MySql server
Excel VBA connect to MySql server

Time:12-04

I set up a test database at db4free.net and uploaded a copy of the northwind training database to it, to see if I could pull some information to an excel workbook and keep getting the generic unspecified/automation error.

I included "Microsoft ActiveX Data Objects 2.8 library" in the references and even tried 6.1 for good measure.

Before anyone freaks out at me including the username and password; the only thing that exists on this test database is a training dataset. I have ZERO personal information stored there.

Here is my code:

Sub sqlTest()
    'Declare some strings to hold the connection string and the SQL statement
    Dim cnStr As String
    Dim sqlStr As String
    
    'Define a connection and a recordset to hold extracted information
    Dim oConn As ADODB.Connection
    Dim rcSet As New ADODB.Recordset
    Set oConn = New ADODB.Connection
    Set rcSet = CreateObject("ADODB.Recordset")
    
    'connection string to connect to db4free.net
    cnStr = "Driver={MySQL ODBC 8.0 Unicode Driver};SERVER=85.10.205.173;DATABASE=resumedemo;PORT=3306;UID=jwaycaster;PWD=resumedemo123;"
    
    'Test SQL query
    sqlStr = "SELECT * FROM `Employees`"
    
    'This is where it crashes
    oConn.Open cnStr
    
    oConn.CommandTimeout = 900
    
    rcSet.Open sqlStr, oConn
    
    Sheets(1).Range("A1").CopyFromRecordset rcSet
    
    rcSet.Close
    
    oConn.Close
End Sub

I've search around several related topics and can't seem to find the answer. Hopefully I'm missing something simple.

EDIT for posterity: After reading responses I realized that the drivers are not installed on this computer (I'm visiting relatives and can't believe I forgot to check that). After installing the drivers and updating my references my code executes just fine, but I would suggest using CDP1802's instead.

CodePudding user response:

It sometimes can be useful to handle the errors yourself. Add references to

  1. Microsoft ActiveX Data Objects 6.1 Library
  2. Microsoft ActiveX Data Objects RecordSet 6.0 Library
Option Explicit

Sub sqlTest()

    ' credentials
    Const SERVER = "85.10.205.173"
    Const DB = "resumedemo"
    Const UID = "jwaycaster"
    Const PWD = "resumedemo123"
      
    'Define a connection and a recordset to hold extracted information
    Dim oConn As ADODB.Connection, rcSet As ADODB.Recordset
    Dim cnStr As String, n As Long, msg As String, e
    
    'connection string to connect to db4free.net
    cnStr = "Driver={MySQL ODBC 8.0 Unicode Driver};SERVER=" & SERVER & _
            ";PORT=3306;DATABASE=" & DB & _
            ";UID=" & UID & ";PWD=" & PWD & ";"
    
    'Test SQL query
    Const SQL = "SELECT * FROM `Employees`"
    
    ' connect
    Set oConn = New ADODB.Connection
    'oConn.CommandTimeout = 900
    
    On Error Resume Next
    oConn.Open cnStr
    If oConn.Errors.Count > 0 Then
        For Each e In oConn.Errors
            msg = msg & vbLf & e.Description
        Next
        MsgBox msg, vbExclamation, "ERROR - Connection Failed"
        Exit Sub
    Else
        MsgBox "Connected to database " & oConn.DefaultDatabase, vbInformation, "Success"
    End If
    
    ' run query
    Set rcSet = oConn.Execute(SQL, n)
    If oConn.Errors.Count > 0 Then
        msg = ""
        For Each e In oConn.Errors
            msg = msg & vbLf & e.Description
        Next
        MsgBox msg, vbExclamation, "ERROR - Execute Failed"
    Else
        Sheets(1).Range("A1").CopyFromRecordset rcSet
        MsgBox SQL & " returned " & n & " records", vbInformation
    End If
    On Error GoTo 0
    
    oConn.Close
    
End Sub
  • Related