changing multiple controls with a single line of code

Posted on 2006-04-05
Last Modified: 2010-05-01


Say I have the following controls on a form:


Whats is the most efficient way to set the enable value to false ?
Question by:andyw27
    LVL 92

    Accepted Solution

    Hi andyw27,

    You could do something like:

    Dim Counter As Long

    For Counter = 1 To 4
        Controls("textbox" & Counter).Enabled = False

    Hardly seems worth it for four controls; pretty handy when there are a dozen...


    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    Put all 4 textboxes onto 1 frame, and change the frame's enabled property to false

    another option is to create a control array of the 4 textboxes, each having the same name but a different index value.
    you can then loop through (assuming you name the control textbox with index values from 0 to 3):

    dim l as long
    for l = textbox.lbound to textbox.ubound
     textbox(l).enabled = false

    Author Comment


    how do you refer to the collective in a control array?

    for example text1(0) refers to the first one, is there a way (other than some kind of loop) to influence all members of the control array?
    LVL 9

    Expert Comment

    Its not possible in single line and without using loop to change all the control's property.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now