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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open Print window 3

Status
Not open for further replies.

ailyn

Programmer
Sep 15, 2005
108
BE
I'm trying to open the print option on a repport. I want to allow the user to choose the printer so I just need the print window to open when the report opens. How can I do this? PrintOut prints it directly and that is not what I need.
 
Hi
Do you mean

DoCmd.OpenReport "ReportName", acViewPreview
 
No, I meant the window with the printers that appears when you go to File/print in acces, word, etc. The window where you select the printer, how many pages, copies,... the window window not the access window.
 
How about:
Code:
DoCmd.OpenReport "Report1", acViewPreview
DoEvents
DoCmd.RunCommand acCmdPrint
Or were you looking for something different?
 
One way is to put a line of code in the Open Event of the report. This will give you the print dialog. You need to add some error handler to catch cancel error
Code:
Private Sub Report_Open(Cancel As Integer)
    DoCmd.RunCommand acCmdPrint
End Sub

________________________________________________________
Zameer Abdulla
Help to find Missing people
My father was a realistic father; not a vending machine dispense everything I demanded for!!
 
Yes, that is precisely what I needed. Thanks a lot!
The only problem now is that I can not print because I have another event running. this is my code for the window (thanks to you guys)

Private Sub Report_Open(Cancel As Integer)
On Error GoTo Report_Open_Error
If MsgBox("Print now?", vbYesNo) = vbYes Then
DoCmd.RunCommand acCmdPrint
End If

Report_Open_Exit:
Exit Sub

Report_Open_Error:
MsgBox Err.Description
Resume Report_Open_Exit
End Sub

& this is the code other code that is running on the same form:

Option Explicit

Private shadeNextRow As Boolean
Const shadedColor = 12632256
' Const shadedColor = 15726583 ' alternative shade colors
' Const shadedColor = 14078404
' Const shadedColor = 13356495
' Const shadedColor = 14281974
Const normalColor = 16777215

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

On Error GoTo Detail_Format_Error

' Choose a color based on the shadeNextRow value
If shadeNextRow = True Then
Me.Section(acDetail).BackColor = shadedColor
Else
Me.Section(acDetail).BackColor = normalColor
End If

' Switch the color for the next row
shadeNextRow = Not shadeNextRow

Detail_Format_Exit:
Exit Sub

Detail_Format_Error:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume Detail_Format_Exit

End Sub

I gor it here in tek-tips and it's to shade uneven lines in the report. Is there no way to avoid this problem? I have other reports with other events that I'm sure will give me more trouble, how could I avoid this?
 
The only solution I can see is to take acCmdPrint out of the report open event and put in a sub:
Code:
Sub OpenTheReport()
DoCmd.OpenReport "Report1", acViewPreview
DoEvents
DoCmd.RunCommand acCmdPrint
End Sub
But there may well be other (and better) solutions.
 
How about this...in the form you are calling the report from do this:
(untested, but should work)

Code:
sub OpenTheReport()
dim ReportName as string
ReportName = "MyReport"

Docmd.OpenReport ReportName, acViewPreview

' Wait until report is opened
do until (SysCmd(acSysCmdGetObjectState, acReport, ReportName) = 0)

  DoEvents
Loop

DoCmd.RunCommand acCmdPrint



Ray D'Andrade
 
Thanks Remou, but how do I call it afterwards? I tried this:

Private Sub Report_Open()
On Error GoTo Report_Open_Error

If MsgBox("Print now?", vbYesNo) = vbYes Then

End If

Exit_Report_Open:
Exit Sub

Report_Open_Error:
MsgBox Err.Description
Resume Report_Open_Error

End Sub

But it didn't work.

And thanks to you too Ray, but the problem with that option is that I open the report by macro 'OpenReport' and then I can apply a filter to it (I have different buttons in the form too -Sold, Not sold,...).
So I tried adding an open module there instead and put our code as the module. There was an error still with the acCmdPrint.
 
ailyn
Both Ray's post and mine are about the same thing, except Ray's is better. Put the code here:
Code:
If MsgBox("Print now?", vbYesNo) = vbYes Then
[blue]Docmd.OpenReport ReportName, acViewPreview

