I'm afraid you didn't miss anything, I did! Yes, you can store the where
clause in the table field, but when it's read in the code it will indeed
be treated a string - stupid of me.
It is still possible to do it in a similar fashion, though more
complicated: you would need a separate table with a one to many
relationship to the forms, one record per field to filter on with fields
for FormName, FieldName, FormCOntrolName and type (numeric vs. text);
then you would need to open this table as a recordset in your code,
filtering on the form name, and loop through the records constructing
the Where clause. workable, though not very neat.
Alternatively, you could use a convention like: no additional table, all
the info for fields to filter on comes from the form itself: for all
fields you wish to filter on, name the corresponding control on the form
with a perfix followed by the field name (like fltrFlieldX for FiledX),
then loop in your code through the form controls and add to the where
clause string for those controls starting fltr, extracting the field
name from the control name (as in Right(ControlName,
Len(ControlName)-4)...). If all the fields you filter on are one kind
(either text or numeric) then things are simple, otherwise you might
rely on the filter value to determine, checking the value with the
IsNumeric function (tricky if you have text fields with numerals only!).
Another approach to that end might be to use an extra letter in the
prefix to idenntify the type, like fltrnField1 for numeric vs.
fltrtField2 for text, extracting that with Mid(ControlName, 5, 1).
Again, not very neat, but probably preferable over the previous one.
I'll post back if I come up with any other idea.
HTH,
Nikos
Hi, Nikos,
I'm afraid i have a problem with your suggestion below:
Nikos Yannacopoulos said:
The new table, say tblQueryFilters has two fields, FormName and
WhereClause; for the particular combination, the values would be:
FormName: Form1 (or whatever)
WhereClause: " WHERE Field3 = " & Forms![Form1]![Control1]
& " AND Field4 = '" & Forms![Form1]![Control2] & "'"
This can't be inserted in a db field, can it? it's not a string.
Forms![Form1]![Control1] will not be evaluated.
Do you mean:
WhereClause: "WHERE Field3 = [Forms![Form1]![Control1] etc"
That way, you call your function as:
f("Query1", [Name])
([Name] will return the form's name if used in a control property in the
form design - provided you don't have a control by that name!)
and modify your function code to:
Public Function f(strQueryName As String, strFormName As String, ...)
Dim db As DAO.Database
Set db = CurrentDb
strSQL = db.QueryDefs(strQueryName).SQL
strWhere = DLookup("WhereClause", "tblQueryFilters", _
"FormName = '" & strFormName & "'")
strSQL = strSQL & strWhere
OK but then I'd get a long string, the same that I have now, with references
to the form is the string, but these won't be evaluated, back to square one
r= CurrentDb.OpenRecordset(strSql)
Or did I miss something?