我在Access 2010数据库的表单上显示了一个Subform / Subreport控件,我用它来显示表单和报表。我有一些事件处理程序,我需要知道报表当前是否加载到Subform / Subreport控件中,或者是否是一个已加载的Form。我已经尝试了以下所有方面无济于事。
以下任何一种情况
If IsEmpty(NavigationSubform.Form) Then '...
If IsNull(NavigationSubform.Form) Then '...
If IsOject(NavigationSubform.Form) Then '...
If NavigationSubform.Form Is Nothing Then '...
If NavigationSubform.Form Is Null Then '...
If Nz(NavigationSubform.Form) Then '...
If (Not NavigationSubform.Form) = -1 Then '... This is a trick I use to check for uninitialized arrays
结果
运行时错误'2467':
您输入的表达式是指已关闭或不存在的对象。
我是否可以通过某种方式检查子窗体/子报表控件当前是否已加载表单或报表而不会故意导致错误?
答案 0 :(得分:1)
我不相信有一种方法可以在没有错误捕获的情况下可靠地执行检查,因此您可能希望将代码包装在Public Function
中并将其放入常规VBA模块中:
Public Function CheckSubformControlContents(ctl As SubForm) As String
Dim obj As Object, rtn As String
rtn = "None"
On Error Resume Next
Set obj = ctl.Form
If Err.Number = 0 Then
rtn = "Form"
Else
On Error Resume Next
Set obj = ctl.Report
If Err.Number = 0 Then
rtn = "Report"
End If
End If
Set obj = Nothing
On Error GoTo 0
CheckSubformControlContents = rtn
End Function
然后,您的表单代码只需调用CheckSubformControlContents(Me.NavigationSubform)
。
答案 1 :(得分:-1)
以下是两个在Access 2013中用于确定名称是报表还是表单的函数。 确定后,可以使用AllForms或AllReports的IsLoaded函数。请注意,dbs是一个对象,rpt或frm是AccessObjects而不是表单或报告
Public Function IsForm(FormName As String) As Boolean
Dim dbs As Object
Dim frm As AccessObject
Set dbs = Application.CurrentProject
IsForm = False
For Each frm In Application.CurrentProject.AllForms
If frm.Name = FormName Then
IsForm = True
Exit For
End If
Next frm
Set frm = Nothing
Set dbs = Nothing
End Function
Public Function IsReport(ReportName As String) As Boolean
Dim dbs As Object
Dim rpt As AccessObject
Set dbs = Application.CurrentProject
IsReport = False
For Each rpt In Application.CurrentProject.AllReports
If rpt.Name = ReportName Then
IsReport = True
Exit For
End If
Next rpt
Set rpt = Nothing
Set dbs = Nothing
End Function
这是一个使用上述功能的程序:
Public Sub EnumerateTaggedControls(ReportName As String,MyTag As String) 昏暗的dbs作为对象 Dim rpt As Report Dim frm As Form Dim col As Controls 昏暗的ctl作为控制 Dim left As Integer Dim top As Integer 昏暗宽度为整数 昏暗高度为整数 Dim标签为String Dim i As Integer Const format1 As String =“0000”
Set dbs = Application.CurrentProject
If IsForm(ReportName) Then
If dbs.AllForms(ReportName).IsLoaded Then
DoCmd.OpenForm ReportName, acViewDesign
Set frm = Forms(ReportName)
Set col = frm.Controls
End If
Else
If dbs.AllReports(ReportName).IsLoaded Then
DoCmd.OpenReport ReportName, acViewDesign
Set rpt = Reports(ReportName)
Set col = rpt.Controls
Else
Debug.Print ReportName & " is not a loaded form or report."
Exit Sub
End If
End If
Set dbs = Nothing
Debug.Print Tab(53); "Left Top Width Height"
For Each ctl In col
With ctl
left = .Properties("Left")
top = .Properties("Top")
width = .Properties("Width")
height = .Properties("Height")
tag = Nz(.Properties("Tag"), vbNullString)
If MyTag = "" Then
i = 1
Else
i = InStr(1, tag, MyTag)
End If
If i > 0 Then
Debug.Print .Name & ">"; Tab(33); tag; Tab(53); Format(left, format1) & Format(top, format1) & Format(width, format1) & Format(height, format1)
End If
End With
Next ctl
Debug.Print "====================================================="
Set ctl = Nothing
Set rpt = Nothing
Set col = Nothing
Set frm = Nothing
End Sub
我希望这符合您的要求。