MySQL query error (ODBC 3.51) - mysql

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.

Related

Execute SQL written in a textbox with VBA

With reference to this question: Avoid new line seperators in mySQL query within VBA code, I wanted to execude a SQL statement that is written in a textbox in the Excel-File.
Therefore, I created a textbox called SqlQuery1 looking like this:
In the VBA I refered to the textbox within the SqlString:
Sub Get_Data_from_DWH ()
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Set conn = New ADODB.Connection
conn.ConnectionString = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=XX.XXX.XXX.XX; DATABASE=bi; UID=testuser; PWD=test; OPTION=3"
conn.Open
SqlString = ThisWorkbook.Sheet1.Shapes("SqlQuery1").OLEFormat.Object.Text
Set rs = New ADODB.Recordset
rs.Open strSQL, conn, adOpenStatic
Sheet1.Range("A1").CopyFromRecordset rs
rs.Close
conn.Close
End Sub
However, I get runtime error 438 on the SqlString.
Do you have any idea what I need to change to make it work?
Thisworkbook.Sheet1 is not a valid object path, try instead:
SqlString = ThisWorkbook.Sheets("Sheet1").Shapes("SqlQuery1").OLEFormat.Object.Text
Or just
SqlString = Sheet1.Shapes("SqlQuery1").OLEFormat.Object.Text
And make sure the sheet is definitely named "Sheet1"
Also, you need to change
rs.Open strSQL, conn, adOpenStatic
to this:
rs.Open SqlString, conn, adOpenStatic
And you should probably use
Dim SqlString as String
at the start of the routine
You're definitely on the right track here and I can see that you've tried solving this error! :-)
Your issue is with the rs.Open part, as far as I can see. I've shuffled your code around a bit. As far as I can see, you did not add the ADODB.Command part. I've added this in the code snippet below.
A word of advice to save time in larger modules; declare your connection as a private global string at the beginning, so you can access it later on in the module.
Private Const CONNECTION As String = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=XX.XXX.XXX.XX; DATABASE=bi; UID=testuser; PWD=test; OPTION=3"
Sub Get_Data_from_DWH()
Dim cmd As ADODB.Command
Dim conn As ADODB.CONNECTION
Dim rs As ADODB.Recordset
Set conn = New ADODB.CONNECTION
conn.Open CONNECTION
conn.CommandTimeout = 900
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
If Not conn.State = ADODB.adStateOpen Then GoTo Bugcatcher
Set cmd = New ADODB.Command
cmd.ActiveConnection = CONNECTION
cmd.CommandText = ws.Shapes("TextBox 2").OLEFormat.Object.Text
cmd.CommandType = adCmdText
Set rs = cmd.Execute
ws.Range("A1").CopyFromRecordset rs
Bugcatcher:
Exit Sub
End Sub

create mysql database with vba

I use VBA mostly to access MySQL database and to downlaod data from the database into an excel worksheet. In order to open connection to MySQL server through vba i use the following code:
Public Sub OpenConnection()
Set conn = New ADODB.Connection
conn.Open GetConnectionString()
End Sub
Function GetConnectionString() As String
Dim ConnectionString$
ConnectionString$ = "DRIVER={MySQL ODBC 5.3 UNICODE Driver}; _
SERVER=localhost;DATABASE=test;USER=root;PASSWORD=google;Option=3"
GetConnectionString = ConnectionString$
End Function
my question is that is there a VBA code i can use to create a new database in MySQL server and give it a specified name?
my MySQL server is version 5.6 if it helps.
thanks for the comment Drew, I actually have other codes that enable me to enter query to MySQL database, the only this that was missing is the query to create a database if the database does not exits, anyways here is the whole code in order to create a MySQL database with VBA:
Sub create_database()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Call OpenConnection
Dim RS As ADODB.Recordset
Set RS = New ADODB.Recordset
Dim Query$
Query$ = "CREATE DATABASE IF NOT EXISTS Database_name"
RS.Open Query$, conn
Set RS = Nothing
Call CloseConnection
End Sub
Public Sub OpenConnection()
Set conn = New ADODB.Connection
conn.Open GetConnectionString()
End Sub
Function GetConnectionString() As String
Dim ConnectionString$
ConnectionString$ = "DRIVER={MySQL ODBC 5.3 UNICODE Driver}; _
SERVER=localhost;DATABASE=test;USER=root;PASSWORD=google;Option=3"
GetConnectionString = ConnectionString$
End Function
Public Sub CloseConnection()
conn.Close
Set conn = Nothing
End Sub

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

How to execute stored procedure from Access using linked tables

I have an Access 2003 database that connects to a SQL Server 2008 box via ODBC. The tables from SQL Server are connected as linked tables in Access. I have a stored procedure on the SQL Server that I am trying to execute via ADO code. The problem I have is that Access cannot seem to find the procedure. What do I have to do within Access to be able to execute this stored procedure? Some facts ...
The stored procedure in question accepts one parameter which is an integer. The stored procedure returns a recordset which I am hoping to use as the datasource for a ListBox.
Here is my ADO code in Access ...
Private Sub LoadUserCaseList(userID As Integer)
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "uspGetUserCaseSummaryList"
Dim par As New ADODB.Parameter
Set par = cmd.CreateParameter("userID", adInteger)
cmd.Parameters.Append par
cmd.Parameters("userID") = userID
Dim rs As ADODB.Recordset
Set rs = cmd.Execute()
lstUserCases.Recordset = rs
End Sub
The error I get is "the microsoft jet database engine cannot find the input table or query "uspGetUserCaseSummaryList".
CurrentProject.Connection is the connection to your Access database. You can verify that by doing this in the Immediate window:
Debug.Print CurrentProject.Connection
You need to create a new ADODB.Connection object with a connection string which points to your SQL Server instance. Have your ADODB.Command object use that connection.
Edit: You can eliminate the ADODB.Command object, and use the Execute method of the connection to return records from your stored procedure. This example uses a stored procedure which expects 3 parameters.
Private Sub GetCenterCodes()
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cnn = New ADODB.Connection
cnn.ConnectionString = "Provider=SQLOLEDB;Data Source=VM2003\sqlexpress;" _
& "User ID=foo;Password=bar;Initial Catalog=Inventory"
cnn.Open
Set rs = New ADODB.Recordset
Set rs = cnn.Execute("EXEC uspGetCenterCodes 14, 14, 501")
Debug.Print rs(0), rs(1), rs(2)
rs.Close
Set rs = Nothing
cnn.Close
Set cnn = Nothing
End Sub
You can find a connection string example which matches your needs at ConnectionStrings.com