3

我在 Access 2010 数据库的表单上显示了一个子表单/子报表控件,我用它来显示表单和报表。我有一些事件处理程序,我需要知道报表当前是否已加载到子表单/子报表控件中,或者它是否是已加载的表单。我尝试了以下所有方法均无济于事。

以下任何一种情况

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”:

您输入的表达式引用了一个已关闭或不存在的对象。

有什么方法可以检查子表单/子报表控件当前是否加载了表单或报表而不会故意导致错误?

4

2 回答 2

2

我不相信有一种方法可以可靠地执行检查而不会捕获错误,因此您可能希望将代码包装在 a 中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).

于 2013-05-23T21:24:52.437 回答
-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) Dim dbs As Object Dim rpt As Report Dim frm As Form Dim col As Controls Dim ctl As Control Dim left As Integer Dim top As Integer Dim width As Integer Dim height As Integer Dim tag As 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

结束子

我希望这能满足你的要求。

于 2013-07-04T07:59:42.353 回答