Excel-Vba Connect Mysql localhost failed - mysql

Trying to implement Excel VBA: writing to mysql database.
Following code runs into a run-time error [MIcrosoft][ODBC Driver Manager] Data source name not found and no default driver specified
Dim cn As Object
Sub Connect()
Dim strCon as string
Set cn = CreateObject("ADODB.connection")
strCon = "DRIVER={MySQL ODBC 3.51 Driver};" & _
"SERVER=localhost;" & _
"DATABASE=dbname;" & _
"USER=root;" & _
"PASSWORD=mypass;" & _
"Port=3306;" & _
"Option=3"
cn.Open strCon
cn.Close
End Sub
checked connection parameters with the following php code
$mysqli = new mysqli('localhost','root','mypass','dbname');
and all works just fine.
I do have Microsoft ActiveX Data Objects 2.8 Library ticked in my VBA Project References.
Any help is welcomed.

For whatever it's worth at this point (since I just ran into it and it took me a day to figure it out):
You may have MySQL (or other Database/Application) in either 32 bit or 64 bit form.
You may have Excel (or other application) in either 32 or 64 bit form.
You may have the ODBC "connection" between the two in either 32 or 64 bit form.
Typically, the ODBC driver will be installed when you install the DB product, so it will be the same as the database.
Unfortunately, that's not important.
What IS important is that the ODBC driver match the application (e.g., Excel) properly.
If you have 32 bit Excel, and install 64 bit MySQL, that's fine, but Excel won't be able to connect to MySQL (except through the MySQL Connector, but that's not the goal here).
You won't be able to write an Excel app to read, via ODBC, from the database.
The solution is simple. Download the MySQL ODBC driver for 32 bit (same place as MySQL), and install it.
(It will tell you 'already installed' - because it will see the 64 bit version - and ask if you want to uninstall. Say Yes.)
Now, 32bit Excel can talk to 32bit ODBC driver, which can talk to 64bit MySQL, to exchange data.
Note: Far as I know, you can only install one type of ODBC driver (per DB App). So, if you are using the 64bit version, perhaps for some other app, you'll have to uninstall it (which breaks access to that App) to install the 32bit version so you can use Excel.

I have a 64 Win machine with 32bit Excel. I experimented with different MySQl ODBC drivers (5.1, 5.2, 5.3). The 64bit drivers did not work fro me, but the 32bit odbc driver did work.
Somewhere in the Microsoft knowledgebase it mentioned that Excel does not work with the 64bit drivers.
I recommend using a more recent one than the one you mentioned in your post:
http://dev.mysql.com/downloads/connector/odbc/5.2.html
This vba code worked for me
Set oConn = New ADODB.Connection
With oConn
.ConnectionString = "Driver={MySQL ODBC 5.2 Unicode Driver};" & _
"Server=" & strServer & ";Port=3306;" & _
"Database=" & strDBName & ";" & _
"Uid=" & strUserID & ";" & _
"Pwd=" & strPasswd & ";Option=3;"
.open
end with

Related

How to INSERT/SELECT data from MySQL to Excel/VBA?

I'm trying to extract data from MySQL to Excel. 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"
Typically, this error is due to the following reasons:
Simple Misspelling: ODBC driver names must be exactly spelled correctly and fully including special characters. To see all ODBC drivers, open up PowerShell window and type: Get-OdbcDriver | Format-Table name, platform -AutoSize to output a table similar to below:
name platform
---- --------
Microsoft Access Driver (*.mdb) 32-bit
Microsoft dBase Driver (*.dbf) 32-bit
Microsoft Excel Driver (*.xls) 32-bit
Microsoft ODBC for Oracle 32-bit
SQL Server 32-bit
SQL Server Native Client 11.0 32-bit
ODBC Driver 13 for SQL Server 32-bit
SQL Server 64-bit
Microsoft Access Driver (*.mdb, *.accdb) 64-bit
Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb) 64-bit
Microsoft Access Text Driver (*.txt, *.csv) 64-bit
SQL Server Native Client 11.0 64-bit
PostgreSQL ANSI(x64) 64-bit
PostgreSQL Unicode(x64) 64-bit
ODBC Driver 13 for SQL Server 64-bit
Microsoft Access dBASE Driver (*.dbf, *.ndx, *.mdx) 64-bit
...
Incompatible 32/64 bit architectures: Whatever ODBC driver you connect to must maintain same bit architecture as your MS Office version. If you run 32-bit Office, only 32-bit drivers are available (indicated under platform in step 1 output). Similarly goes for 64-bit. To verify which Office bit version you have installed on Windows and Mac, see here.
Permissions issue: While drivers are available, they may not be accessible to current user or group at file and/or folder level. This tends to be more an issue on *Unix systems (Linux/Mac) and not Windows which requires chmod or chown handling. For Windows, adjust properties of file or folder in Explorer for read/write/execute permissions.

