When I try to add this reference - "Microsoft DAO 3.51 Object Library" (there is also DAO 3.6), it gives me this error.
"Name conflicts with existing module, project, or object library."
Currently, I have the following selected as references
- Visual basic for applications
- MS Access 10.0 Object library
- OLE automation
- MS ActiveX Data Objects 2.1 library
- MS DAO 2.5/3.51 Compatibility library
I think I was getting the error message, because I declared rs as ADODB.Recordset instead of just Recordset. How are these two different, and would you also know why ADODB was causing the error?
When I refer to my Access Queries, then is it not necessary to do rs.Open sql, CurrentProject.Connection???
ADO (ActiveX data objects) is the newer way to access data, DAO (data access objects) is still popular but ADO probably is the way to go, especially if you are not set in your ways. I will eventually crack and get proficient with ADO.
'- set reference which is the ADOX library
'- Microsoft ADO Ext. 2.6 for DDL and Security
Dim cg As New ADOX.Catalog
Set cg.ActiveConnection = CurrentProject.Connection
Dim vn As View
Set vn = cg.Views("query1"
sql1 = "Select * from query1"
rst.MoveFirst
'Whatever you want to do with the recordset...
rst.Close
con.Close
Set rst = Nothing
Set con = Nothing
End Function
Using the Currentdb.Name avoids having to hard code the location of the database, in case you want to move it to a different drive or folder later.
Use adCmdStoredProc if you want to run an existing query
Use adCmdText if you want to hard code a SQL statement
Use adCmdTable if you want to return the contents of a table
sjh,
It's a little hard to tell what you're asking, but if what you are wanting to do is use some piece of data from the recordset as criteria in a query, here's how:
On the criteria line of the query, instead of putting a value, put the name of the function where the "ADO Query" is located, followed by a pair of parenthesis.
The SQL will look like this:
SELECT CUST.CUST_NBR, CUST.F_NAME, CUST.L_NAME
FROM CUST
WHERE CUST.CUST_NBR=myFunction();
The function is like this:
Public function myFunction() as String
<all of the Dims, open connection, open recordset; etc.>
myFunction = rst.Fields("<field name>"
End Function
Now, each time the query is run, it will fetch the criteria from the recordset first.
Of course, if you need to, you can pass arguments to the function, and use them to modify the function.
If what you were asking is how to use the "Parameter Collection" of the ADO, just let me know, and I'll send you some sample code that does that. (It is quite a bit more complicated than what I talked about above, but not so bad that you couldn't do it, if you really need to).
If you don't know for sure what technique you need to use, just write and describe what you're trying to accomplish, and I'll try to come up with a simple solution.
My code is: Dim rs As Recordset
Dim SQL As String
SQL = "SELECT count(table1.test) as TestCount FROM table1"
Set rs = CurrentDb().OpenRecordset(SQL)
Me.txtTest = rs("TestCount"
rs.Close
Runtime error '13' Type Mismatch.
Tried the ADODB for Dim rs As ADODB.Recordset and still the same error.
Mike,
As far as I know, (which is admittedly not very far most of the time), you cannot put the parentheses after the CurrentDB reference (I don't know why that is, because CurrentDb is a function that returns a reference to the "current" database), anyway, you would need to just do this:
Set rs = CurrentDb.OpenRecordset(SQL)
Here's a line of code from one of my modules where I do essentially the same thing (with DAO):
Set datetest = CurrentDb.OpenRecordset("Select top 1 [mydate] from tester order by mydate desc;"
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.