Trying to connect in VBA, ADO to localhost MySQL - 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

Related

Trying to connect a mysql database to an excel sheet using visual basics

I feel like this is a simple problem but i cant seem to figure it out. I am trying to establish a connection between my excel sheet and my mysql server but keep getting the error "Run-time error '-2147467259 (80004005)':
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified" I have the necessary drivers installed so i am unsure where to go from here, this is the code i have so far
Dim conn As New ADODB.Connection
Dim TableName As String
Dim sqlstr As String
Dim rs As ADODB.Recordset
Dim CRow As Long, Trow As Long
Dim Column1 As Integer, Column2 As Integer
Set conn = New ADODB.Connection
conn.Open ("DRIVER = {MySQL ODBC 5.3 Unicode Driver}" _
& ";SERVER=127.0.0.1" _
& ";DATABASE=tmi_stock" _
& ";UID=root" _
& ";PWD=MyRoot2018")
Set rs = New ADODB.Recordset
sqlstr = "INSERT INTO tester VALUE(1, 2)"
rs.Open sqlstr, conn, adOpenStatic
Set rs = Nothing
conn.Close
Set conn = Nothing

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.

VBA to mysql connection

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}

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

MySQL Sample for Visual Basic 6.0 - read/write

I'd like to find a simple example of working with remote MySQL base. I know, there are some tutorial over the internet, explaining how to set up ADODB.Connection and connectionstrings, but I couldnt make it work. Thanks for any help!
Download the ODBC connector from the MySQL download page.
Look for the right connectionstring over here.
In your VB6 project select the reference to Microsoft ActiveX Data Objects 2.8 Library. It's possible that you have a 6.0 library too if you have Windows Vista or Windows 7. If you want your program to run on Windows XP clients too than your better off with the 2.8 library. If you have Windows 7 with SP 1 than your program will never run on any other system with lower specs due to a compatibility bug in SP1. You can read more about this bug in KB2517589.
This code should give you enough information to get started with the ODBC connector.
Private Sub RunQuery()
Dim DBCon As adodb.connection
Dim Cmd As adodb.Command
Dim Rs As adodb.recordset
Dim strName As String
'Create a connection to the database
Set DBCon = New adodb.connection
DBCon.CursorLocation = adUseClient
'This is a connectionstring to a local MySQL server
DBCon.Open "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=myDataBase; User=myUsername;Password=myPassword;Option=3;"
'Create a new command that will execute the query
Set Cmd = New adodb.Command
Cmd.ActiveConnection = DBCon
Cmd.CommandType = adCmdText
'This is your actual MySQL query
Cmd.CommandText = "SELECT Name from Customer WHERE ID = 1"
'Executes the query-command and puts the result into Rs (recordset)
Set Rs = Cmd.Execute
'Loop through the results of your recordset until there are no more records
Do While Not Rs.eof
'Put the value of field 'Name' into string variable 'Name'
strName = Rs("Name")
'Move to the next record in your resultset
Rs.MoveNext
Loop
'Close your database connection
DBCon.Close
'Delete all references
Set Rs = Nothing
Set Cmd = Nothing
Set DBCon = Nothing
End Sub