Printing report
Hi,
I have a problem on printing a report.
I have a from where the user can choose wich report to print (this is a list box) and some other criteria like margins and number of copies. There is also a text box, where the printer to use is inserted by a lookup in a table (because I have different printers according to the report to print).
What I would have is that by pressing an action button 'print', the selected report prints whith the criteria on this form. So it has to take the printer that is on the form, the margins, and the number of copies. I do not want the report to be shown, only the rapport printer to the selected printer.
I know some of you will tell me to change the property in the report setting to the printer I want, but if in the future the printer to print on changes, I will have to go back to every report to change this. Know I have 3 different types of reports, and the form shows all the reports of the same report type. I will only have to change the value in the table where the report types and printers to print on are stored into... This is why I want it to work this way (if possible, but I guess yes)
I have written this code, but it is not working. Can anyone help me on this?
Private Sub cmdPrinten_Click()
On Error GoTo Err_cmdPrinten_Click
Dim strRapport As String
'check if everything has been filled in correctly
If Me.lstRapporten.ListIndex = -1 Then
MsgBox "Gelieve een rapport uit de lijst te kiezen." & vbCrLf & _
"Veuillez choisir un rapport dans la liste.", vbExclamation, "CobelAdmin"
Exit Sub
End If
If IsNull(Me.cboBegindatum) Or Me.cboBegindatum = "" Then
MsgBox "Gelieve een begindatum in te vullen." & vbCrLf & vbCrLf & _
"Veuillez entrer une date de départ.", vbExclamation, "CobelAdmin"
Me.cboBegindatum.SetFocus
Exit Sub
End If
If IsNull(Me.cboEinddatum) Or Me.cboEinddatum = "" Then
MsgBox "Gelieve een einddatum in te vullen." & vbCrLf & vbCrLf & _
"Veuillez entrer une date de fin.", vbExclamation, "CobelAdmin"
Me.cboEinddatum.SetFocus
Exit Sub
End If
If Me.cboEinddatum < Me.cboBegindatum Then
MsgBox "De einddatum bevindt zich voor de begindatum." & vbCrLf & _
"Gelieve dit te wijzigen." & vbCrLf & vbCrLf & _
"La date de fin se situe avant la date de départ." & vbCrLf & _
"Veuillez corriger.", vbCritical, "CobelAdmin"
Me.cboBegindatum = ""
Me.cboEinddatum = ""
Me.cboBegindatum.SetFocus
Exit Sub
End If
If IsNull(Me.txtAantalCopies) Or Me.txtAantalCopies = "" Then
MsgBox "Gelieve het aantal exemplaren in te vullen." & vbCrLf & vbCrLf & _
"Veuillez remplir le nombre d'exemplaires.", vbExclamation, "CobelAdmin"
Me.txtAantalCopies.SetFocus
Exit Sub
End If
If Me.txtAantalCopies <= 0 Then
MsgBox "Gelieve het aantal exemplaren minimum op 1 in te stellen." & vbCrLf & vbCrLf & _
"Veuillez regler le nombre d'exemplaires sur minimum 1.", vbExclamation, "CobelAdmin"
Me.txtAantalCopies = 1
Me.txtAantalCopies.SetFocus
Exit Sub
End If
'open the report to give the criteria to the report (printer,
'margins, number of copies), but first add 'rpt' back to the name
strRapport = Me.lstRapporten
strRapport = "rpt" & strRapport
DoCmd.OpenReport strRapport, acViewPreview
'give printer
Set Reports(strRapport).Printer = Application.Printers(Me.txtPrinter)
'give margins and number of copies
With Reports(strRapport).Printer
.TopMargin = Me.txtMargeBoven * 567
.BottomMargin = Me.txtMargeBeneden * 567
.LeftMargin = Me.txtMargeLinks * 567
.RightMargin = Me.txtMargeRechts * 567
.Copies = Nz(Me.txtAantalCopies, 1)
End With
'print the report
DoCmd.RunCommand acCmdPrint
'close the report
DoCmd.Close acReport, strRapport
DoCmd.Maximize
'empty boxes for possible next choise
Me.cboBegindatum = ""
Me.cboEinddatum = ""
Me.lstRapporten = ""
Exit_cmdPrinten_Click:
Exit Sub
Err_cmdPrinten_Click:
If Err.Number <> 2501 Then MsgBox Err.Description 'error no
'data on report
Resume Exit_cmdPrinten_Click
End Sub
Thanks...
|