Access 2013 Error Binding to ADO Recordset - ms-access

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

Related

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

Error 3001 Excel VBA MySQL Select

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

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.

Excel VBA: writing to mysql database

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.

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"