vba MS-Access 中的打印对话框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17712679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:17:43  来源:igfitidea点击:

Print Dialog in MS-Access

vbams-accessdialog

提问by Steve

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.

我想打开一个打印对话框。我已经十年没有用 VB 编程了,而且我有点生疏了。

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:

我得到了一个 MS Access VB 脚本的副本,该脚本选择一个 MDB 文件,然后只打印一个副本。我的思考过程是:

  1. Open up a dialog box;
  2. Select the printer;
  3. Type in number of copies;
  4. Print.
  1. 打开一个对话框;
  2. 选择打印机;
  3. 输入份数;
  4. 打印。

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?

我找到的一个答案是 David-W-Fenton(谢谢!),展示了如何创建对话框;以下脚本会完成我想做的事情吗?而且,如何向对话框添加一部分以指定要打印的份数?

...  
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

回答by skmbdev

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.

在报表上放置一个 PRINT 命令按钮,并编写如下代码。

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.

在“acViewReport”查看模式下打开报告。

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

单击打印命令按钮,系统打印对话框将被打开。设置参数并打印报告。

回答by David Zemens

Check the documentation about the PrintOutmethod:

检查有关该PrintOut方法的文档:

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

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

In your original script to modify, just add the Copiesargument to the DoCmd.PrintOutstatement, like:

在要修改的原始脚本中,只需将Copies参数添加到DoCmd.PrintOut语句中,例如:

DoCmd.PrintOut Copies:=x

DoCmd.PrintOut Copies:=x

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

其中x是一个变量,表示要打印的份数。

Reviewing the documentation for the DoCmd.OpenFormmethod,

查看该DoCmd.OpenForm方法的文档,

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

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 certainthere are other ways to allow user input at run-time. In Excel/PowerPoint we would use an InputBox or a UserForm.

我认为您拥有的代码不允许用户输入副本数量。我不在 Access 中编程,但我确信还有其他方法可以在运行时允许用户输入。在 Excel/PowerPoint 中,我们将使用 InputBox 或 UserForm。

I think you can use an InputBox in Access:

我认为您可以在 Access 中使用 InputBox:

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

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