View Full Version : DoCmd.OpenReport Where Clause


DoubleD
05-27-2009, 12:45 PM
I'm having a problem using VBA to open a report. My code uses a passed OpenArg variable to select records but it isn't working. My code is:

DoCmd.OpenReport DocName, acViewPreview, , "tblcustcall.[ExaminerID]= Arg2", acWindowNormal

No matter how I format the string the variable Arg2 is not assigned to the field [ExaminerID]. The debugger shows the correct value for Arg2. If I change Arg2 to the text DDOS2 the report is properly filtered.:mad: Anyhelp on the formatting would be appreciated.

SOS
05-27-2009, 12:50 PM
DoCmd.OpenReport DocName, acViewPreview, , "[ExaminerID]='" & Arg2 & "'"

DoubleD
05-27-2009, 12:54 PM
Thanks you are correct. Any explanation on the formatting? I've gone nuts trying to figure out the format. :)

SOS
05-27-2009, 12:59 PM
Thanks you are correct. Any explanation on the formatting? I've gone nuts trying to figure out the format. :)
When you are looking for values from

1. A Text Field - surround with quotes (single quotes, triple double quotes or Chr(34) will do).
DoCmd.OpenReport DocName, acViewPreview, , "[ExaminerID]='" & Arg2 & "'"
OR
DoCmd.OpenReport DocName, acViewPreview, , "[ExaminerID]=" & """ & Arg2 & """
OR
DoCmd.OpenReport DocName, acViewPreview, , "[ExaminerID]=" Chr(34) & Arg2 & Chr(34)

2. A Date Field - Surround with octothorpes (#)
DoCmd.OpenReport DocName, acViewPreview, , "[DateFieldName]=#" & Arg2 & "#"

3. A numeric Field - do not use anything.
DoCmd.OpenReport DocName, acViewPreview, , "[NumericFieldName]=" & Arg2