I asked a similar question here.
The second block of code worked perfectly before. But now I'm getting the read-only error on it for some reason. I pasted it again below.
Dim oConn As New ADODB.Connection
Dim cmd As New ADODB.Command
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 5.1 Driver};" & _
"SERVER=server;" & _
"DATABASE=database;" & _
"USER=user;" & _
"PASSWORD=password;" & _
"Option=3"
Set cmd = New ADODB.Command
cmd.ActiveConnection = oConn
cmd.CommandText = strSQL
cmd.Execute
oConn.Close
MsgBox ("Query Successful")
I tried looking through the documentation and didn't see anything too helpful. I also mainly did an insert on the database and it still ran fine so the database isn't read-only.
What's wrong? Is there a way to force a read/write property?
Ok I essentially just created a new endpoint that was a copy of my old server, set server= to something else, and it worked. Both servers have the same permissions so I'm not exactly sure why but that's my fix for anyone who encounters this in the future.
I've got lot of problem with the ODBC 5.1 Drivers for MySQL (provided by default). Same case than you, worked fine and one day, many problems without any modification... But was solved with an update to the last version of MySQL Connector, the 8.0.
You can go into "ODBC Data Source Adminsitrator" from the Start Menu, and check into your "Drivers" panel if you have the last one. If not, you can download it on the official MySQL website here.
Then replace DRIVER={MySQL ODBC 5.1 Driver} by DRIVER={MySQL ODBC 8.0 Driver}.
Hope will help on your problem.
Related
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]")
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.
I'm using MS Access 2007 with Win7 Professional x64.
I have to connect to an external MySQL database passing by the ODBC v5.1 local driver.
From VBA I'm able to connect to my database succesfully and from Access I can open my linked tables and see the data.
You can see below the code that I'm using to create the linked table. I dont want to use DSN way; I prefer the driver way.
Dim CnnString As String
Dim tdf As DAO.TableDef
CnnString = "ODBC;DRIVER=MySQL ODBC 5.1 Driver;DATABASE=xxxx;OPTION=2048;PORT=0;SERVER=xxxxx;UID=xxxxx;PWD=xxxx;Connect Timeout=45; Command Timeout=90;"
Set tdf = CurrentDb.CreateTableDef("table1")
tdf.Connect = CnnString
tdf.SourceTableName = "table1"
CurrentDb.TableDefs.Append tdf
Set tdf = Nothing
When I quit my VBA/Access application and I reopen it, the linked table appears in the tables list but if I double click on it, the ODBC connector windows appears asking me a DSN connection, I cannot see the data and my application doesn't work.
How can I make the connection to my linked table permanent?
Thanks in advance
Now I've had the chance to check how I did this...
I have successfully (and permanently) linked DSN-less ODBC tables with VBA using the DoCmd.TransferDatabase command:
' CnnString as above
DoCmd.TransferDatabase _
TransferType:=acLink, _
DatabaseType:="ODBC", _
DatabaseName:=CnnString, _
ObjectType:=acTable, _
Source:="table1", _
Destination:="table1", _
StructureOnly:=False, _
StoreLogin:=True
The important part is StoreLogin:=True -- I don't think you can specify this when using the CreateTableDef method.
I am running into a weird situation, hoping someone here can help.
I am communicating to a MySQL database using 'MySQL ODBC 5.1 Driver', although the below code works fine when run separately on a .vbs file, when i put the same in HTA i get Error
Error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
I put the same code in a .vbs file and run the VBS file via an HTA using objShell.run command still the same problem.
When the VBS file is run independently or via CMD it runs fine displaying the contents. Any ideas ?
Call Query
Sub Query
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
adOpenStatic = 3
adLockOptimistic = 3
objConnection.Open _
"Driver={MySQL ODBC 5.1 Driver};Server=[Some Address];Database=bldb;User=usr;Password=pass;"
objRecordSet.Open "SELECT * FROM Clients Where Machine Like 00000" & , _
objConnection, adOpenStatic, adLockOptimistic
Do While Not objRecordSet.EOF
Company = objRecordSet (1)
Contact = objRecordSet (2)
Phone = objRecordSet (3)
objRecordSet.MoveNext
Loop
objRecordSet.Close
objConnection.Close
Set objRecordSet=Nothing
Set objConnection=Nothing
End Sub
I was able to figure this on, the problem was the 32/64 Bit nature of HTA and the ODBC Driver.
I had installed a 64Bit ODBC driver and my HTA was running as a 32Bit Application.
Either run %Windrir%\System32\mshta.exe or install the the 32Bit Driver.
I chose to install the 32Bit variant of the MySQL ODBC Driver which resolved the problem.
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