Been using this construct to edit sql statements in code.
Would like to find out if this can be put into a function or if can put [tt]QueryDefs[/tt] and [tt]CreateQueryDef[/tt] in a variable so can use one line instead of using the above code for all the places I need to modify a query. I experimented with Eval and either didn't have syntax right or couldn't get it to work.
Could above 6 lines of code be simplified something like this:
stsql="Select..."
EditQryDef("qryQuery")
Here is what I tried.
Module:
form:
Code:
...
...
stsql="Select....."
If QueryExists("qryQuery") Then
Set qd = db.QueryDefs("qryQuery")
Else
Set qd = db.CreateQueryDef("qryQuery")
End If
qd.SQL = stsql
Would like to find out if this can be put into a function or if can put [tt]QueryDefs[/tt] and [tt]CreateQueryDef[/tt] in a variable so can use one line instead of using the above code for all the places I need to modify a query. I experimented with Eval and either didn't have syntax right or couldn't get it to work.
Could above 6 lines of code be simplified something like this:
stsql="Select..."
EditQryDef("qryQuery")
Here is what I tried.
Module:
Code:
Function EditQryDef(stQueryName As String)
'Edit the query based on supplied variables
'Test for existence. If query exists, edit
'querydef, otherwise create querydef. Use
'to replace all the instances in the other
'modules and subs
'20150918
Dim db As DAO.Database
Dim qd As DAO.QueryDef
On Error GoTo NewQueryDef
Set db = CurrentDb
EditQryDef = "Set qd = db.QueryDefs(""" & stQueryName & """)"
QryDef_Exit:
Exit Function
NewQueryDef:
EditQryDef = "Set qd = db.CreateQueryDef(""" & stQueryName & """)"
Resume QryDef_Exit
End Function
form:
Code:
Eval("EditQryDef('XYZ')")
Code:
Eval(EditQryDef("XYZ"))
Eval ("EditQryDef('XYZ')") give run time error 91
Eval (EditQryDef("XYZ")) gives run time error 2482
Or how could I get the statement to run with a variable? example qdqc would contain either "CreateQueryDef" or "QueryDefs"
Set qd = db. & qdqc & ("qryQuery")