I'm hosting the MySQL database in Heroku. I created a connection from Excel with 'MySQL for Excel' add-in, but I want to establish a connection from Excel with VBA code.
I get
Access denied for user 'user' to database 'database'
My VBA code, but I think the reason for the error is somewhere else (privileges, database settings, etc... ).
Dim con As ADODB.Connection
Set con = New ADODB.Connection
Dim strConn As String
strConn = "Driver={MySQL ODBC 8.0 Unicode Driver};SERVER=serverName;DATABASE=databaseName;USER=username;PASSWORD=password"
con.Open strConn
For exammple, here is a piece of code by using mariaDB, btw you have to install corresponding driver for database:
host_address = "xxx.xxx.xxx.xxx"
Set conn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
conn.ConnectionString = "DRIVER={MariaDB ODBC 3.0 Driver};" _
& "SERVER=" & host_address & ";" _
& " DATABASE=databasename;" _
& "UID=username;PWD=password; OPTION=3"
conn.Open
strSQL ="....."
....
Related
I have multiple instances of MySQL installed in my pc which are instance1 & instance2...
1st instance data directory is "C:\MYSQL2\Data", address is: localhost, port= 3306;
2nd instance data directory is "C:\ProgramData\MySQL\MySQL Server 5.7\Data", address is: localhost, port= 3308;
server_name = "localhost"
port = "3308"
database_name = "test2"
user_id = "root"
password = "xxxx"
Set conn = New ADODB.Connection
conn.Open "DRIVER={MySQL ODBC 3.51 Driver}" _
& ";Data Source=localhost" _
& ";SERVER=" & server_name _
& ";PORT=" & port _
& ";DATABASE=" & database_name _
& ";UID=" & user_id _
& ";PWD=" & password _
& ";OPTION=16427"
When i run the above code to connect with 2nd instance database it showing me error:
Run-time error '-2147467259(80004005)': [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
This code also showing error for my 1st instance which server name is localhost and port is=3306
This code running fine where only default instance is installed.
In the connection string if
Driver={MySQL ODBC 8.0 Unicode Driver}
then if you open ODBC data Sources (32-bit) not ODBC data Sources (64-bit)
see the same name
Then this code will work
Public Sub ask_sql1()
Dim SQL As String
SQL = "SELECT * FROM Users"
Dim Conn As ADODB.Connection
Set Conn = New ADODB.Connection
Conn.ConnectionString = "Driver={MySQL ODBC 8.0 Unicode Driver};Server=127.0.0.1;Database=test;UID=root;PWD=*********"
Conn.Open
Dim recordSet As ADODB.recordSet
Dim Field As ADODB.Field
Set recordSet = New ADODB.recordSet
recordSet.Open SQL, Conn, adOpenStatic, adLockReadOnly, adCmdText
If recordSet.State Then
For Each Field In recordSet.Fields
MsgBox Field.Name
Next Field
Set recordSet = Nothing
End If
Conn.Close
End Sub
also is you office 64 or 32
Can you add a screenshot of ODBC data Sources (32-bit)/Drivers ?
I have added the Microsoft Active DataX Object to the Excel file.
I have also downloaded the mySQL ODBC Connector.
Below is the Code for Connection in VBA:
Sub ConnectDB2()
Dim rs
Dim Conn
Dim cmdString
Set rs = CreateObject("ADODB.Recordset")
Set Conn = CreateObject("ADODB.Connection")
Conn.ConnectionString = "DRIVER={MySQL ODBC 5.3 Driver};SERVER=52.74.22.123;DATABASE=sads;USERNAME=shridas;PASSWORD={KJH3232C8fed5AD!87367$Bds};"
Conn.Open
cmdString = "Select * from order"
Set rs = Conn.Execute(cmdString)
Conn.Close
End Sub
But I am getting an error:
'-2147467259 (80004005)': Automation error , Unspecified error.
Can you Please help me with this.
Change the USERNAME parameter in this line:
Conn.ConnectionString = "DRIVER={MySQL ODBC 5.3 Driver};SERVER=52.74.22.123;DATABASE=sads;USERNAME=shridas;PASSWORD={KJH3232C8fed5AD!87367$Bds};"
to just USER
Conn.ConnectionString = "DRIVER={MySQL ODBC 5.3 Driver};SERVER=52.74.22.123;DATABASE=sads;USER=shridas;PASSWORD={KJH3232C8fed5AD!87367$Bds};"
On my machine, I've also got to use different ODBC name:
DRIVER={MySQL ODBC 5.3 ANSI Driver}
I just installed SQL Server 2008 on my laptop. I also have Microsoft Access 2010 installed. Using VBA, I am trying to create an ADODB connection to my own database on SQL Server but I'm having trouble finding the right line of code:
When I use this below, it doesn't work.
The name of my computer is LAPTOPX and the database is HomeSQL.
I am sure it's super easy but since I'm just starting out I can't seem to find the right way to ask the question.
Thanks!
Dim DBCONT As Object
Set DBCONT = CreateObject("ADODB.Connection")
Dim strDbPath As String
strDbPath = "LAPTOPX/HomeSQL"
Dim sConn As String
sConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source =" & strDbPath & ";" & _
"Jet OLEDB:Engine Type=5;" & _
"Persist Security Info=False;"
DBCONT.Open sConn
First, you need to make sure SQL Native Client is instaled. Reference
SQL Server 2008
Standard security
Provider=SQLNCLI10;Server=myServerAddress;Database=myDataBase;Uid=myUsername;
Pwd=myPassword;
Trusted connection
Provider=SQLNCLI10;Server=myServerAddress;Database=myDataBase;
Trusted_Connection=yes;
Connecting to an SQL Server instance
The syntax of specifying the server instance in the value of the server key is the same for all connection strings for SQL Server.
Provider=SQLNCLI10;Server=myServerName\theInstanceName;Database=myDataBase;
Trusted_Connection=yes;
Source
Dim conn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim sConnString As String
Dim recordsAffected as Long
'Create connection string
sConnString = "Provider=sqloledb; Server=LAPTOPX; Database=HomeSQL; Trusted_Connection=True;"
'Open connection and execute
conn.Open sConnString
'Do your query
With cmd
.ActiveConnection = conn
.CommandType = adCmdText
.CommandText = "Select ...;"
.Execute recordsAffected 'Includes a return parameter to capture the number of records affected
End With
Debug.Print recordsAffected 'Check whether any records were inserted
'Clean up
If CBool(conn.State And adStateOpen) Then conn.Close
Set cmd = Nothing
Set conn = Nothing
This connetion string works under Excel VBA. In MsAccess also should.
dbName = "test" 'your database name
dbFilePath = "C:\db.mdf" 'your path to db file
connStr = "Driver={SQL Server native Client 11.0};" & _
"Server=(LocalDB)\v11.0;" & _
"AttachDBFileName=" & dbFilePath & ";" & _
"Database=" & dbName & ";" & _
"Trusted_Connection=Yes"
Full solution: http://straightitsolutions.blogspot.com/2014/12/how-to-connect-to-sql-server-local.html
I am trying to setup a remote database connection to a server running MySQL version 5.0.96. When I execute I get the following error.
Any assistance is appreciated.
Here is my connection code:
Public Function opendb()
Dim oConn As ADODB.Connection
Dim Server_Name As String
Dim User_Name As String
Dim Password As String
Dim Database_Name As String
Server_Name = Sheets("_config").Range("B2").value
User_Name = Sheets("_config").Range("B3").value
Password = Sheets("_config").Range("B4").value
Database_Name = Sheets("_config").Range("B5").value
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 5.0.96 Driver};" & _
"SERVER=Server_Name;" & _
"DATABASE=Database_Name;" & _
"USER=User_Name;" & _
"PASSWORD=Password;" & _
"Option=3"
End Function
Dim oConn As ADODB.Connection
Dim Server_Name As String
Dim User_Name As String
Dim Password As String
Dim Database_Name As String
Server_Name = Sheets("_config").Range("B2").value
User_Name = Sheets("_config").Range("B3").value
Password = Sheets("_config").Range("B4").value
Database_Name = Sheets("_config").Range("B5").value
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 5.0.96 Driver};" & _
"SERVER=" & Server_Name & ";" & _
"DATABASE=" & Database_Name & ";" & _
"USER=" & User_Name & ";" & _
"PASSWORD=" & Password & ";" & _
"Option=3"
There might be other problems with your drivers but when I tested the code you posted on my end I got the same error - it's not going to read your variables unless you concat them
Edit - I'm using a 5.2a server - when I try to connect using 5.0.96 I get the same error again , but of course using the 5.2a driver in the connection string works fine. So you will probably need to download the 5.0.96 driver specifically. I did a little searching and couldnt find it, I'll keep looking around for a minute
MySQL server is running 5.0.96. DO NOT INSTALL MySQL Connector/ODBC 5.2.6. This driver is NOT backwards compatible from VBA. If you have it installed and you are having the same problem I did do the follow:
1) FIRST remove any User & System Data Sources you have added
2) Uninstall all ODBC 5.2 drivers
3) Installed Connector/ODBC 3.51.30. (You May Need to Reboot)
4) Make your Connection String > DRIVER={MySQL ODBC 3.51 Driver};
Hope this saves you lots of time.
You may write Set oConn = CreateObject("ADODB.Connection") 'NEW STATEMENT
instead of Set oConn = New ADODB.Connection
I would like to write a macro in Excel that will write to a mysql database. Can someone please get me started on this?
You can connect to MySQL with a connection string and ADO:
''http://support.microsoft.com/kb/246335
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
strCon = "Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=MyDB;" _
& "User=root;Password=pw;Option=3;"
cn.Open strCon
You can also use DSN with a connection to Excel using the Jet driver:
Dim cn As ADODB.Connection
''Not the best way to get the name, just convenient for notes
strFile = Workbooks(1).FullName
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
Set cn = CreateObject("ADODB.Connection")
''For this to work, you must create a DSN and use the name in place of
''DSNName
strSQL = "INSERT INTO [ODBC;DSN=DSNName;].NameOfMySQLTable " _
& "Select AnyField As NameOfMySQLField FROM [Sheet1$];"
cn.Execute strSQL
Writing to a mysql database is no different to writing to any other database.
You'd create an ADODB.Connection object, .Open it with an appropriate connection string and use the .Execute method (or ADODB.Command) to execute sql.
See http://msdn.microsoft.com/en-us/library/ms807027.aspx for more information.
You'd have to a have a mysql access driver installed (ODBC or OLEDB) and reference the Microsoft ActiveX Data Objects 2.8 from your vba project.