确定子窗体/子报表是否在MS Access中加载了窗体或报表



我有一个Subform/Subreport控件显示在Access 2010数据库的窗体上,我用它来显示窗体和报表。我有几个事件处理程序,我需要知道是否一个报告是当前加载到Subform/Subreport控件,或者如果它是一个表单加载。我尝试了以下所有方法,但都无济于事。

下列条件之一

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

您输入的表达式指向一个关闭或不存在的对象。

是否有一些方法,我可以检查是否一个子窗体/子报表控件当前有一个窗体或报表加载没有故意造成错误?

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

Access 2013中有两个函数用于确定名称是Report还是Form。一旦确定,就可以使用AllForms或AllReports的IsLoaded函数。请注意,dbs是一个对象,而rpt或from是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
下面是使用上述函数的程序:

公共子EnumerateTaggedControls(ReportName作为字符串,MyTag作为字符串)Dim dbs作为对象将报告设置为报告从As形式暗淡的暗冷控制Dim ctl As控制Dim left As IntegerDim top As IntegerDim width As IntegerDim height As IntegerDim标签作为字符串Dim i As IntegerConst 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

结束子

我希望这符合你的要求。

最新更新