【问题标题】:Access vba function getting stopping when other form opens as part of function当其他表单作为函数的一部分打开时,访问 vba 函数将停止
【发布时间】:2016-01-07 08:28:18
【问题描述】:

当数据库打开时,我在表单加载时运行以下函数。我知道缺少某些东西,但我不确定到底是什么。该代码运行良好,直到它打开一个表单,以便用户可以选择打印机。然后表单就有了它自己的工作原理。
我中断了代码,代码在 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 之后与它的函数一起运行的 sub

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

【问题讨论】:

    标签: function ms-access vba


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-20
      • 1970-01-01
      • 2022-06-14
      • 1970-01-01
      相关资源
      最近更新 更多