how to invoke existing query as record source when coding? Thanks a lot!

X

xpengi

hi guys,

i have built some queries named "query1, query2....", now i want t
build a subform's record source based on those queries....my questio
is how to invoke those queries in coding?

for example, i code it as
...
Me.RecordSource = [query1]

but not work, did i use the wrong way "[query1]" to invoke ?

in additin, I know if i build the query in code, like
...
strQuery = "select * form students"
me.RecordSource = strQuery
...will be fine, whereas my boss required the previous way...

Thanks a lot!

Pete
 
A

Allen Browne

The RecordSource property is a String type, so put the name of the query in
quotes, e.g.:

Me.RecordSource = "Query1"
 
J

Jeff Boyce

Me.RecordSource = "query1"

(your version, with [query1], tells Access to look for a field named
query1).

Good luck

Jeff Boyce
<Access MVP>
 
X

xpengi

Thanks Allen, it works in my subform!

a new question is, how to get the content of my existing query "query1
of which is "select * from...",

for example,
...
dim strQuery as string

Me.RecordSource = "Query1"

strQuery = content of "Query1" <-- i don't know how to get it
.....


Thanks really
 
A

Allen Browne

Examine the SQL property of the QueryDef:

strQuery = CurrentDb.QueryDefs("Query1").SQL
 
J

John Vinson

hi guys,

i have built some queries named "query1, query2....", now i want to
build a subform's record source based on those queries....my question
is how to invoke those queries in coding?

for example, i code it as
..
Me.RecordSource = [query1]

but not work, did i use the wrong way "[query1]" to invoke ?

Yes: you can use

Me.RecordSource = "Query1"

i.e. set the recordsource property to a text string naming the query.
It also works if the text string is a valid SQL query.

John W. Vinson[MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top