How to create a QueryDef object - easy

C

cjg.groups

I am having problems creating a QueryDef object to store the SQL
statement that I'm building in VB. This must be a simple syntax
problem. Here are excerpts of my code:

Dim qdfMake As DAO.QueryDef
Dim strSQL As String

strSQL = "SELECT Orders.OrderID, "
' use conditionals to finish building strSQL

Set qdfMake = New DAO.QueryDef
' Set qdfMake = db.QueryDefs("qryOutputReport_calcs")
qdfMake.SQL = strSQL
qdfMake.Parameters(0) = Eval(strCtlOID)
qdfMake.Execute

The statement is assigned to .SQL, as shown by "Debug.Print
qdfMake.SQL". But the .Parameters line fails, saying something's not
in the collection. Commenting that line out, the Execute line fails
saying the object doesn't exist (error 91).

I tried to reuse a properly created qdfMake by commenting my Set line
and uncommenting the one below. The Execute line failed claiming it
didn't have enough parameters (the built query depends on a form
control), so maybe the object is ok. But oddly, now a QueryDef and
RecordSet earlier in my code have failed.......

Any ideas for simply creating an object? Thanks.
 
M

MGFoster

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

You have to use the CreateQueryDef function, not the New predicate.
E.g.:

Set qdfMake = db.CreateQueryDef("qryOutputReport_calcs", strSQL)

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBRJmWe4echKqOuFEgEQJiigCdHFfymeGQvkLojXGrJh5hgD2w8OsAoNup
3i4VQZQ+++GoEy2SqKplBQQl
=ilOZ
-----END PGP SIGNATURE-----
 
C

cjg.groups

Thank you very much for your help. This worked well (though when I
don't specify a name, it gives me a runtime error 3125, which I'm
looking into).

To help me ask less silly questions, is there a method for finding
answers like this? Newsgroups led me to New() and CreateObject(). I
suspected QueryDefs needed to be associated with the database, but I
didn't think to check there in Help. I'm learning Access entirely from
Newsgroups and Help (due to limited implementation time), so any help
 

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