How to connect to mysql using vba in excel 2007 - mysql

I have used the code bellow to connect to mysql and I have install mysql odbc connector version 5.3.
'Connect to database
Function CnnOpen(ByVal ServerName As String, ByVal DBName As String, _
ByVal TblName As String, ByVal User As String, ByVal PWD As String)
Dim CnnStr As String
Set Cnn = CreateObject("ADODB.Connection")
Cnn.CommandTimeout = 15
CnnStr = "DRIVER = {MySql ODBC 5.3 Driver}; SERVER =" & ServerName & _
"; Database =" & DBName & "; USER =" & User & "; PASSWORD =" _
& PWD & "; Option=3"
Cnn.ConnectionString = CnnStr
Cnn.Open
End Function
I have the following error:
Run-time error '-2147467259 (80004005)':
[Microsoft][ODBC Driver Manager]Data source name not found and
no default driver specified
Is there any incorrect connection string or any mistake in the code? Please, help.

Related

Excel VBA connection to external MySQL server (ODBC 8.0)

I wanted to connect an excel file with a MySQL server for a project, so I searched the internet for a solution. I found some, but all of it was old and I don't prefer 8–10-year-old methods and drivers.
All I got is [Microsoft][ODBC Driver Manager] Data source name not found, and no default driver specified
So, I setted up the following setup:
A MySQL 8.0 server (on a Ubuntu server in the oracle cloud) 64-bit, ODBC connector 8.0 Unicode 64-bit
Office 365 64 bit
I tried to make a connection with some of the methods, but they gave all the same error:
[Microsoft][ODBC Driver Manager] Data source name not found, and no default driver specified
Public Function OpenConnection() As ADODB.Connection
''This function requires the "Microsoft ActiveX Data Objects" Library (Choose v2.8 from references for compatibility across Office versions)
Dim source As String, location As String, user As String, password As String
location = My server IP
user = "ExcelTest"
password = "Pass"
database = "Test"
mysql_driver = "MySQL ODBC 8.0 Driver" ''Tried "MySQL ODBC 8.0 Unicode Driver" too
''Build the connection string
Dim connectionString As String
connectionString = "Driver={" & mysql_driver & "};Server=" & location & ";Database=" & database & ";UID=" & user & ";PWD=" & password
''Create and open a new connection
Set OpenConnection = New ADODB.Connection
OpenConnection.CursorLocation = adUseClient
Call OpenConnection.Open(connectionString)
End Function
Usually I found a pretty good tutorial or walkthrou but this time nothing up to date.
I yes. I am pretty sure All the things are 64 bit.
I do not know where to search Please help
Test the driver has installed correctly by using Windows ODBC Data Source Administrator first.
Option Explicit
Sub test()
Dim conn
Set conn = OpenConnection()
With conn
.CursorLocation = adUseClient
MsgBox "Connected to " & .DefaultDatabase, vbInformation
End With
End Sub
Public Function OpenConnection() As ADODB.Connection
Const location = ""
Const user = ""
Const password = ""
Const database = "test"
Const mysql_driver = "MySQL ODBC 8.0 Unicode Driver"
' Build the connection string
Dim s As String
s = "Driver={" & mysql_driver & "};Server=" & _
location & ";Database=" & _
database & ";UID=" & _
user & ";PWD=" & password
Debug.Print s
' Open connection
Set OpenConnection = New ADODB.Connection
OpenConnection.Open s
End Function

COMException including Excel during ADO connection

Ok, i don't know if it will be useful but i want to share. Modos and admin will delete if they want.
I'm developping a VB application (actually, i migrate one from Excel VBA) to interact with MySQL Database.
I use VS 2019 16.8.2, Windows 7 x64.
I got a cryptic issue during connection to the database (using ADO, reference used: MS ADO 2.6).
Here's the code:
Public Sub test_connect()
Dim server_name As String
Dim database_name As String
Dim login_database As String
Dim port_number As String
Dim pwd_database As String
server_name = "my_server"
database_name = "my_database"
login_database = "whoisyourdaddy"
pwd_database = "youare"
connexion_dlweb_doc = New ADODB.Connection With {
.ConnectionString = "DRIVER={MySQL ODBC 8.0 ANSI Driver};" _
& "SERVER=" & server_name & "; " _
& "PORT=" & port_number & "; " _
& "DATABASE=" & database_name & "; " _
& "UID=" & login_database & "; " _
& "PWD=" & pwd_database & "; " _
& "OPTION=3"
}
If connexion_dlweb_doc.State <> adStateOpen Then
connexion_dlweb_doc.Open()
End If
End Sub
So, i got an exception with the " connexion_dlweb_doc = New ADODB.Connection" command. It's where it becomes cryptic:
System.TypeInitializationException : 'The type initializer for 'MyAPP.mod_declaration_variables' threw an exception.'
COMException : Retrieving the COM class factory for component with CLSID {00020819-0000-0000-C000-000000000046} failed due to the following error: 80040154 Classe non enregistrée (0x80040154 (REGDB_E_CLASSNOTREG)).
mod_declaration_variables is a module where i declare global variables like connexion_dlweb_doc:
Public connexion_dlweb_doc As ADODB.Connection
So, i asked my best friend google about this cryptic message and the CLSID {00020819-0000-0000-C000-000000000046} refer to...Microsoft.Interop.Excel 14.0.0.0
I use the reference MS Excel Objet Library 14.0 in my project. How did this interact with the ADO connection part? What's the link? Why the code crash here and not before?
I don't know why but the solution was to delete (or comment) every variable declaration using Excel library, e.g:
Public xlApp_arbo_wiki As Excel.Application
Guess it would help noobs like me.

