Excel VBA Connection string to MySQL remote Database is not working - mysql

I have MySQL server installed on a remote machine.
I am trying to connect to it via excel VBA - i can succesfully connect to it through excels built-in database connection tools and copy the connection string, however, VBA hates that connection string. here is my code and the error
Dim conn As New ADODB.Connection
Dim sConnectionString As String
Set conn = New ADODB.Connection
sConnectionString = "ODBC;Driver={MySQL ODBC 5.3 ANSI Driver};Provider=MSDASQL;Server=AddressHere;Port=3306;Database=DatabaseName;User=UserName;Option=3"
conn.ConnectionString = sConnectionString
conn.Open
Here is the error messgae i get when it tries to connect, i have verified that the ODBC driver is installed.

Have you checked to make sure 32 bit vs 64 bit are all configured/installed? It gets a bit messy...
Based on personal experience with a very similar problem. 64 bit OS using 32 bit office install and 32 bit mySQL eventually made it work for me.. but different combinations can prove to be incompatible.
Don't think that your 64 bit OS means you need 64 bit mySQL if your Office install is 32 bit. Also keep in mind that if you use ODBC to connect, there are 32 bit and 64 bit versions of that too in your system.

Related

Output parameter of MySQL stored procedures via ADODB in MS Access (VBA) correct on one computer and random on another

I have tried (nearly) everything to isolate the problem, but I am lost.
I have an MS Access application that uses ADODB to interface to a local MySQL database. I copied it to a new computer, but now the output parameters of the stored procedures contain a random value each time (if done via ADODB). When executed in MySQL WorkBench, the output parameters are correct.
Here are the specs of the 2 computers:
old: Windows 7 Pro, Office 2010 ProPlus, MySQL ODBC 5.3.4, MySQL server 5.6.22 (all are 64-bit; 32-bit ODBC is also installed);
new: Windows 10 Pro, Office 2016 ProPlus, MySQL ODBC 5.3.6, MySQL server 5.7.16 (all are 64-bit, except MS Office; 32-bit ODBC is also installed).
To isolate the problem, I used the following simple stored procedure:
CREATE PROCEDURE `testit`(
OUT iTest INT(11))
BEGIN
SET iTest = 123;
END
And a test MS Access database containing only the following VBA code (and a reference to the Microsoft ActiveX Data Objects 6.1 library):
Public Function dbTestIt() As Long
Dim dbConn As ADODB.Connection
Dim dbCmd As ADODB.Command
'Open new connection
Set dbConn = New ADODB.Connection
dbConn.ConnectionString = "Driver={MySQL ODBC 5.3 Ansi Driver};option=3;database=xxx;user=root;password=yyy;"
dbConn.Open
'Execute new command
Set dbCmd = New ADODB.Command
With dbCmd
Set .ActiveConnection = dbConn
.CommandTimeout = 0
.CommandType = adCmdStoredProc
.CommandText = "testit"
.Parameters.Append dbCmd.CreateParameter("iTest", adInteger, adParamOutput)
.Execute
dbTestIt = dbCmd.Parameters.Item(0).Value
End With
'Close Connection
dbConn.Close
End Function
Here are the test results:
On the old computer, dbTestIt() always returns 123;
On the new computer, dbTestIt() returns random values (e.g. 51, 1936020585, 1);
And if I connect from the new computer to the MySQL server on the old computer (using server=192.168.1.x in the connection string), it always returns 123 as well;
This tells me the problem is (only) in the MySQL server on the new computer (right?);
However, if I then connect from the old computer to the MySQL server on the new computer, it also always returns 123!
So the problem seems to be in the combination of the components on the new computer, but which and why? And how to test it?
Anybody any bright ideas?
To further locate the cause of the problem, I downgraded the MySQL ODBC driver 5.3.6 on the new computer to version 5.3.4 (which is on the old computer) and now it works! To confirm that this is indeed the cause, I upgraded to version 5.3.6 again and it failed again.
So it looks like MySQL ODBC driver 5.3.6 (released 8 months ago!) has problems handling output parameters from a (local) MySQL Server 5.7.16, maybe only in combination with ADODB and Windows 10.
So for the moment I will use the 5.3.4 version and report a bug to MySQL.
MySQL server version 5.7 has compatibility issues with latest ODBC connectors. Downgrading the server to version 5.6 fixed the issue.
Even after 4 years, this bug is still there. Since I could not get any MySQL 5.3/8.0 ODBC driver to work with MySQL Server 8.0.22 and I did not want to be held hostage by this bug any longer, I decided to switch from a stored procedure (with an output parameter) to a stored function (with a return value) and made a few changes in the VBA example above:
.CommandType = adCmdText
.CommandText = "select testit() as parOut"
Set dbRest = .Execute
dbTestIt = dbRest!parOut

