Open Report in Print Preview (2 Viewers)

Tophan

Registered User.
Local time
Today, 17:30
Joined
Mar 27, 2011
Messages
402
Hi,

I have a hyperlink on a form to open a report. The below code is what I entered in the OnClick event on the form. I also have the default view for the report set to Print Preview, yet the report opens in Report View. Please help.


Code:
Private Sub prntRevHistory_Click()
Dim strWhere As String

    If Me.Dirty Then    'Save any edits.
        Me.Dirty = False
    End If

    If Me.NewRecord Then 'Check there is a record to print
        MsgBox "Select a record to print"
    Else
        strWhere = "[ProjectID] = """ & Me.[ProjectID] & """"
        strWhere = "[DwgNo] = """ & Me.[DwgNo] & """"
        DoCmd.OpenReport "rptRevHistory", acViewPreview, , strWhere
    End If
End Sub
 
Are you sure this code runs? This line...
DoCmd.OpenReport "rptRevHistory", acViewPreview, , strWhere
...explicitly opens the report in in print preview (because you've used the acViewPreview switch, which overrides the default view). If it is opening in report view, it seems possible the HyperlinkAddress property of the hyperlink might be opening the report first.

I would run this code from a command button...
Code:
Private Sub prntRevHistory_Click()
    Dim strWhere As String

    If Me.NewRecord Then 'Check there is a record to print
        MsgBox "Select a record to print"
    Else
        Me.Dirty = False    ' this doesn't need an if block
        strWhere = "ProjectID = " & Me.[ProjectID] & "AND [DwgNo] = '" & Me.[DwgNo] & "'"
        DoCmd.OpenReport "rptRevHistory", acViewPreview, , strWhere
    End If
End Sub
 
Are you sure this code runs? This line...

...explicitly opens the report in in print preview (because you've used the acViewPreview switch, which overrides the default view). If it is opening in report view, it seems possible the HyperlinkAddress property of the hyperlink might be opening the report first.

I would run this code from a command button...
Code:
Private Sub prntRevHistory_Click()
    Dim strWhere As String

    If Me.NewRecord Then 'Check there is a record to print
        MsgBox "Select a record to print"
    Else
        Me.Dirty = False    ' this doesn't need an if block
        strWhere = "ProjectID = " & Me.[ProjectID] & "AND [DwgNo] = '" & Me.[DwgNo] & "'"
        DoCmd.OpenReport "rptRevHistory", acViewPreview, , strWhere
    End If
End Sub
Hi,

I tried the code you provided but when I run it I am being prompted to enter the ProjectID and then the report still opens in report view instead of print preview.
 
Maybe post your database. I am curious to see code that opens a report in ReportView when called using acViewPreview. I have never seen such a blatant error in Access before.
 
> strWhere = "[ProjectID] = """ & Me.[ProjectID] & """"
> strWhere = "[DwgNo] = """ & Me.[DwgNo] & """"
I'm curious:
1: Is ProjectID indeed alphanumeric?
2: Is DwgNo indeed alphanumeric?
3: Did you not want to filter by ProjectID, but only by DwgNo?
 
> strWhere = "[ProjectID] = """ & Me.[ProjectID] & """"
> strWhere = "[DwgNo] = """ & Me.[DwgNo] & """"
I'm curious:
1: Is ProjectID indeed alphanumeric?
2: Is DwgNo indeed alphanumeric?
3: Did you not want to filter by ProjectID, but only by DwgNo?
Hi Tom,

The ProjectID is an autonumber which I have formatted as "P- "000 to distinguish it from any other autonumber fields I may have in other tables.
The DwgNo is alphanumeric and is noted as shown on the drawings.
Ideally, I would like to sort by project but right now I only have one project I'm using this DB for.

The code to run the report is working - I just can't figure out why it's opening in report view instead of print preview even though I have the default view on the report set to print preview and also in the code set to acViewPreview.

It's just a preference of mine to open reports in print preview.
 
Open the immediate window (press Ctrl+G) and enter:
Code:
? acviewpreview
This should return the value 2. acViewReport is 5
 
Do you get the same view if you use similar code with another report? Do you get the same view in a new Access file?
 
Do you get the same view if you use similar code with another report? Do you get the same view in a new Access file?
I had copied this code from another database I did years ago and that report opens in print preview 🤷‍♀️ maybe it's just a glitch
 
Try change your code to acViewPrint to see if there is a difference.
 
Is the report set to open on a default printer which isn't available on that machine?
 
Hi Tom,

The ProjectID is an autonumber which I have formatted as "P- "000 to distinguish it from any other autonumber fields I may have in other tables.
The DwgNo is alphanumeric and is noted as shown on the drawings.
Ideally, I would like to sort by project but right now I only have one project I'm using this DB for.

The code to run the report is working - I just can't figure out why it's opening in report view instead of print preview even though I have the default view on the report set to print preview and also in the code set to acViewPreview.

It's just a preference of mine to open reports in print preview.
Thanks for these answers. We know that this line:
strWhere = "[ProjectID] = """ & Me.[ProjectID] & """"
is currently not being used, but if you were using it in the future: the Autonumber value should not be wrapped in double-quotes. How you have formatted the field is of no concern: that is just eye candy for humans; underneath it's still a long integer, so that line should be:
strWhere = "[ProjectID] = " & Me.[ProjectID]
 
Thanks for these answers. We know that this line:
strWhere = "[ProjectID] = """ & Me.[ProjectID] & """"
is currently not being used, but if you were using it in the future: the Autonumber value should not be wrapped in double-quotes. How you have formatted the field is of no concern: that is just eye candy for humans; underneath it's still a long integer, so that line should be:
strWhere = "[ProjectID] = " & Me.[ProjectID]
Ah...ok. Thank you - understood. I will make the adjustment in the event I use this DB for another project.
 
My default printer is print to pdf so changing to acViewPrint is prompting me to save the file
Just returning to my question in post #12, even though your default Windows printer is Print to PDF, individual reports can still have a different printer specified. That can cause problems when the specified printer isn’t available. Whether it might explain opening in report view is another question.
 

Users who are viewing this thread

Back
Top Bottom