Query in code

  • Thread starter Thread starter Marc
  • Start date Start date
M

Marc

Query in code

If I enter a query using sql-code, how can I add the query
to the query definitions of the database to see it when I
call it from:
1. Another query
2. A report
3. A subform

Can someone past me an example of all the code necessary
for defining a query? Does it need to be defined from a
module? Will the new query appear automatically on the
list of existing queries when I want to call it from
another object such as a report or I need to enter a
special function?

Thank you very much,
 
Marc said:
Query in code

If I enter a query using sql-code, how can I add the query
to the query definitions of the database to see it when I
call it from:
1. Another query
2. A report
3. A subform

A Query written in SQL is just the same as a Query created using the Query
By Expression Grid. The QBE Grid itself is just a user-friendly way for
Access to get around to making SQL for the user.

Any Query (aka QueryDef) is used the same way as any other.

SELECT Q1.*
FROM MyQuery AS Q1
WHERE Q1.MyColumn = <whatever>

MyQuery = Some other Query in Access.
 
Query in code

If I enter a query using sql-code, how can I add the query
to the query definitions of the database to see it when I
call it from:
1. Another query
2. A report
3. A subform

Can someone past me an example of all the code necessary
for defining a query? Does it need to be defined from a
module? Will the new query appear automatically on the
list of existing queries when I want to call it from
another object such as a report or I need to enter a
special function?

Thank you very much,

If you're entering the query using the SQL window of the Query editor,
you can save it as a named query. You don't need any code at all, and
you can copy and paste the SQL text from some outside source (such as
a newsgroup post or another program - watch for differences in SQL
dialects!)

If you're creating the SQL string in VBA code, use the CreateQuerydef
method:

Dim strSQL As String
Dim db As DAO.Database
Dim qd As DAO.QueryDef
Set db = CurrentDb
strSQL = <some valid SQL string>
Set qd = db.CreateQuerydef(strSQL)
 
Back
Top