views:

3362

answers:

5

Hello Everyone,

This is a super basic question but I'm trying to execute a Query that I'm building via some form values against the MS Access database the form resides in. I don't think I need to go through ADO formally, but maybe I do.

Anyway, some help would be appreciated. Sorry for being a n00b. ;)

A: 

Are you building a new query (i.e. raw SQL) from scratch based on the values collected in the form, or are you trying to execute an existing parameterized query, and trying to pass the form values as parameters to the query?

Mike Spross
+2  A: 

You can use the following DAO code to query an Access DB:

Dim rs As DAO.Recordset
Dim db As Database

Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT * FROM Attendance WHERE ClassID = " & ClassID)

do while not rs.EOF
  'do stuff
  rs.movenext
loop

rs.Close
Set rs = Nothing

In my case, ClassID is a textbox on the form.

jinsungy
I'd accept this answer except that the If block doesn't function as excepted (I don't think). It should be `If Not rs.EOF Then`?
Tim Visher
I've edited it to include the "not rs.EOF" and the rs.movenext.
jinsungy
A: 

This is what I ended up coming up with that actually works.

Dim rs As DAO.Recordset
Dim db As Database

Set db = CurrentDB
Set rs = db.OpenRecordset(SQL Statement)

While Not rs.EOF
    'do stuff
Wend

rs.Close
Tim Visher
You need to rs.Movenext statement if you want to iterate through the records in the recordset, assuming SQL will return multiple rows.
jinsungy
Save yourself a set and use CurrentDb.OpenRecordset(). There is no need to make the separate database object.
Brettski
I think that sample code should use best practices, and CurrentDB.OpenRecordset() in DAO can possible leave an explicit object reference hanging. Secondly, as sample code, that's the place where you'd open a *different* database than the current one, so as sample code, I think it's better.
David-W-Fenton
A: 

The answers you've been given and that you seem to be accepting loop through a DAO recordset. That is generally a very inefficient method of accomplishing a text. For instance, this:

  Set db = CurrentDB()
  Set rs = db.OpenRecordset("[sql]")
  If rs.RecordCount > 0
     rs.MoveFirst
     Do While Not rs.EOF
       rs.Edit
       rs!Field = "New Data"
       rs.Update
       rs.MoveNext
     Loop 
  End If
  rs.Close
  Set rs = Nothing
  Set db = Nothing

will be much less efficient than:

  UPDATE MyTable SET Field = "New Data"

which can be run with:

  CurrentDb.Execute "UPDATE MyTable SET Field = 'New Data'"

It is very seldom the case that one needs to loop through a recordset, and in most cases a SQL update is going to be orders of magnitude faster (as well as causing much shorter read/write locks to be held on the data pages).

--
David W. Fenton
David Fenton Associates

David-W-Fenton
Well, question doesn't actually state that an update is being performed. Might be reading data out for display?
MarkJ
Display in what? A form? If so, then that's not the best way to do it. If in a report, it's *really* not the best way to do it. To me "execute a query" means "update data via a query," but perhaps I'm reading the OP wrong.
David-W-Fenton
Actually, they said "execute a Query": big Q = big difference. A query (small q) is a SQL DML SELECT, which would prehap imply a recordset. A Query (big Q) is a MS Access (not Jet, not ACE) object that may contain any SQL statement: SELECT/INSERT/UPDATE/DELETE SQL DML, even SQL DDL or SQL DCL.
onedaywhen
A: 

Here just in case you wanted an ADO version:

Dim cn as new ADODB.Connection, rs as new ADODB.RecordSet
Dim sql as String

set cn = CurrentProject.Connection
sql = "my dynamic sql string"

rs.Open sql, cn ', Other options for the type of recordset to open, adoOpenStatic, etc.

While Not rs.EOF
  'do things with recordset
  rs.MoveNext   ' Can't tell you how many times I have forgotten the MoveNext. silly.
Wend
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing

DAO and ADO are very close in usage. You get more control with DAO and slightly better performance with ADO. In most access database applications I have come across it really doesn't make a difference. When I have seen a big difference is with linked tables. ADO often performs better.

Brettski
I've found DAO works faster with Access 97 format databases, and ADO with Access 2000 and greater. I think it's a difference between jet 3.5 and jet 4.0
MarkJ