Error 3001 Excel VBA MySQL Select - mysql

I'm trying to select rows in a MySQL database using Excel Macros. The connection appears to be working OK but I get a VBA 3001 error
(Microsoft visual basic 3001 arguments are of the wrong type, or are out of acceptable range, or are in conflict with one another)
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim oConn As ADODB.Connection
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 5.2 Unicode Driver};" & _
"SERVER=localhost;" & _
"DATABASE=talar;" & _
"USER=root;" & _
"PASSWORD=root;" & _
"Option=3"
strSQL = "SELECT * FROM ots where Estado in (2,3)"
rs.Open SQLStr, Cn, adOpenForwardOnly, adLockReadOnly
Dim myArray()
myArray = rs.GetRows()
oConn.Close
MySQL ODBC 5.2 Unicode Driver is installed, MySQL service is running fine, I am using Excel 2010, windows 7. anyone have any idea? thanks!

You've actually got two problems both of which would be easier to spot of you were using Option Explicit.
Both are in this line:
rs.Open SQLStr, Cn, adOpenForwardOnly, adLockReadOnly
Your SQL string variable is strSQL and your connection object is oConn. So change the line of code to:
rs.Open strSQL, oConn, adOpenForwardOnly, adLockReadOnly

Related

Trying to extract data from MySQL to Excel using VBA

I am getting an error and I don't understand what is the problem. Here is my code
Sub test()
Dim rs As ADODB.Recordset
Dim sqlstr As String ' SQL to perform various actions
Dim oConn As ADODB.Connection
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 5.3 Unicode Driver};" & _
"SERVER=ETS-DEV-01;" & _
"DATABASE=reporting;" & _
"USER=guest_user;" & _
"PASSWORD=0X4ZT9kwsY%yGOp;" & _
"Option=3"
sqlstr = "select * from tveuptimes"
rs.Open sqlstr, oConn
End Sub
I am getting the error:
Run-time error '91'
Object variable or with block variable not set.
I don't understand what i am doing wrong.
The line Set oConn = New ADODB.Connection isn't necessary. Instead, when you dimension rs and oConn add the word New before the type, like this:
Sub test()
Dim rs As New ADODB.Recordset
Dim sqlstr As String
Dim oConn As New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 5.3 Unicode Driver};" & _
"SERVER=ETS-DEV-01;" & _
"DATABASE=reporting;" & _
"USER=guest_user;" & _
"PASSWORD=0X4ZT9kwsY%yGOp;" & _
"Option=3"
sqlstr = "select * from tveuptimes"
rs.Open sqlstr, oConn
End Sub
Here's a good tutorial: http://analysistabs.com/excel-vba/ado-sql-macros-connecting-database/

Error 3001 vba Excel Macros MySQL Insert

Im trying to insert a row in a mysql db using Excel Macros. The connection appears to be working OK but i get a vba 3001 error
(Microsoft visual basic 3001 arguments are of the wrong type, or are out of acceptable range, or are in conflict with one another)
when y execute this code:
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
ConnectDB
'strSQL = "INSERT INTO talar.ots (UbicacionTecnica, Equipo, Posmant) VALUES ('sdasd', 'sdasd','sdasd')"
rs.Open strSQL, oConn, adOpenDynamic, adLockOptimistic
I allready read and try different things with 50 tutorials and other posts in this page, all bad results....
this is the code of the connection:
Private Sub ConnectDB()
Dim oConn As ADODB.Connection
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 5.2 Unicode Driver};" & _
"SERVER=localhost;" & _
"DATABASE=talar;" & _
"USER=root;" & _
"PASSWORD=root;" & _
"Option=3"
End Sub
MySQL ODBC 5.2 Unicode Driver is installed, mysql service is running fine, I am using Excel 2010, windows 7. I dont know if this information is enough.
anyone have any idea?
thanks!
oConn is local to the ConnectDB sub so you are passing nothing to the recordsets Open.
Add Option Explicit to the top of your code file to receive a warning when you do something like this.
Make a function that returns the connection:
Private Function ConnectDB() As ADODB.Connection
Set ConnectDB = New ADODB.Connection
ConnectDB.Open "DRIVER={MySQL ODBC 5.2 Unicode Driver};SERVER=localhost;DATABASE=talar;USER=root;PASSWORD=root;Option=3"
End Function
Then
dim cn as ADODB.Connection
set cn = ConnectDB()
cn.Execute "INSERT INTO talar.ots (UbicacionTecnica, Equipo, Posmant) VALUES ('sdasd', 'sdasd','sdasd')"
cn.Close
You do not need a Recordset for an insert as no rows will be returned.
When you do need a Recordset adOpenForwardOnly, adLockReadOnly are better than adOpenDynamic, adLockOptimistic unless you specifically need the features offered by the latter.

