PC Review


Reply
Thread Tools Rate Thread

recordset on a query

 
 
dickminter@clearchannel.com
Guest
Posts: n/a
 
      29th Jan 2004
My query criteria references a field value on an open form
([forms]![frmName]![fieldname]). When I use the SQL
statement for this query to create a DAO recordset, I get
an error. If I remove the field reference from the SQL
statment and substitute a value, it works find.

I can solve the problem by using docmd to run the query as
a make table, and set the recordset on the the table
created, but why won't it work the other way? How else to
filter the recordset using the field value from the open
form?

DM

P.S. The purpose of the query is to create a recordset
containing all records from the open form's linked subform
with fields values from both.
 
Reply With Quote
 
 
 
 
Dirk Goldgar
Guest
Posts: n/a
 
      30th Jan 2004
"(E-Mail Removed)" <(E-Mail Removed)>
wrote in message news:6efa01c3e6bb$2a508a20$(E-Mail Removed)
> My query criteria references a field value on an open form
> ([forms]![frmName]![fieldname]). When I use the SQL
> statement for this query to create a DAO recordset, I get
> an error. If I remove the field reference from the SQL
> statment and substitute a value, it works find.
>
> I can solve the problem by using docmd to run the query as
> a make table, and set the recordset on the the table
> created, but why won't it work the other way? How else to
> filter the recordset using the field value from the open
> form?
>
> DM
>
> P.S. The purpose of the query is to create a recordset
> containing all records from the open form's linked subform
> with fields values from both.


The problem is that, while Access knows what that form reference means,
DAO does not. The form reference is a query parameter that must be
filled in before the query can be evaluated. When *Access* processes
the query, as in DoCmd.OpenQuery or DoCmd.RunSQL -- or when you open the
query from the database container window -- it fills in the paramer
value from the form. But when you open the query via DAO, it's your
responsibility to fill in the parameter value. For a stored query, you
can do this by way of the DAO QueryDef object, like this:

Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim prm As DAO.Parameter
Dim rs As DAO.Recordset

Set db = CurrentDb
Set qdf = db.QueryDefs("MyStoredQuery")

For Each prm in qdf.Parameters
prm.Value = Eval(prm.Name)
Next prm

Set rs = qdf.OpenRecordset

' ... work with rs ...
rs.Close

Set rs = Nothing
Set qdf = Nothing
Set db = Nothing

If you're building your query as a SQL statement in a string literal or
variable, though -- rather than using a stored query -- you don't need
all the above. You can just embed the current value of the form control
as a literal in the SQL string, like this:

Dim strSQL As String

strSQL = _
"SELECT <some fields> FROM <some table expression> " & _
"WHERE CriteriaField=" & Forms!FormName!ControlName

Set rs = CurrentDb.OpenRecordset(strSQL)

If the field in question is text or a date field, you need to include
the proper delimiters in building your string; e.g.,

strSQL = _
"SELECT <some fields> FROM <some table expression> " & _
"WHERE CriteriaField='" & Forms!FormName!ControlName & "'"

strSQL = _
"SELECT <some fields> FROM <some table expression> " & _
"WHERE CriteriaField=#" & Forms!FormName!ControlName & "#"


--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
query a recordset =?Utf-8?B?U0Ft?= Microsoft Access VBA Modules 3 2nd May 2007 04:27 PM
recordset from a query on a query not updateable =?Utf-8?B?Sm9uYXRoYW4=?= Microsoft Access Queries 1 6th Dec 2005 04:50 AM
recordset for sum query Alex Microsoft Access VBA Modules 2 31st Aug 2004 02:31 AM
Re: !Recordset from a parameterized query, as the form's recordset. Problem on sorting... Savvoulidis Iordanis Microsoft Access Forms 0 24th Jun 2004 09:52 AM
Query as recordset Brad Microsoft Access VBA Modules 4 17th Feb 2004 06:15 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:01 PM.