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.
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.
I'm trying to write a straightforward piece of code to connect to a MySQL database.
Sub DBConnection()
Dim conn As New ADODB.Connection
Dim connectionStr As String
connectionStr = "DRIVER={SQL SERVER} ;SERVER=localhost; database=sakila; uid=test1; pwd=123"
conn.Open connectionStr
conn.Close
MsgBox "Connected!"
End Sub
I am faced with this error when I run it:
I suspect the details I am providing in the connection may be incorrect but I'm not sure.
User and database name in the MySQL workbench:
Perhaps it's the server name but as I understand it being on my local machine it should be localhost.
Would appreciate any help on this, thanks!
I have the following MYSQL products installed
I have installed the 5.2 driver and followed the connection string for 5.2 from the website provided. The error is still persisting (see below). I'll keep googling but any help would be appreciated as I am very new to this.
My first stop for connection strings is connectionstrings.com, where you should e able to find a connection string that fits your MySQL version and your connection method.
The standard MySQL connection string is:
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
...and several others on the site for various situations.
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
I've just started looking into interacting with an SQL database via Excel VBA, and I started off with the basic connection code from MSDN:
Sub GetDataFromADO()
'Declare variables'
Set objMyConn = New ADODB.Connection
Set objMyRecordset = New ADODB.Recordset
Dim strSQL As String
'Open Connection'
objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=fatcoin;User ID=root;Password=root;"
objMyConn.Open
'Set and Excecute SQL Command'
strSQL = "select * from productlist"
'Open Recordset'
Set objMyRecordset.ActiveConnection = objMyConn
objMyRecordset.Open strSQL
'Copy Data to Excel'
ActiveSheet.Range("A1").CopyFromRecordset (objMyRecordset)
End Sub
The issue I'm having is while I have an SQL instance running on my machine, on port 3306, which I can access using for example, HeidiSQL, every time I run this code I get an error message:
[DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied
I have also tried adding a port:
objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=localhost,3306;Initial Catalog=fatcoin;User ID=root;Password=root;"
And other such things. I can't see any reason it shouldn't work, but I haven't played with this much at all. I've tried searching through other threads to no avail.
Any ideas?
I am working with Excel 2010, on a 64-bit Machine, using MySQL 5.7
I should mention the above coding is being inputted into the "Module1" section of the VBAProject on Excel.
Thanks
Thanks for the answers.
I solved my own question via a helpful link I found that suggested I go into ODBC 32-bit (because Excel is 32-bit) and create a System DSN. Then, I used the connections string below:
objMyConn.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;User ID=root;Data Source=localhost;Initial Catalog=fatcoin"
It worked a treat.