我目前有一个Access 2003数据库,它有一个表单,可根据用户选择的工作打印一系列报告。该查询也在VBA代码中,查看作业的编目部分并打开相应的报告。
以下是正确执行此操作的代码:
Private Sub Print_Report_Click()
Dim default_cat As Dao.Database
Dim d As Dao.Recordset
Dim q As String
Set default_cat = CurrentDb
q = "SELECT DISTINCT CATALOG, USER3 FROM [_MASTER_UPLOAD] WHERE SerialNumber='" & Me.[SerialNumberSelection] & "'"
Set d = default_cat.OpenRecordset(q, dbOpenDynaset)
d.MoveFirst
DoCmd.RunCommand acCmdPrint
Do While Not d.EOF
Select Case d!USER3
Case "COM"
DoCmd.OpenReport "RPTCompressor", , , "CATALOG = '" & d!CATALOG & "'"
Case "CON"
DoCmd.OpenReport "RPTCondenser", , , "CATALOG = '" & d!CATALOG & "'"
Case "CRV"
DoCmd.OpenReport "RPTCapacityRegValve", , , "CATALOG = '" & d!CATALOG & "'"
Case "CV"
DoCmd.OpenReport "RPTCheckValve", , , "CATALOG = '" & d!CATALOG & "'"
Case "etc..."
End Select
d.MoveNext
Loop
d.Close
End Sub
基本上我现在的问题在于打印机选择DoCmd.RunCommand acCmdPrint
。弹出窗口出现,我可以选择我想要的任何打印机,但是该选择似乎只影响该组的第一个报告(所有其他报告都转到计算机的默认打印机)。最有可能的是,我会从我们的打印室打印机打印报告,或者只是将它们转换为PDF,但这也意味着我无法对其进行硬编码以始终打印到特定位置。
如何让我的打印机选择对所有报告保持有效,最好不必为同一组中的每个报告运行acCmdPrint命令?
提前致谢。
修改
此问题已在此处指定并继续:How to pass reference to combobox?
答案 0 :(得分:1)
从谷歌搜索的第一个结果access vba select printer(只是说):
如果您的报告设置为在默认打印机上打印,最简单的方法是临时更改默认打印机:
Set Application.Printer = Application.Printers("myPrinterDeviceName")
DoCmd.OpenReport "FirstReport"
DoCmd.OpenReport "SecondReport"
' ...
Set Application.Printer = Nothing
如果没有,您可以按报告执行此操作:
DoCmd.OpenReport "FirstReport", View:=acPreview, WindowMode:=acHidden
Set Reports("FirstReport").Printer = Application.Printers("myPrinterDeviceName")
DoCmd.OpenReport "FirstReport", View:=acViewNormal
链接页面还有一个示例,说明如何显示可用打印机列表以选择一个。
Private Sub GetPrinterList(ctl As Control)
Dim prt As Printer
For Each prt In Printers
ctl.AddItem prt.DeviceName
Next prt
ctl = Application.Printer.DeviceName
End Sub