Print Preview a Report Button

  • Thread starter Thread starter Keith
  • Start date Start date
K

Keith

I added a Print Preview button with the wizard and modified it so that it
would only print the report for the record I was on, but I am having
problems.

My code is below.

The field containing the unique value of the record is
Code:
 and the first
record in the database (for example) contains the value A001 in this field.

However, when I click Print Preview, it asks for a parameter for A001 and I
have to enter the code I want to view the report for.

Why would this be?
 
The issue could be that your primary key field is Text (not number), so
extra quote marks are needed as the delimiter:

Private Sub cmdPreview_Click()
Dim strWhere As String

If Me.Dirty Then 'Save first.
Me.Dirty = False
End If
If Me.NewRecord Then
MsgBox "Select a record to print"
Else
strWhere = "Code = """ & Me.Code & """"
DoCmd.OpenReport "MyReport", acViewPreview, , strWhere
End If
End Sub
 
Thank you for your help

Allen Browne said:
The issue could be that your primary key field is Text (not number), so
extra quote marks are needed as the delimiter:

Private Sub cmdPreview_Click()
Dim strWhere As String

If Me.Dirty Then 'Save first.
Me.Dirty = False
End If
If Me.NewRecord Then
MsgBox "Select a record to print"
Else
strWhere = "Code = """ & Me.Code & """"
DoCmd.OpenReport "MyReport", acViewPreview, , strWhere
End If
End Sub

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Keith said:
I added a Print Preview button with the wizard and modified it so that it
would only print the report for the record I was on, but I am having
problems.

My code is below.

The field containing the unique value of the record is
Code:
 and the
first
record in the database (for example) contains the value A001 in this
field.

However, when I click Print Preview, it asks for a parameter for A001 and
I
have to enter the code I want to view the report for.

Why would this be?[/QUOTE]
[/QUOTE]
 
Back
Top