Access 2013 Error Binding to ADO Recordset

I'm trying to bind a continuous form to an ADO Recordset using "Microsoft ActiveX Data Objects 2.8 Library". I'm getting the following error when I try to bind the recordset to my form:
3265 Item cannot be found in the collection
corresponding to the requested name or ordinal.
And here's my code:
Dim cn As New ADODB.Connection, rs As New ADODB.Recordset
cn.ConnectionString = "Driver={MySQL ODBC 5.2 ANSI Driver};" & _
"Server=redacted;Database=redacted;" & _
"User=redacted;Password=redacted;"
cn.Open
rs.Open "SELECT * FROM device", cn, adOpenStatic, adLockReadOnly
'I can debug.print records and fields right here
Set Me.Recordset = rs 'Error happens here
cn.Close
Set cn = Nothing
I'm using Office 2013 32bit on Windows 8.1 64bit with the MySQL 32bit ODBC driver (the 64bit driver cannot be called from a 32bit application).
Well, the problem persisted on Access 2010/Win7. But I fixed it by specifying the cursor location before running the Open method:
rs.CursorLocation = adUseClient
rs.Open "SELECT * FROM device", cn, adOpenStatic, adLockReadOnly

How can VBA connect to MySQL database in Excel?

Dim oConn As ADODB.Connection
Private Sub ConnectDB()
Set oConn = New ADODB.Connection
Dim str As String
str = "DRIVER={MySQL ODBC 5.2.2 Driver};" & _
"SERVER=sql100.xtreemhost.com;" & _
"PORT=3306" & _
"DATABASE=xth_9595110_MyNotes;" & _
"UID=xth_9595110;" & _
"PWD=myPassword;" & _
"Option=3"
''' error '''
oConn.Open str
End Sub
Private Sub InsertData()
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
ConnectDB
sql = "SELECT * FROM ComputingNotesTable"
rs.Open sql, oConn, adOpenDynamic, adLockOptimistic
Do Until rs.EOF
Range("A1").Select
ActiveCell = rs.Fields("Headings")
rs.MoveNext
Loop
rs.Close
oConn.Close
Set oConn = Nothing
Set rs = Nothing
End Sub
Doing the similar things in PHP, I could successfully log in to the MySQL server.
I have installed the ODBC connector.
But in the above VBA codes, I failed.
An error turns up. (see the codes where the error exists)
$connect = mysql_connect("sql100.xtreemhost.com","xth_9595110","myPassword") or die(mysql_error());
mysql_select_db("myTable",$connect);
This piece of vba worked for me:
Sub connect()
Dim Password As String
Dim SQLStr As String
'OMIT Dim Cn statement
Dim Server_Name As String
Dim User_ID As String
Dim Database_Name As String
'OMIT Dim rs statement
Set rs = CreateObject("ADODB.Recordset") 'EBGen-Daily
Server_Name = Range("b2").Value
Database_name = Range("b3").Value ' Name of database
User_ID = Range("b4").Value 'id user or username
Password = Range("b5").Value 'Password
SQLStr = "SELECT * FROM ComputingNotesTable"
Set Cn = CreateObject("ADODB.Connection") 'NEW STATEMENT
Cn.Open "Driver={MySQL ODBC 5.2.2 Driver};Server=" & _
Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"
rs.Open SQLStr, Cn, adOpenStatic
Dim myArray()
myArray = rs.GetRows()
kolumner = UBound(myArray, 1)
rader = UBound(myArray, 2)
For K = 0 To kolumner ' Using For loop data are displayed
Range("a5").Offset(0, K).Value = rs.Fields(K).Name
For R = 0 To rader
Range("A5").Offset(R + 1, K).Value = myArray(K, R)
Next
Next
rs.Close
Set rs = Nothing
Cn.Close
Set Cn = Nothing
End Sub
Ranjit's code caused the same error message as reported by Tin, but worked after updating Cn.open with the ODBC driver I'm running. Check the Drivers tab in the ODBC Data Source Administrator. Mine said "MySQL ODBC 5.3 Unicode Driver" so I updated accordingly.
Just a side note for anyone that stumbles onto this same inquiry... My Operating System is 64 bit - so of course I downloaded the 64 bit MySQL driver... however, my Office applications are 32 bit... Once I downloaded the 32 bit version, the error went away and I could move forward.
Updating this topic with a more recent answer, solution that worked for me with version 8.0 of MySQL Connector/ODBC (downloaded at https://downloads.mysql.com/archives/c-odbc/):
Public oConn As ADODB.Connection
Sub MySqlInit()
If oConn Is Nothing Then
Dim str As String
str = "Driver={MySQL ODBC 8.0 Unicode Driver};SERVER=xxxxx;DATABASE=xxxxx;PORT=3306;UID=xxxxx;PWD=xxxxx;"
Set oConn = New ADODB.Connection
oConn.Open str
End If
End Sub
The most important thing on this matter is to check the proper name and version of the installed driver at:
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers\
Enable Microsoft ActiveX Data Objects 2.8 Library
Dim oConn As ADODB.Connection
Private Sub ConnectDB()
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 5.1 Driver};" & _
"SERVER=localhost;" & _
"DATABASE=yourdatabase;" & _
"USER=yourdbusername;" & _
"PASSWORD=yourdbpassword;" & _
"Option=3"
End Sub
There rest is here: http://www.heritage-tech.net/908/inserting-data-into-mysql-from-excel-using-vba/
Just to update this further
instead of looping through every row and column which takes forever.
try using
`Sub connect()
Dim Password As String
Dim SQLStr As String
'OMIT Dim Cn statement
Dim Server_Name As String
Dim User_ID As String
Dim Database_Name As String
'OMIT Dim rs statement
Set rs = CreateObject("ADODB.Recordset") 'EBGen-Daily
Server_Name = "Server_Name "
Database_Name = "Database_Name" ' Name of database
User_ID = "User_ID" 'id user or username
Password = "Password" 'Password
SQLStr = "SELECT * FROM item"
Set Cn = CreateObject("ADODB.Connection") 'NEW STATEMENT
Cn.Open "Driver={MySQL ODBC 8.0 ANSI Driver};Server=" & _
Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"
rs.Open SQLStr, Cn, adOpenStatic
Range("A2").CopyFromRecordset rs
rs.Close
Set rs = Nothing
Cn.Close
Set Cn = Nothing
End Sub
this is multiple minutes faster

Connect to mysql 5.0 database using pure vbscript?

I've tried the below script but I am getting an error:
dim cn, rs
set cn = CreateObject("ADODB.Connection")
set rs = CreateObject("ADODB.Recordset")
cn.connectionstring = "Provider=MysqlProv; Data Source=Adonis; User Id=mysqluser; Password = mysqlpass;"
cn.open
rs.open "select * from Countries", cn, 3
rs.MoveFirst
while not rs.eof
wscript.echo rs(0)
rs.next
wend
cn.close
wscript.echo "End of program"
Its giving the following error:
C:\mysql.vbs(6, 1) ADODB.Connection: Provider cannot be found. It may not be pro
perly installed.
When I googled for an odbc connector I came up to this page where I could download the odbc 5.1 connector. Wondering if this is enough to connect to a mysql server 5.0 database...?
Install MySQL Connector/ODBC and use a connection string like the following
connectionString = "Driver={MySQL ODBC 5.1 Driver};Server=yourServerAddress;" & _
"Database=yourDataBase;User=yourUsername;" & _
"Password=yourPassword;"
I made small changes to the above script and is working fine:
dim cn, rs
i = 0
set cn = CreateObject("ADODB.Connection")
set rs = CreateObject("ADODB.Recordset")
connectionString = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;" & _
"Data Source=dsn_hb; Database=TP; User=root; Password=***;"
cn.Open connectionString
rs.open "select * from test.Login", cn, 3
rs.MoveFirst
'msgbox rs(0)'
while not rs.eof
msgbox rs.Fields(0)
rs.MoveNext
wend
cn.close
MsgBox "End of program"