64 bit Wscript AND 64 bit Microsoft.ACE.OLEDB.12.0

I have 64 bit windows 10 (and 7), 64 bit wscript opening my script, and AccessDatabaseEngine_x64.
I cannot get things to work with everything running x64, though 32 bit wscript works fine. I have attempted this on several computers including a new windows install. I am always greeted with...
Script: C:\Users\ARoberts\Desktop\REPORTER\reporter_1.vbs
Line: 126
Char: 3
Error: Provider cannot be found. It may not be properly
installed.
Code: 800A0E7A
Source: ADODB.Connection
from the below VBSCRIPT connection method.
Set DBConn = CreateObject("ADODB.Connection")
With DBConn
.Provider = "Microsoft.ACE.OLEDB.12.0"
'.Properties("Extended Properties").Value = "Excel 12.0 Xml; ReadOnly=false; HDR=Yes;IMEX=1;"
.Open fso.BuildPath(CurrentDirectory, "DB.mdb")'xlsx")
End With
I have done countless hours of research on this and wonder if the driver simply isn't capable of playing nicely with x64-wscript. I have tried excel files, access databases, along with a preconfigured User DSN. My database is very large and requires a large amount of processing.
Please let me know what other information I can provide and I will update my question with it.

Error trying to connect to MySql using DSN

It has been a long time since using VB6 and I didn't use MySql or DSN connections then, but I'm working on a legacy application that uses these. When I try to open the connect I am getting a
Data source not found and no default driver specified with this code
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset
Dim sql As String
Set rs = New ADODB.Recordset
Set cmd = New ADODB.Command
Set conn = New ADODB.Connection
conn.Open "DSN=AddressHealthcheck" '<- Fails on this statement
I have also tried specifying the DSN in the conn.ConnectionString but get the same error.
The AddressHealthcheck exists in the local System DSN, uses the MySql ODBC 5.1 Driver and connects successfully when tested.
I'm sure (and hope) I missing something obvious. Any suggestions?
Thanks,
Kevin
I use a DSN-less ODBC stored in a UDL. It's basically a file with a connection string in it. Maybe the connection string will be of use. Here's an example:
[oledb]
; Everything after this line is an OLE DB initstring
Provider=MSDASQL.1;Persist Security Info=True;Extended Properties="Driver=MySQL ODBC 5.1 Driver;SERVER=mysqlsvr;UID=userid;PWD=password;DATABASE=mydatabase;PORT=3306;OPTION=18475";Initial Catalog=mydatabase
rags comment (above) wasn't the solution, but it definitely helped track it down.
When using a full connection string the error message was different and much more useful, the driver failed to load.
The issue seems to be that since I'm on a 64 bit machine it was the 64 bit driver. VB6 can't use the 64 bit driver and the 32 bit driver doesn't show up in the ODBC Connection Administrator on a 64 bit machine.
DSN is not an option in my machine.
There are two ODBC Admin tools in a 64-bit system. Run the 32-bit version. Or better yet drop ODBC and get an OLEDB Provider for MySQL.
I was researching this by 1 labour day (yesterday). Today I've realized some errors I had yesterday in order to have this working.
Dim conexion As New ADODB.Connection
conexion.ConnectionString = "DSN = yourDsnName"
conexion.open
Don't forget to...
Create a user in your mysql server that has all the priviledges you need.
Assign the server's name or ip from where it is going to connect to mysql
Create your dsn on the client machine with the correct options en test it.

Excel VBA connect to MySQL - architecture mismatch error