Connect MySQL(multiple instances) Database with Excel VBA

I have multiple instances of MySQL installed in my pc which are instance1 & instance2...
1st instance data directory is "C:\MYSQL2\Data", address is: localhost, port= 3306;
2nd instance data directory is "C:\ProgramData\MySQL\MySQL Server 5.7\Data", address is: localhost, port= 3308;
server_name = "localhost"
port = "3308"
database_name = "test2"
user_id = "root"
password = "xxxx"
Set conn = New ADODB.Connection
conn.Open "DRIVER={MySQL ODBC 3.51 Driver}" _
& ";Data Source=localhost" _
& ";SERVER=" & server_name _
& ";PORT=" & port _
& ";DATABASE=" & database_name _
& ";UID=" & user_id _
& ";PWD=" & password _
& ";OPTION=16427"
When i run the above code to connect with 2nd instance database it showing me error:
Run-time error '-2147467259(80004005)': [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
This code also showing error for my 1st instance which server name is localhost and port is=3306
This code running fine where only default instance is installed.
In the connection string if
Driver={MySQL ODBC 8.0 Unicode Driver}
then if you open ODBC data Sources (32-bit) not ODBC data Sources (64-bit)
see the same name
Then this code will work
Public Sub ask_sql1()
Dim SQL As String
SQL = "SELECT * FROM Users"
Dim Conn As ADODB.Connection
Set Conn = New ADODB.Connection
Conn.ConnectionString = "Driver={MySQL ODBC 8.0 Unicode Driver};Server=127.0.0.1;Database=test;UID=root;PWD=*********"
Conn.Open
Dim recordSet As ADODB.recordSet
Dim Field As ADODB.Field
Set recordSet = New ADODB.recordSet
recordSet.Open SQL, Conn, adOpenStatic, adLockReadOnly, adCmdText
If recordSet.State Then
For Each Field In recordSet.Fields
MsgBox Field.Name
Next Field
Set recordSet = Nothing
End If
Conn.Close
End Sub
also is you office 64 or 32
Can you add a screenshot of ODBC data Sources (32-bit)/Drivers ?

Excel VBA: ODBC Driver Manager]Data source name not found and no default driver specified

I couldnt connect my vba to the mysql. can you tell me where did I go wrong?
this method raise runtime error "[Microsoft][ODBC Driver Manager]Data source name not found and no default driver specified"
my OS is win 7 64 bit and I just installed 64bit ODBC Connector from https://downloads.mysql.com/archives/c-odbc/
but still the error occurs. I also use xampp with no password if that is also need specifying.
Sub SqlConnect()
Dim ReturnArray
Dim Conn As New ADODB.Connection
Dim mrs As New ADODB.Recordset
Dim DBPath As String
Dim sconnect As String
DBPath = ThisWorkbook.FullName
sconnect = "DRIVER={MySQL ODBC 5.2.2 Driver}; SERVER = localhost;" & _
"PORT=3306;" & _
"DATABASE= timeintimeoutdb;" & _
"UID=root;" & _
"PWD=;"
Conn.Open sconnect
sqlstring = "SELECT * FROM [students]"
mrs.Open sqlstring, Conn
ActiveSheet.Range("A2").CopyFromRecordset mrs
mrs.Close
Conn.Close
End Sub

VBA Connect to MYSQL

I am trying to setup a remote database connection to a server running MySQL version 5.0.96. When I execute I get the following error.
Any assistance is appreciated.
Here is my connection code:
Public Function opendb()
Dim oConn As ADODB.Connection
Dim Server_Name As String
Dim User_Name As String
Dim Password As String
Dim Database_Name As String
Server_Name = Sheets("_config").Range("B2").value
User_Name = Sheets("_config").Range("B3").value
Password = Sheets("_config").Range("B4").value
Database_Name = Sheets("_config").Range("B5").value
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 5.0.96 Driver};" & _
"SERVER=Server_Name;" & _
"DATABASE=Database_Name;" & _
"USER=User_Name;" & _
"PASSWORD=Password;" & _
"Option=3"
End Function
Dim oConn As ADODB.Connection
Dim Server_Name As String
Dim User_Name As String
Dim Password As String
Dim Database_Name As String
Server_Name = Sheets("_config").Range("B2").value
User_Name = Sheets("_config").Range("B3").value
Password = Sheets("_config").Range("B4").value
Database_Name = Sheets("_config").Range("B5").value
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 5.0.96 Driver};" & _
"SERVER=" & Server_Name & ";" & _
"DATABASE=" & Database_Name & ";" & _
"USER=" & User_Name & ";" & _
"PASSWORD=" & Password & ";" & _
"Option=3"
There might be other problems with your drivers but when I tested the code you posted on my end I got the same error - it's not going to read your variables unless you concat them
Edit - I'm using a 5.2a server - when I try to connect using 5.0.96 I get the same error again , but of course using the 5.2a driver in the connection string works fine. So you will probably need to download the 5.0.96 driver specifically. I did a little searching and couldnt find it, I'll keep looking around for a minute
MySQL server is running 5.0.96. DO NOT INSTALL MySQL Connector/ODBC 5.2.6. This driver is NOT backwards compatible from VBA. If you have it installed and you are having the same problem I did do the follow:
1) FIRST remove any User & System Data Sources you have added
2) Uninstall all ODBC 5.2 drivers
3) Installed Connector/ODBC 3.51.30. (You May Need to Reboot)
4) Make your Connection String > DRIVER={MySQL ODBC 3.51 Driver};
Hope this saves you lots of time.
You may write Set oConn = CreateObject("ADODB.Connection") 'NEW STATEMENT
instead of Set oConn = New ADODB.Connection