Access VBA - How would I have a loop in VBA that allows me to loop through control names

前端 未结 3 1856
暗喜
暗喜 2020-12-11 04:39

I have about 10 text boxes on a form that are actually used for display not entry. They are are named txt_001_Name, txt_002_Title, etc..what kind o

3条回答
  •  执笔经年
    2020-12-11 04:59

    I prefer to use a FOR EACH to iterate through the controls collection of whatever the textboxes are on (either the form itself or a panel control)

    dim myBox as Textbox
    For each myBox in myForm
        myBox.Text = "hello"
    Next
    

    Also means you can make custom groups (by putting them all on the same container). Note that if you have other controls, you might need a typecheck in there (IF TYPEOF(myBox) = "TextBox" THEN ...)

    You could also do it like:

    dim i as integer
    For i = 1 to 10
         myForm.Controls("txt_00" & i & "_Title").Text = "hello"
    Next i
    

    I definitely prefer the For Each, though.

提交回复
热议问题