' Wait until report is opened
do until (SysCmd(acSysCmdGetObjectState, acReport, ReportName) = 0)

  DoEvents
Loop

DoCmd.RunCommand acCmdPrint
[/blue]    
End If

Not forgetting the Dim statement and name for the report:
Code:
[blue]Function[/blue] Report_Open()
On Error GoTo Report_Open_Error
[blue]dim ReportName as string
ReportName = "MyReport"[/blue]

Note that I have changed Private Sub to Function (don't forget End Function), which means that you can call the whole thing from a macro (RunCode).
 
Sorry I forgot to put 'OpenTheReport' inside the If on my prev post. I had it as a Sub.

Well I tried what you told me now, Remou, but whithin the Private Sub Report_Open(). I tried it and no luck. My db blocks completely and the form doesn't even show. I cannot close it and I have to close access instead. I think the loop may be doing something wrong.
and for what you told me, should I make a function instead? If I do, where do I call it? in Report_Open event?
I'm a bit lost now.
 
ok, now I tried Ray's code in a function and called the function in the Report_Open event within the If of the MsgBox and it showed the MsgBox. I clicked yes. It opened the printer's window I selected one and clicked ok. And it gave me the same error that while there is an event I can't print or something like that. Then I clicked ok in the error window & I kept getting error windows constantly, everything blocks with them.
 
Sounds nerve-racking! Let's start again. What you need is a module containg a function called, say, OpenTheReport. I made a mistake with the function name above by cutting and pasting from your post, it should not be part of the code on your report, it should be in a module (do not give the module the same name as the function). Make sure you get rid of "DoCmd.RunCommand acCmdPrint" from your report, leaving just the original formatting code. I tried Ray's code, and found I had a problem with it, don't know why, whereas the code below works for me:
Code:
Function OpenTheReport()
Dim ReportName As String
ReportName = "Report Name"

If MsgBox("Print now?", vbYesNo) = vbYes Then
   DoCmd.OpenReport ReportName, acViewPreview
   DoEvents
   DoCmd.RunCommand acCmdPrint
End If
End Function
 
I just did it. I called the function of the module (names module2) in the Report_Open event. after selecting printing and clicking ok, it gives the error of report event... When I click ok it just shows the report. At least this time it doesn't loop to death, but it doesn't work either. Sorry!
couldn't it be something that is in the previous code?
(check my post of 8 Nov 05 10:40)
 
Ah, do not call it in the report open event, call it with a macro, or, for testing, just run it in the module and see if that works. Thank goodness the loop-of-death has gone!
 
Great!!! it works! I run the module from VB and it worked but how do I call it now when I open the form? I always open it from a macro where in the OpenForm I select one filter or another. I tried OpenModule but after printing it opens the Module window. I cannot do that, the user could destroy the code!
 
Great! Create a macro
Action : RunCode
FunctionName : OpenTheReport() Or what ever you called the function
Or add a button to your form:
Name : cmdPrint
OnClick : [Event Procedure]

This is the code for the on click event:
Code:
Private Sub cmdPrint_Click()
Call OpenTheReport   ' Or what ever name you used
End Sub

You can add error handling when you are happy.

(If you have 'dangerous' users, it might be best to protect your code.)

 
a small note to add..
You can convert the macro to code if you are working on Access2000+

TOOLS | MACRO | CONVERT MACRO...


________________________________________________________
Zameer Abdulla
Help to find Missing people
My father was a realistic father; not a vending machine dispense everything I demanded for!!
 
I have just noticed that we are in the wrong forum for this question [rednose]. There is a forum specifically for reports.
Microsoft: Access Reports Forum
forum703


 
Oops! Sorry for the misplacement of my thread. Just one more question can I open the form with different filters or do I have to make a module for each one of them (where I include the filter, obviously)? See, that is what I tried to explain on my last post. I open the report with different buttons, different filters.
But this code opens the report for me without a filter so, what should I do? Different modules?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top