Open a new quey from code

  • Thread starter Thread starter Neil
  • Start date Start date
N

Neil

Hello,

How can you get Access to open a new query from code (like clicking on the
"Create query in design view" button in the database window)

Cheers,

Neil.
 
---------- "Neil said:
How can you get Access to open a new query from code (like clicking on the
"Create query in design view" button in the database window)

Neil,

here's some code:

Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As String

Set db = CurrentDb

'Put together the query as needed
'(see also the SQL view of saved queries)
strSQL = "SELECT * FROM MyTable"

'Create the new query

'If you want to save the query
'so that it will show up in the database window,
'you need to specify the name:
Set qdf = db.CreateQueryDef("MyQuery", strSQL)

'If you want a temporary query
'not saved in the DB, pass a nullstring as
'the name
Set qdf = db.CreateQueryDef("", strSQL)

Set db = Nothing

Please note: if there is already a query with the given name, there'll
be an error. See also Help item CreateQueryDef method and the
examples.

Best regards
Emilia

Emilia Maxim
PC-SoftwareService, Stuttgart
http://www.maxim-software-service.de
 
Emilia Maxim said:
Neil,

here's some code:

Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As String

Set db = CurrentDb

'Put together the query as needed
'(see also the SQL view of saved queries)
strSQL = "SELECT * FROM MyTable"

'Create the new query

'If you want to save the query
'so that it will show up in the database window,
'you need to specify the name:
Set qdf = db.CreateQueryDef("MyQuery", strSQL)

'If you want a temporary query
'not saved in the DB, pass a nullstring as
'the name
Set qdf = db.CreateQueryDef("", strSQL)

Set db = Nothing

Please note: if there is already a query with the given name, there'll
be an error. See also Help item CreateQueryDef method and the
examples.

Best regards
Emilia

Emilia Maxim
PC-SoftwareService, Stuttgart
http://www.maxim-software-service.de

Thanks Emilia,

I will give your sugestion a try.

Neil.
 
Back
Top