Printer Dialog and Parameters

I have a VB6 application that calls a Crystal Reports 9.0 viewer dll.  I currently have a form with a viewer control (named crviewer1) which succesfully opens and displays the report.

My users need to be able to print the report, but when doing so they need the print dialog box to appear so they may select a printer, number of copies, page range, etc.  Currently I have most of the functionality I need (see code below) but the user is prompted for the printer selection and the page range in 2 separate dialog boxes.  Also, if they cancel the first dialog, they still receive the second - rather unprofessional.

The larger problem is that many of the reports contain parameters.  When the user clicks print the first time they are prompted for the parameters again (this one confuses the users and they tend to enter the incorrect parameter values - go figure).  According to users, this only happens the first time though, not subsequent times even if the parameters have been changed (and even supposedly if they are looking at a different report, as long as they have not closed the application).

Basically, what is the correct way to print the displayed report (with the current parameters) and give the users full printer dialog control???

I can supply the full project file if it would help.  The entire fnuction of the application is to be a small, compact but full featured crystal report viewer application only.  I have a different screen that displays a list of known reports populated from a DB file.  Once the user selects a report, they can open the viewer form.  

Jeff Mitchell

Code modified frmo Crystal examples:

Dim CRAppl As New CRaxdrt.Application
Dim CRRep As CRaxdrt.Report

Private Sub Form_Load()
  Set CRRep = CRAppl.OpenReport(frm_ReportSelection.Text4.Text)
'The following sets the database location for all tables that are
'used in the report

'Where i is a Long that will be used to increment in a For..Next loop
'depending on the number of tables that exist in the report
Dim i As Long

'From the Report object CrystalReport, we take the current database used
'with the report and assigns it to DbName using the Database property
Set DbName = CRRep.Database

'From the Database object DbName, we take all the tables from the database
'used with the report and assign it to DbTables using the Tables property
Set DbTables = DbName.Tables
Set DbTable = DbTables.Item(1)

frm_ReportSelection.txt_DllName.Text = DbTable.DllName 'gets the DLL name currently in use in the report

'Combo2.AddItem DbTable.LogOnServerName 'gets the Server name currently in use
frm_ReportSelection.txt_ServerName.Text = DbTable.LogOnServerName
'If the original datasource is used, LogOnServer can be used since it just opens up the connection
'and Crystal uses the original connection

If frm_ReportSelection.Check1.Value = 1 Then
    'CrystalApplication.LogOnServer txt_DllName.Text, txt_ServerName.Text, Text1.Text, Text2.Text, Text3.Text
    For i = 1 To DbTables.Count
        'From the DatabaseTables object DbTables, we extract one item (the first table)
        'using the Item property (in CRW, the tables would be listed in order in
        'Database|Set Location)
        Set DbTable = DbTables.Item(i)
        'The SetLogOnInfo method opens up a connection to the database and forces the report
        'to use this connection instead of the one specified in the report. It is then useful
        'if you want to change location of the database for the report. You must set this
        'method for each table. If you want to use the same connection as specified in the report,
        'you can also use the LogOnServer method
        DbTable.SetLogOnInfo frm_ReportSelection.txt_ServerName.Text, , frm_ReportSelection.txt_UserID.Text, frm_ReportSelection.txt_Password.Text
        Next i
End If
Screen.MousePointer = vbHourglass
CRViewer1.ReportSource = CRRep
CRViewer1.DisplayTabs = True
CRViewer1.EnableDrillDown = True
CRViewer1.EnableGroupTree = True
CRViewer1.EnableHelpButton = False
CRViewer1.EnablePopupMenu = False
CRViewer1.EnableSelectExpertButton = True
CRViewer1.EnableExportButton = True
CRViewer1.EnableNavigationControls = True
CRViewer1.EnablePrintButton = True
CRViewer1.EnableSearchControl = True
CRViewer1.EnableSearchExpertButton = True
CRViewer1.EnableZoomControl = True
CRViewer1.EnableStopButton = True


CRRep.EnableParameterPrompting = True
'CRRep.VerifyOnEveryPrint = False

Screen.MousePointer = vbDefault
End Sub

Private Sub CRViewer1_PrintButtonClicked(UseDefault As Boolean)

'Create an instance of the Printer Setup dialog box
'Set the handle of the Printer Setup dialog box to the form
CRRep.PrinterSetup frmCrViewer.hWnd

'The 'Print' dialog box will appear by default
'The 'Print' dialog box needs to be displayed if you want the user
'to set start page and stop page, the number of copies, and collate
End Sub

Private Sub Form_Resize()
   CRViewer1.Top = 0
   CRViewer1.Left = 0
   CRViewer1.Height = ScaleHeight
   CRViewer1.Width = ScaleWidth
End Sub
Who is Participating?
AzraSoundConnect With a Mentor Commented:
I use CR 8.0 but I always did the print dialog myself using the common dialog control, e.g.,

Private Sub CRViewer1_PrintButtonClicked(UseDefault As Boolean)
    UseDefault = False
    On Error GoTo Cancel
    CommonDialog1.CancelError = True
    CommonDialog1.Flags = cdlPDPrintSetup
    Report.PrintOut False
    Exit Sub
    If Err.Number <> cdlCancel Then
        MsgBox Err.Number & " : " & Err.Description, vbExclamation + vbOKOnly, _
    End If
End Sub

With that being said, it may be as simple as you setting the UseDefault parameter to False (i notice that difference in our two code segments).  I can't honestly say I remember what that parameter was for...its been awhile.   :-)
jmitchell13760Author Commented:
Ok, getting closer, I added a commondialog control and used your code, but I still don't have the option of selecting page ranges to print.

I removed the cdlpdprintsetup flag which gives me a little better print dialog - it includes the number of copies and page range, but the option is just greyed out (and this could be a Crystal problem or something since it looks like I should be able to) - .  I tried playing with cdlpdallpages and such optinos but have not been able to get the pages option enabled - any ideas?

You need to set the Min, Max, FromPage, and ToPage values first, e.g.,

With CommonDialog1
    .Flags = cdlPDPageNums
    .Min = 1
    .Max = 50
    .FromPage = 1
    .ToPage = 50
End With
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

jmitchell13760Author Commented:
That seemed to resolve my problems.  Thank you very much for your assistance.
Glad I could help  :-)
jmitchell13760Author Commented:
OK, new problem with this solutino, which appears to be a common one - I am using the commondialog control but it changes the windows default printer to whatever printer was chosen for that print job.  I know I am supposed to be able to change the printerdefault property to false to stop that behavior, but when I do that, it ignores what printer I select and sends the job to the windows default printer.

I could probably get around this (messy though) by using some other code I have to set the window default, ect.

The second problem i have with this solution is that many of my reports have on-demand subreports which I need to be able to print.  The commondialog solution will not allow me to print subreports - when I try, I get the parent report.

So, back to my original question, I either need to be able to supress the parameter prompt when using crystal print code, or print subreports and not change the wnidows default prniter when using the commondialog.

Thanks again for any help.
>>I know I am supposed to be able to change the printerdefault property to false to stop that behavior

This should work.  Are you setting the PrinterDefault property to false BEFORE you call the ShowPrinter method?  If not, you need to be.

>>The commondialog solution will not allow me to print subreports - when I try, I get the parent report

This should have nothing to do with the common dialog control.  Once you have opened the main report and its subreports and told Crystal to process them, they should print out.  Perhaps its an issue with how you are opening your subreports?
jmitchell13760Author Commented:
I never was able to exactly get this working, but found a completely different way of accomplishing the report printer.  Thanks for all your help and time AzraSound.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.