Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mysterious Printing of Report 3

Status
Not open for further replies.

khwaja

Technical User
Aug 27, 2001
431
AU
I have a form designed to create a purchase order. I saved it as a report in order for the user to print the same invoice when ready by using a command button provided on the form. I used the folowing code to link the form and the report.

Private Sub CommandPO_Click()
On Error GoTo Err_CommandPO_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "rptPO"

stLinkCriteria = "[POFormID]=" & Me![POFormID]
DoCmd.OpenReport stDocName, , , stLinkCriteria

Exit_CommandPO_Click:
Exit Sub

Err_CommandPO_Click:
MsgBox Err.Description
Resume Exit_CommandPO_Click

End Sub

I thought that this code will first open the purchase order saved as a report and then user will need to use menu or a command button on the report to print the matching invoice. But somehow, this invoice is printed straightaway without opening the report. Frankly, I couldn't ask for more but I would like to learn what is really triggering the printing process so that I can use the same technique in other contexts. Could someone shed light on why a command button on a form directly prints the report even though I have no such code in the report. Saving a form as report may have something to do this. I am using Access 97.

Cheers
 
DoCmd.OpenForm stDocName, , , stLinkCriteria 'will open the form
DoCmd.OpenReport stDocName, , , stLinkCriteria 'will print the report
DoCmd.OpenReport stDocName,acPreview , , stLinkCriteria ' will open the report for user view

Herman
 
Thanks Herman. Appreciate your help. What would be the code for user to print after the last statement which may either bring up the print menu or send the report for printing. User has run-time access, so limited menus are available.

Kind regards
 
This wil open the report in preview mode then bring up the print dialog so they can choose a printer and set other options. When the click the OK button the report will print and then close.

DoCmd.OpenReport stDocName, acViewPreview, , stLinkCriteria
DoCmd.RunCommand acCmdPrint
DoCmd.Close acReport, stDocName

In your error handling you will need trap error # 2501. This will happen if the user clicks the Cancel button on the print dialog. Something like below will work.

***********************************************************
Select Case Err.Number

Case 2501
DoCmd.Close acReport, stDocName
Exit Sub
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description"

End Select
***********************************************************

This works in the runtime version as well.

I hope this helps.

Dermot
 
In my apps I useally have a toolbar with a printer and report page setup icon, among others like sort a..z/z..a, etc.
This enables the end user, in a proff way, to print whatever he/she wants and when it also enables, same, to change margens, selece special printer etc. again in a proff way.
All the best
Herman
 
Laois

Sorry to be back but thought you might be able help. As a result of this code, I am getting a blank page of the report in the preview mode. Any idea why is that? Things seem OK until I get to following line.

DoCmd.OpenReport stDocName, acViewPreview, , stLinkCriteria

Msg box line was also showing error so I commented it out.

Will appreciate if you could help.

Cheers



My full code is as under:
DoCmd.RunCommand acCmdPrint
'DoCmd.Close acReport, stDocName

Exit_CmdPrintCurrent_Click:
Exit Sub

Err_CmdPrintCurrent_Click:

Select Case Err.Number

Case 2501
DoCmd.Close acReport, stDocName
Exit Sub
'Case Else
' MsgBox "Error " &Err.Number& ": " & Err.Description"

End Select

Resume Exit_CmdPrintCurrent_Click

End Sub
 
Hi,
Are you sure that your report contains data? If it doesnot, it just displays a blank page. To rectify this, you can try OnNoData event of the report in question. Set its Cancel property to true 0r 1 as required.

Hope it helps. Let me know what happens.
With regards,
PGK
 
Thanks PGK. If you have noticed, this report is being printed based on a record you are currently on in a form. So it could not be blank report as long as you are on record in form which is already saved. Any further insights?

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top