【问题标题】:Microsoft Access - Loop through all forms and controls on each formMicrosoft Access - 循环遍历每个表单上的所有表单和控件
【发布时间】:2016-01-20 13:47:28
【问题描述】:

好的,所以当我按下特定按钮时,我想遍历所有表单,然后在每个表单中找到带有“TESTING”标签的每个控件。如果 tag = 'TESTING' 那么我想将对象的标题更改为 'abc123'。

带有“TESTING”标签的唯一对象将是标签,因此它们将具有标题属性。

到目前为止,我有这个功能:

Public Function changelabel()

On Error Resume Next
Dim obj As AccessObject, dbs As Object
Dim ctrl as Control

Set dbs = Application.CurrentProject

For Each obj In dbs.AllForms
DoCmd.OpenForm obj.Name, acDesign
    For Each ctrl In Me.Controls
        If ctrl.Tag = "TESTING" Then
        ctrl.Caption = "abc123"
        End If

        Next ctrl

Next obj

End Function

然后这个作为按钮代码:

Public Sub TestButton_Click()
Call changelabel
End Sub

所以它执行第一个 for 循环并正确打开设计视图中的所有表单。问题在于第二个 for 循环。没有将标签属性为“TESTING”的标签标题更改为“abc123”。

那么我需要进行哪些更改才能使第二个 for 循环正常工作?

【问题讨论】:

  • 您在设计模式下以打开的形式使用我。您需要将其打开到表单对象中或引用它,我是代码所在的加载表单。
  • @Nathan_Sav 完美,这正是我的代码出了问题。我已经将它从 Me.Controls 更改为 Form(obj.Name).Controls 并且它现在似乎正在工作。谢谢!

标签: forms vba ms-access for-loop


【解决方案1】:
    Public Sub GetForms()
    Dim oForm As Form
    Dim nItem As Long
    Dim bIsLoaded As Boolean
    For nItem = 0 To CurrentProject.AllForms.Count - 1
        bIsLoaded = CurrentProject.AllForms(nItem).IsLoaded
        If Not bIsLoaded Then
            On Error Resume Next
            DoCmd.OpenForm CurrentProject.AllForms(nItem).NAME, acDesign
        End If
        Set oForm = Forms(CurrentProject.AllForms(nItem).NAME)
        GetControls oForm
        If Not bIsLoaded Then
            On Error Resume Next
            DoCmd.Close acForm, oForm.NAME
        End If
    Next
End Sub

Sub GetControls(ByVal oForm As Form)
    Dim oCtrl As Control
    Dim cCtrlType, cCtrlCaption As String
    For Each oCtrl In oForm.Controls
        If oCtrl.ControlType = acSubform Then Call GetControls(oCtrl.Form)
        Select Case oCtrl.ControlType
            Case acLabel: cCtrlType = "label": cCtrlCaption = oCtrl.Caption
            Case acCommandButton: cCtrlType = "button": cCtrlCaption = oCtrl.Caption
            Case acTextBox: cCtrlType = "textbox": cCtrlCaption = oCtrl.Properties("DataSheetCaption")
            Case Else: cCtrlType = ""
        End Select
        If cCtrlType <> "" Then
            Debug.Print oForm.NAME
            Debug.Print oCtrl.NAME
            Debug.Print cCtrlType
            Debug.Print cCtrlCaption
        End If
    Next
End Sub

【讨论】:

    【解决方案2】:

    类似的东西

    Public Function changelabel()
    
    Dim f As Form
    Dim i As Integer
    Dim c As Control
    
    For i = 0 To CurrentProject.AllForms.Count - 1
        If Not CurrentProject.AllForms(i).IsLoaded Then
            DoCmd.OpenForm CurrentProject.AllForms(i).Name, acDesign
        End If
        Set f = Forms(i)
    
        For Each c In f.Controls
            If c.Tag = "TESTING" Then
                c.Caption = "TESTING"
            End If
        Next c
    Next i
    
    
    End Function
    

    您需要添加一些整理工作以将使用的对象设置为空等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多