VBA to mysql connection - mysql

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}

Related

Connecting to MySQL Database (hosted in Heroku) from Excel VBA

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 ="....."
....

Excel VBA: ODBC Driver Manager]Data source name not found and no default driver specified

I couldnt connect my vba to the mysql. can you tell me where did I go wrong?
this method raise runtime error "[Microsoft][ODBC Driver Manager]Data source name not found and no default driver specified"
my OS is win 7 64 bit and I just installed 64bit ODBC Connector from https://downloads.mysql.com/archives/c-odbc/
but still the error occurs. I also use xampp with no password if that is also need specifying.
Sub SqlConnect()
Dim ReturnArray
Dim Conn As New ADODB.Connection
Dim mrs As New ADODB.Recordset
Dim DBPath As String
Dim sconnect As String
DBPath = ThisWorkbook.FullName
sconnect = "DRIVER={MySQL ODBC 5.2.2 Driver}; SERVER = localhost;" & _
"PORT=3306;" & _
"DATABASE= timeintimeoutdb;" & _
"UID=root;" & _
"PWD=;"
Conn.Open sconnect
sqlstring = "SELECT * FROM [students]"
mrs.Open sqlstring, Conn
ActiveSheet.Range("A2").CopyFromRecordset mrs
mrs.Close
Conn.Close
End Sub

Trying to connect in VBA, ADO to localhost MySQL

I'm using
Sub fff()
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
Dim RS As ADODB.Recordset
ConnectionString = "{MySQL ODBC 5.3 Unicode Driver};Server=localhost;Database=test;User=root;"
cn.Open ConnectionString
cn.Close
Set cn = Nothing
MsgBox "connected"
I'm running Office 32b on Win 64b
I've installed MySql drivers as shown here:
I get the error

MySQL query error (ODBC 3.51)

I'm trying to execute query in a VB6 app.
Here is my code:
Dim con As ADODB.Connection
Set con = New ADODB.Connection
con.ConnectionString = "Driver={MySQL ODBC 3.51 Driver}; Server=***; Database=***; Username=***; Password=***; Option=3"
con.Open
Dim cmd As New ADODB.Command
With cmd
.ActiveConnection = con
.CommandText = "SELECT COD_CONFIG FROM FDT_CONFIG"
.CommandType = adCmdText
End With
Dim rs As New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open cmd, , adOpenStatic, adLockOptimistic
I've hidden some information in the connection string but in my app I'm using the correct values.
I can successfully open the connection, but when I try to execute the query I get:
"Execution error '2147467259 (800004005)' : unspecified error"
Can someone tell me where I'm going wrong?
If you want to assign a Command object's ActiveConnection to an existing Connection object, you must use Set:
With cmd
Set .ActiveConnection = con
....
End With
It's a little confusing because you can also assign a string to the ActiveConnection property and ADO will create an ad-hoc connection for you. In that case, you wouldn't use Set because you're just assigning a value to an intrinsic type (string):
With cmd
.ActiveConnection = "Driver={MySQL ODBC 3.51 Driver}; Server=***; Database=***; Username=***; Password=***; Option=3"
...
End With
So the property can be used multiple ways. In your scenario, however, since you're assigning an object reference, you'll need to use the Set keyword.

odbc driver does not support the requested properties

odbc driver does not support the requested properties error come when we run the program at last line can anyone please give me idea.
Dim conn As New ADODB.Connection
Dim rsRec As ADODB.Recordset
Dim cmd As ADODB.Command
Dim query As String
Set conn = New ADODB.Connection
Set rsRec = New ADODB.Recordset
conn.connectionString = "DRIVER={MySQL ODBC 3.51 Driver}; Server=127.0.0.1 ;Database=try;User=root;Password=root;"
conn.Open
query = "INSERT INTO user_table (Name)"
query = stSQL & "VALUES (Anupam)"
rsRec.Open query, conn, adOpenDynamic, adLockOptimistic
I must note that this error is somewhat confusing since it also occurs when there is ANY typo in the SQL statement.
I for example had this error because a table was renamed and therefore did no longer exist.
When you encounter this error, check the spelling of all parameters and the table names.
Try This:-
Dim conn As New ADODB.Connection
Dim rsRec As ADODB.Recordset
Dim cmd As ADODB.Command
Dim query As String
Set conn = New ADODB.Connection
Set rsRec = New ADODB.Recordset
conn.connectionString = "DRIVER={MySQL ODBC 3.51 Driver}; Server=127.0.0.1 ;Database=try;User=root;Password=root;"
conn.Open
query = "INSERT INTO user_table (Name) VALUES (Anupam)"
rsRec.Open query, conn, adOpenDynamic, adLockOptimistic