简体   繁体   中英

Microsoft Access - Loop through all forms and controls on each form

Okay so when I press a specific button I want to loop through all forms, then find every control in each form with the tag 'TESTING'. If the tag = 'TESTING' then I want to change the caption of the object to 'abc123'.

The only objects with the tag 'TESTING' will be labels, so they will have the caption property.

So far I have this as the function:

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

Then this as the button code:

Public Sub TestButton_Click()
Call changelabel
End Sub

So it executes the first for loop and opens all the forms in design view correctly. The problem lies with the second for loop. None of the label captions that have the tag property as 'TESTING' are changed to 'abc123'.

So what do I need to change to get the second for loop to work?

Something like this

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

You'll need to add a bit of house-keeping to set the objects used to nothing etc..

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM