can't filter report open/preview from form

  • Thread starter Thread starter guilin
  • Start date Start date
G

guilin

I have struggled on this for about a week.........

Have a command button to open/preview report from a form. I want it to
open the report for the current report. I used:

********Code start*********

Private Sub cmdPreviewInvoice_Click()
On Error GoTo Err_cmdPreviewInvoice_Click

Dim stDocName As String

stDocName = "rptPrintInvoice"
DoCmd.OpenReport stDocName, acPreview, "", InvoiceID =
Me.InvoiceID, acWindowNormal

Exit_cmdPreviewInvoice_Click:
Exit Sub

Err_cmdPreviewInvoice_Click:
MsgBox Err.Description
Resume Exit_cmdPreviewInvoice_Click

End Sub

*******Code end***********

which opened the whole report without filtering. there's no error msg
Thanks for any advice!!!
 
The WhereCondition must be a string made up of the field name with the value
concatenated:
DoCmd.OpenReport stDocName, acViewPreview, , "InvoiceID = " &
Me.InvoiceID

Without the quotes, if the form is on record 99, then what you previously
had becomes:
99 = 99
and that statement is true for all records.
 
Put the "Where Condition" in quotes. Also, you don't need the null string ("") in the placeholder after the "acPreview" clause; just the commas are enough. By the same token, the "acWindowNormal" at the end is the default, so you really don't need to include it.

DoCmd.OpenReport stDocName, acPreview, "", InvoiceID = Me.InvoiceID, acWindowNormal

should be

DoCmd.OpenReport stDocName, acPreview, , "[InvoiceID] = " & Me.InvoiceID
 
Back
Top