(Solved) – How to INSERT/SELECT data from Excel/VBA to MySQL?

(solved)-–-how-to-insert/select-data-from-excel/vba-to-mysql?

Hello guys this is my first post so I hope that you can help me with this doubt. I’m trying to extract data from MySql to Excel and I have no idea why it not working. I saw a lot of tutorials but I get the same error all the time.

Here is my code:

Sub consultdate()

Dim conn As New ADODB.Connection
Dim server_name As String
Dim database_name As String
Dim user_id As String
Dim password As String

Dim i As Long
Dim sqlstr As String
Dim table1 As String, table2 As String
Dim field1 As String, field2 As String
Dim rs As ADODB.Recordset
Dim vtype As Variant

server_name = "localhost"
database_name = "controle_fin"
user_id = "root"
password = "userpassword"

Set conn = New ADODB.Connection
conn.Open "DRIVER={MySQL ODBC 3.51 Driver}" _
& ";SERVER=" & server_name _
& ";DATABASE=" & database_name _
& ";UID=" & user_id _
& ";PWD=" & password _
& ";OPTION=16427"

table1 = "classe"

GoTo skipextract
Set rs = New ADODB.Recordset
sqlstr = "SELECT * FROM " & table1
rs.Open sqlstr, conn, adOpenStatic
Planilha1.Range("A1").CopyFromRecordset rs
skipextract:

On Error Resume Next
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
On Error GoTo 0
End Sub

Error that I got: “ODBC Error. Data Source Name Not Found and No Default Driver Specified”

What am I doing wrong? Please help me.

Thanks for attention.

Leave a Reply

Your email address will not be published. Required fields are marked *