MariaDB 2.0.13 vs MySQL 5.3 ODBC Drivers (VBA Connection)

In Excel-VBA We used to have a sql database connection using MySQL drivers. Since this wasn't working for everyone we decided to install MariaDB drivers at every pc and use these drivers. The speed of opening an connection however, has decreased dramatically. Here is the code of the new and old vba script.
p_dbConn.ConnectionString = _
"DRIVER={MariaDB ODBC 2.0 Driver};" & _
"SERVER=xx;" & _
"DATABASE=xx;" & _
"UID=xx;PASSWORD=xx;OPTION=3"
p_dbConn.Open
p_dbConn.ConnectionString = _
"DRIVER={MySQL ODBC 5.3 ANSI Driver};" & _
"SERVER=xx;" & _
"DATABASE=xx;" & _
"UID=xx;PASSWORD=xx;OPTION=3"
p_dbConn.Open
I ran the script in debug mode and the .open statement takes way longer with the mariadb driver. Anyone has any idea why?
Thanks!
I know it's not the driver that you are using ... but I use the following connection in my VBA code to connect to SQL Servers:
Reference File: Microsoft DAO 3.6 Object Library
'*************************************
'* SQL Server database connection *
'*************************************
Dim db As ADODB.Connection
Set db = New ADODB.Connection
db.Open "Provider=sqloledb; Data Source = 192.168.0.10; Database = [your DB Name];User Id = xxxxxx; Password = xxxxxxxx"
if a command takes longer than 45 seconds to execute ... use this to extend the execution default time
db.CommandTimeout = 1200 '(1200/60 = 20 minutes)
Sample Simple Commands
Set rst = db.execute("Select * from [your table name]")
db.execute("Delete from [your table name] where [your criteria]")

excel vba mysql ado connection

I'm trying to establish an ADO connection between excel on my local machine and a MySQL database on my server.
In the examples I've seen (here and here, for instance) there's a driver of the form MySQL ODBC 5.x Driver. It seems that after installing the latest mysql connector / odbc download (32-bit, to match my msexcel) the relevant registry driver files HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ODBC Driver\ are now named 'SQL Server' and 'SQL Server Native Client 11.0.' I'm not having success establishing a connection to MySQL with either of these.
My VBA
Sub connect()
Dim Password As String
Dim SQLStr As String
Dim Server_Name As String
Dim User_ID As String
Dim Database_Name As String
Set rs = CreateObject("ADODB.Recordset") 'EBGen-Daily
Server_Name = "184.154.225.243"
Database_Name = "*******" ' Name of database
User_ID = "********" 'id user or username
Password = "*******" 'Password
Port = "3306"
SQLStr = "SELECT * FROM *******"
Set Cn = CreateObject("ADODB.Connection")
Cn.Open "Driver={SQL Server};Server=" & _
Server_Name & ";Port=" & Port & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"
rs.Open SQLStr, Cn, adOpenStatic
Upon running the above, I receive error [Microsoft][ODBC SQL Server Drive][DBNETLIB]SQL Server does not exist or access denied. The error for the 'native client 11.0' driver is Could not open a connection to SQL Server[53].
I've tested the connection parameters in MySQL workbench and all is functional. What's going on?
Check if you are using the 32 bit or the 64 bit version of Microsoft Office.
Based on the above, download and install the appropriate MySQL driver from the download link
Once the ODBC driver installation is complete, check the ODBC snap in to see the driver is listed as installed.
If you are using a 32 bit OS, then everything is 32 bit. Use Run -> odbcad32.exe -> Drivers tab.
If you are using a 64 bit OS, and Microsoft Office is 32 bit, then use c:\windows\syswow64\odbcad32.exe -> Drivers tab.
If you are using a 64 bit OS, and Microsoft Office is 64 bit, then use Run -> odbcad32.exe -> Drivers tab.
If the MySQL drivers are properly installed, they should appear as shown above
Create a System DSN using the ODBC snap in with the MySQL driver listed above and test the connection to see if it works.
Use the same parameters when you try to create an ODBC from within VBA.
Example:
Driver={MySQL ODBC 5.3 ANSI Driver};Server=localhost;Database=myDataBase;
User=myUsername;Password=myPassword;Option=3;
Once it is established that you can successfully create a connection to the MySQL server, then change the driver name in the registry (make sure to update both the registry keys) and try using the new name you give such as SQL Server.
Remember: On a x64 bit system for a x32 bit drivers:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ODBC\ODBCINST.INI\ODBC Drivers
A restart may be required after changing the driver name.

VBA/MySQL issue using ODBC connector

I have been struggling with this for a few days now. Any help much appreciated.
Trying to connect to MySQL database using Excel VBA, on a PC with the following:
Excel 2007
Windows 7 x64 Home Premium
MySQL 5.5
MySQL ODBC Connector 5.1, 64 bit
In the Excel VBA I have referenced Microsoft ActiveX Objects 2.8 Library.
The VBA I am using to connect is:
Dim oConn As ADODB.Connection
Public Sub ConnectDB()
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 5.1 Driver};" & "SERVER=localhost;" & "DATABASE=test;" & "USER=root;" & "PASSWORD=PWhere;" & "Option=3"
End Sub
Every time I run this I get the error dialog: "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"
MySQL service is definitely running.
I have used Windows Data Source Administrator to check that MySQL ODBC Connector 5.1 is present and correct: it is, checks out OK when I try to create a DSN in this way.
Looking through the VBA project reference options, I note the options to reference a whole host of different ADO Libraries, including (Multi-dimensional) options and library versions 2.0,2.1,2.5,2.6,2.7, and 6.0 - maybe the answer lies in one of these?
Any more details required, let me know.
You need to use either the 32 or 64 bits version depending on the version of Excel, not Windows. So even if you run Windows 7 64 bits, I believe Excel 2007 only comes in 32 bits so you would need to use the 32 bits mysql connector.
See also this bug report that is similar to your issue.
I got similar message when moved my application to other system with different version of driver - it looks like misspelled driver name causes identical message. To find correct driver name and make application driver version independent I use the following code:
Public Function Get_Driver() As String
Const HKEY_LOCAL_MACHINE = &H80000002
Dim l_Registry As Object
Dim l_RegStr As Variant
Dim l_RegArr As Variant
Dim l_RegValue As Variant
Get_Driver = ""
Set l_Registry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
l_Registry.enumvalues HKEY_LOCAL_MACHINE, "SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers", l_RegStr, l_RegArr
For Each l_RegValue In l_RegStr
If InStr(1, l_RegValue, "MySQL ODBC", vbTextCompare) > 0 Then
Get_Driver = l_RegValue
Exit For
End If
Next
Set l_Registry = Nothing
End Function

How to connect vb.net on my mysql database with windows 7

I have some problems with a vb.net application.
My application was perfectly working on XP, but now on my windows 7, each time I want to connect to localhost, I have a driver problem (in french, that's why I don't put it here)...
Do you know were does it come from?
My odbc driver is up to date...
Download and install Connector/.NET.
In your project, you should be able to add a reference to MySql.Data. You can then create your connection string.
Dim oMySqlConn As MySqlConnection = New MySqlConnection()
oMySqlConn.ConnectionString = "Data Source=localhost;" & _
"Database=mySQLDatabase;" & _
"User ID=myUsername;" & _
"Password=myPassword;" & _
"Command Logging=false"
oMySqlConn.Open()
localhost can be replaced with either an IP address or 'servename.com'