Steve
Steve

Reputation: 31

Print Dialog in MS-Access

I want to open a print dialog box. I haven't programmed in VB for a decade and I am more than a little rusty.

I got a copy of an MS Access VB script that selects an MDB file and then prints only one copy. My thought process was:

  1. Open up a dialog box;
  2. Select the printer;
  3. Type in number of copies;
  4. Print.

Currently, the portion of the original script I want to modify is:

[SQL query above this]
....

' Open form
Dim rs As DAO.Recordset
Set rs = db.OpenRecordset("select@@identity")
Debug.Print rs(0)

Dim q As String
q = "transfer_id=" & rs(0)
DoCmd.OpenForm "Transfer Report", acNormal, , q, ,acDialog
DoCmd.PrintOut

...
[End If, End Sub, etc.]

This only prints out one instance of the report and the company does not have the ability to print out other copies at the same time. Since it is in the field, they don't have access to a copier but still need multiple copies.

One answer I found, by David-W-Fenton (thank you!), shows how to create a dialog box; Would the following script accomplish what I want to do? And, how do I add a portion to the dialog box to specify how many copies to print?

...  
Dim varPrinter As Printer
Dim strRowsource As String
Dim q As String
Dim rs As DAO.Recordset

Set rs = db.OpenRecordset("select @@identity")
Debug.Print rs(0)

If Len(Me.OpenArgs) > 0 Then
  q = Me.OpenArgs
  Me.Tag = q
  For Each varPrinter In Application.Printers
   strRowsource = strRowsource & "; " & varPrinter.DeviceName
  Next varPrinter
  Me!cmbPrinter.RowSource = Mid(strRowsource, 3)
  ' first check to see that the report is still open
If (1 = SysCmd(acSysCmdGetObjectState, acReport, q)) Then
   With Reports(q).Printer
     Me!cmbPrinter = .DeviceName
     Me!optLayout = .Orientation
     End With
    Me!txtPageTo = Reports(q).Pages
  End If
End If

Public Function PrintReport(q As String) As Boolean
  q = "transfer_id=" & rs(0)
 ' open report in PREVIEW mode but HIDDEN
 DoCmd.OpenReport q, acViewPreview, , , acHidden
 ' open the dialog form to let the user choose printing options
 DoCmd.OpenForm "dlgPrinter", , , , , acDialog, q
 With Forms!dlgPrinter
   If .Tag <> "Cancel" Then
    Set Reports(q).Printer = Application.Printers((!cmbPrinter))
    Reports(q).Printer.Orientation = !optLayout
    Application.Echo False
    DoCmd.SelectObject acReport, q
    DoCmd.PrintOut acPages, !txtPageFrom, !txtPageTo
    PrintReport = True
   End If
 End With
 DoCmd.Close acForm, "dlgPrinter"
 DoCmd.Close acReport, q
 Application.Echo True
End Function

Upvotes: 1

Views: 20644

Answers (2)

skmbdev
skmbdev

Reputation: 51

Here is a late reply, some times it may help others.

In order to open the print dialogue:

Place a command button for PRINT on the report, and write the code as below.

Private Sub CmdbtnPrint_Click()
DoCmd.RunCommand acCmdPrint
End Sub

Change the 'Display When' property of button to 'Screen Only'.

Open the report in 'acViewReport' view mode.

Click the PRINT command button, and the systems print dialogue box will be open. Set the parameters and print the report.

Upvotes: 5

David Zemens
David Zemens

Reputation: 53623

Check the documentation about the PrintOut method:

http://msdn.microsoft.com/en-us/library/office/ff192667.aspx

In your original script to modify, just add the Copies argument to the DoCmd.PrintOut statement, like:

DoCmd.PrintOut Copies:=x

where x is a variable representing the number of copies you want to print.

Reviewing the documentation for the DoCmd.OpenForm method,

http://msdn.microsoft.com/en-us/library/office/ff820845.aspx

I don't think the code you have can allow a user-input for the number of copies. I do not program in Access, but I'm certain there are other ways to allow user input at run-time. In Excel/PowerPoint we would use an InputBox or a UserForm.

I think you can use an InputBox in Access:

http://office.microsoft.com/en-us/access-help/inputbox-function-HA001228856.aspx

Upvotes: 0

Related Questions