I am trying to connect to MySQL from my local machine located on a server using VBA. Initially I was receiving the below error.
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified.
After some research, I figured out that the MySQL ODBC driver should be installed first. I installed the driver from the below location.
http://dev.mysql.com/downloads/connector/odbc/
I have a 64 bit machine and so I installed the driver for 64 bit and tried to establish the connection. Even then I was receiving the same data source name not found error. However from the ODBC data source administrator, if I select System DSN, I am able to see MySQL driver installed and I am able to create a new data source for my database in the server.
However from VBA, if I call the data source I receive another error.
[Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application.
This is how I am calling the data source from my VBA.
Dim oConn As ADODB.Connection
Set oConn = New ADODB.Connection
oConn.Open "data_source_name"
Also,for the 64 bit ODBC connector I am able to see two drivers as below in my System DSN.
MySQL ODBC 5.2 ANSI Driver
MySQL ODBC 5.2 Unicode Driver
For both of the drivers, my connection is successful from the ODBC data administrator.
I tried to figure out a solution for the architecture mismatch problem and I read if I create the DSN by running the ODBC application from the below location on a 64 bit machine, it might work.
C:\Windows\System32\odbcad32
However for this scenario too, I received the same architecture mismatch error.
I even tried installing MySQL Connector/ODBC 5.2.5 32 bit in my machine. For this case, I am not able to see the driver listed in the system DSN.
Can someone help me figure out what am actually doing wrong?
I can select and loop thru results sets and do inserts. data verified in db. let me know if you need help
windows 7 Ultimate
version 6.1 (build 7601: service pack 1)
64 bit
************************************************************************
ODBC Data Source Administrator
run by command: %windir%\system32\odbcad32.exe
2 drivers:
MySQL ODBC 5.2 ANSI Driver , 5.02.05.00, Oracle, MYODBC5A.DLL, 4/4/2013
MySQL ODBC 5.2 Unicode Driver , 5.02.05.00, Oracle, MYODBC5W.DLL, 4/4/2013
************************************************************************
create System DSN, named hp
using Unicode Driver
dsn=hp
descr=hp
tcpip server=192.168.1.11
user=root
password=xxxxx
Database=test
************************************************************************
ODBC Data Source Admin tool
far right tab called About
Admin, Control Panel, Cursor Library, Driver Mgr, Localized R DLL, Unicode Cursor Lib all Version 6.1.7601-ish
************************************************************************
MSFT Office Professional Plus 2010
Version 14.0.6129.5000 (64 bit)
VBA 7.0
Tools Menu / References / References - VBAProject, scroll down, click on:
Microsoft ActiveX Data Objects 6.1 Library
References Location= c:\program files\common files\system\ado\msado15.dll
************************************************************************
code same, get into an excel Macro:
Sub Macro1()
'
' Macro1 Macro
'
Dim oConn As ADODB.Connection
Dim rsPass As ADODB.Recordset
Dim sql As String
Set oConn = New ADODB.Connection
oConn.Open "hp"
Set rsPass = New ADODB.Recordset
sql = "select * from charlie1"
rsPass.Open sql, oConn
rsPass.Close
sql = "insert into charlie1 (billybob,birthdate,funny_num) values (5,now(),383.111)"
rsPass.Open sql, oConn
End Sub
Run Task Manager and look for EXCEL.EXE - most likely it has *32 after it (the issue is you are running a 32-bit version of excel, attempting to use the 64-bit version of MySQL Connector/ODBC).
To resolve:
Be sure to remove any defined ODBC data sources (DSNs) before
uninstalling the 64-bit driver (unable to remove if the driver is
already uninstalled)
Uninstall 64-bit MySQL Connector/ODBC driver
Download and install 32-bit version of MySQL Connector/ODBC driver
To setup DSN, see this http://forums.mysql.com/read.php?37,357786,360776#msg-360776
Note: You may be able to have both the 64-bit and 32-bit drivers installed and therefore not require uninstall of the 64-bit version. I did not need both so I haven't tested whether it is possible to have both installed.
I was recently fighting with this problem myself.
Taking advice from iOSdedude, I followed the link you originally posted, downloaded the 32-bit driver, and my ODBC connection started working again.
I am running Windows 7 on and my OS shows 64-bit under My Computer --> Properties --> System Info, so I was surprised to see that the 64-bit driver didn't work.
Not a good explanation as to why this works, but it worked for me.

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