2016-01-20 26 views
0

好吧,當我按下特定的按鈕時,我想循環遍歷所有窗體,然後在每個窗體中找到每個窗體的標籤'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循環,並能正確打開在設計視圖中的所有形式。問題在於第二個循環。沒有任何標籤標籤的標籤屬性爲'TESTING'變爲'abc123'。

那麼,我需要改變以獲得第二個循環的工作?

+2

您使用我在打開的形式在設計模式下。您需要將其打開成表單對象或引用它,我是代碼所在的加載表單。 –

+0

@Nathan_Sav完美,這正是我的代碼有什麼問題..我已經將它從Me.Controls更改爲Form(obj.Name).Controls,它現在似乎正在工作。謝謝! – Alex

回答

1

像這樣的事情

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 

你需要添加一點看家用來設置什麼等對象..

0
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 
相關問題