当其他表单作为函数的一部分打开时,访问vba函数停止

时间:2016-01-07 08:28:19

标签: vba function ms-access access-vba

数据库打开时,我在表单加载时运行以下函数。我知道有些东西丢失但我不确定到底是什么。代码运行正常,直到它打开一个表单,以便用户可以选择一台打印机。然后表格有自己的作品通过。
我放弃了,代码在SelectPrinter子上停止,所以我想我需要该代码返回函数或者我可以将代码写入函数吗?

这是功能:

Option Compare Database
Function PrintReports()

Dim ExeCount As Long
Dim ExdCount As Long
Dim ExiCount As Long
Dim ExnCount As Long
Dim ExpCount As Long
Dim Answer As Integer
DoCmd.SetWarnings (WarningsOff)
'Create Ex e Table
DoCmd.RunSQL "SELECT tbl_AHAD.*, IIf([Last_Insp_Date] Is           Null,Date(),DateAdd('yyyy',4,[Last_Insp_Date])) 
AS Due_Date " & _  
"INTO tbl_Ex_e " & _
"FROM tbl_AHAD " & _
"WHERE (((IIf([Last_Insp_Date] Is Null,Date(),DateAdd('yyyy',4,      
[Last_Insp_Date])))<=Date()) " & _
"AND ((tbl_AHAD.Device_Class) In ('Ex e','Ex eb','Ex ed','Ex em','Ex    
emb','Ex mb','Ex mbe','Ex me')))" & _
"ORDER BY tbl_AHAD.Maint_Item"
'Create Ex d Table
DoCmd.RunSQL "SELECT tbl_AHAD.*, IIf([Last_Insp_Date] Is    
Null,Date(),DateAdd('yyyy',4,[Last_Insp_Date])) AS Due_Date " & _
"INTO tbl_Ex_d " & _
"FROM tbl_AHAD " & _
"WHERE (((IIf([Last_Insp_Date] Is Null,Date(),DateAdd('yyyy',4,  
[Last_Insp_Date])))<=Date()) " & _
"AND ((tbl_AHAD.Device_Class) In ('Class 1','Ex d','Ex de','Ex dmb')))" & _
"ORDER BY tbl_AHAD.Maint_Item"
'Create Ex i Table
DoCmd.RunSQL "SELECT tbl_AHAD.*, IIf([Last_Insp_Date] Is  
Null,Date(),DateAdd('yyyy',4,[Last_Insp_Date])) AS Due_Date " & _
"INTO tbl_Ex_i " & _
"FROM tbl_AHAD " & _
"WHERE (((IIf([Last_Insp_Date] Is Null,Date(),DateAdd('yyyy',4, 
[Last_Insp_Date])))<=Date()) " & _
"AND ((tbl_AHAD.Device_Class) In ('Ex i','Ex ia')))" & _
"ORDER BY tbl_AHAD.Maint_Item"
'Create Ex n Table
DoCmd.RunSQL "SELECT tbl_AHAD.*, IIf([Last_Insp_Date] Is 
Null,Date(),DateAdd('yyyy',4,[Last_Insp_Date])) AS Due_Date " & _
"INTO tbl_Ex_n " & _
"FROM tbl_AHAD " & _
"WHERE (((IIf([Last_Insp_Date] Is Null,Date(),DateAdd('yyyy',4,
[Last_Insp_Date])))<=Date()) " & _
"AND ((tbl_AHAD.Device_Class) In ('Ex n','Ex nA','Ex nR')))" & _
"ORDER BY tbl_AHAD.Maint_Item"
'Create Ex p Table
DoCmd.RunSQL "SELECT tbl_AHAD.*, IIf([Last_Insp_Date] Is   
Null,Date(),DateAdd('yyyy',4,[Last_Insp_Date])) AS Due_Date " & _
"INTO tbl_Ex_p " & _
"FROM tbl_AHAD " & _
"WHERE (((IIf([Last_Insp_Date] Is Null,Date(),DateAdd('yyyy',4,   
[Last_Insp_Date])))<=Date()) " & _
"AND ((tbl_AHAD.Device_Class) In ('Ex p'))) " & _
"ORDER BY tbl_AHAD.Maint_Item"

DoCmd.SetWarnings (WarningsOff)
'Open message box to ensure user wants to continue

ExeCount = DCount("ID", "tbl_Ex_e")
ExdCount = DCount("ID", "tbl_Ex_d")
ExiCount = DCount("ID", "tbl_Ex_i")
ExnCount = DCount("ID", "tbl_Ex_n")
ExpCount = DCount("ID", "tbl_Ex_p")

Answer = MsgBox("There are " & vbCrLf & vbCrLf & ExeCount & "   Ex e Reports   
" & vbCrLf & ExdCount & "   Ex d Reports " & vbCrLf & _
ExiCount & "   Ex i Reports" & vbCrLf & ExnCount & "   Ex n Reports" &  
vbCrLf & ExpCount & "   Ex p Reports" & _
vbCrLf & vbCrLf & "Records to Print", vbOKCancel)
'If Ok then print all reports
If Answer = vbOK Then
DoCmd.OpenForm "SelectPrinter", , , , , acDialog
**'Stopping here**
 Set Application.Printer = _
      Application.Printers(cboDestination.ListIndex)
DoCmd.Close acForm, "SelectPrinter", acSaveYes

   If ExpCount > 0 Then
    DoCmd.OpenReport "rpt_Ex_p"

End If
' Switch back to original default printer
Set Application.Printer = Application.Printers(strDefaultPrinter)
Else
Exit Function
End If
'Update table with today's date

DoCmd.RunSQL "UPDATE tbl_AHAD INNER JOIN tbl_Ex_p ON tbl_AHAD.ID = tbl_Ex_p.ID " & _
    "SET tbl_AHAD.Last_Insp_Date = Date() "
End Function

这是在DoCmd.OpenForm“SelectPrinter”,,,,,, acDialog

之后运行的子函数
Private Sub Form_Load()
cboDestination = ""
m_GetPrinters cboDestination
cboDestination.SetFocus
cboDestination.ListIndex = 0

End Sub

Public Sub m_GetPrinters(ByRef objListOrCombo As Object)
Dim objPrinter          As Printer
Dim intNbOfPrinters     As Integer

intNbOfPrinters = Printers.Count - 1
For Each objPrinter In Printers
    objListOrCombo.AddItem objPrinter.DeviceName
Next
End Sub

1 个答案:

答案 0 :(得分:0)

您专门以对话模式打开表单:

DoCmd.OpenForm "SelectPrinter", , , , , acDialog

因此,您的代码不会停止,但它会转到打开的表单并静默等待,直到该表单关闭 - 然后代码将继续。

如果您希望打开表单并让功能PrintReports()继续而不注意表单,请不要使用acDialog设置。