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
Related
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.
Objective:
I'm trying to connect to a database (e.g. associated with a website hosted by goDaddy) via VBA; using MS Word. I would like to distribute the VBA code via a word template so that others can also connect to my database.
Current Understanding - Is it correct?
In order to connect to a remote mySQL database I MUST configure a ODBC Data Source using (for example) mySQL Connector/ODBC (available here)?
There seems to be a way to connect without using a DSN as suggested here.
Issue:
I have been trying to use the mySQL Connector tool and am attempting to configure it with the information I have at hand. Steps taken:
download connector tool from dev.mysql.com
Control Panel > System & Security > Administrative Tools > ODBC Data Sources 64 Bit
add host: www.mywebsite.com
add user: NameOfDataBaseUser
add pwrd: PWForUser
I get the impression that I am using the wrong credentials... I found some documentation that said a list of DataBases would be displayed if connection is successful. That suggests to me that I should be using credentials for a master user - which user would that be?
Disclaimer
I do plan to connect to an online DB via VBA, but suspect that it might be better to connect indirectly via a php web-page.
If anyone has thoughts on this (security, ease of deployment, other) please let me know, it would probably be a new question. Other disclaimer, I am highly INexperienced with databases but keen to learn - slowly ;-)
I am currently working on a project with Excel where I am successfully connecting to a remote MySQL database.
I am using the DSN-less approach and this could probably work well for you, too:
Set remoteCon = New ADODB.Connection
conStr = "DRIVER={MySQL ODBC 5.3 Unicode Driver};" & _
"SERVER=myhomepage.com;PORT=3306;DATABASE=mydb;" & _
"UID=username;PWD=secret"
remoteCon.Open conStr
remoteCon.Execute ("USE mydb;")
In order for this to work, you also have to add a reference (in VBA backend): Tools > References > Check "Microsoft ActiveX Data Objects x.x Library".
You also need to have the MySQL ODBC Driver (in my case "MySQL ODBC 5.3 Unicode Driver") installed on your computer.
Queries can then be executed like this:
Dim rs As ADODB.Recordset
Set rs = remoteCon.Execute("SELECT * FROM table")
If Not rs.BOF And Not rs.EOF Then
result = rs.GetRows
End If
How to connect VBA to a Remote mySQL DataBase using ODBC
Thanks to #EVilliger & #tobifasc for your help with this, there are many 'how to configure mySQL questions' floating around but none that solved my (larger) issue.
Basic problem - my host does NOT allow remote connections to the database, except for from a single white-listed IP (this seems fairly common).
Questions Answered:
It turns out that you do NOT need to configure the mySQL connection using the connector, however you DO NEED to have an appropriate ODBC driver installed. The connector (with the driver) can be found here: https://dev.mysql.com/downloads/connector/odbc/
I uninstalled the mySQL Connector and everything seemed to continue working, until it didn't. Conclusion don't uninstall the mySQL Connector unless you have something to replace it with.
The credentials to use can be for a database user, not some elevated user.
For anyone interested in setting up and experimenting with mySQL from VBA here is a way forward:
Download & install the drivers (see above) - if the install fails you may need to install vcredist_x64.exe
Set-up a free mySQL database, I used HelioHost: https://www.heliohost.org/ (they will also give you a domain)
Create a database & user in HelioHost cPanel
Configure remote access - use wildcard % to allow all IPs
Add user to database
Use the following code to connect...
Code from accepted answer:
Sub connect2OnlineSQL()
' Note: add referecne to Microsoft ActiveX Data Objects #.# Library
' Tools > References > (scroll down...)
Set remoteCon = New ADODB.Connection
conStr = "DRIVER={MySQL ODBC 5.3 Unicode Driver};" & _
"SERVER=something.heliohost.org;PORT=3306;DATABASE=db_name;" & _
"UID=db_user;PWD=yourPassWordHere"
remoteCon.Open conStr
remoteCon.Execute ("USE db_name;")
End Sub
Thanks again :)
Following solution worked for me
Prerequisite:
Under Developer ->Tools->Reference Add the relevant Plug in for Oracle
Download and install ODBC Driver version for Oracle which you are using (32bit/64 bit)
Windows search -> ODBC Data Source -> Add Oracle driver under user DSN and System DSN
Copy paste below code which I used for MySQL and change the parameter based on your requirement
Sub ConnectToDB()
dbName = InputBox("Enter DB Name")
'Connection To MySQL
Dim oConn As ADODB.Connection
Dim str As String
str = "DRIVER={MySQL ODBC 8.0 Unicode Driver};SERVER=localhost;DATABASE=Employeeportal;PORT=3306;UID=root;PWD=root;"
Set oConn = New ADODB.Connection
oConn.Open str
MsgBox "Connected to MySQL DB"
'Exporting result set to Excel
Dim query As String
query = "select * from " & dbName
Dim recordSet As New ADODB.recordSet
recordSet.Open query, oConn
Sheet1.Range("A1:D1").CopyFromRecordset recordSet
oConn.Close
End Sub
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
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
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.