Results 1 to 5 of 5
  1. #1
    2 Star Lounger
    Join Date
    Jun 2001
    Location
    Virginia, USA
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Loop through controls on a form (XP)

    Can someone direct me to the appropriate code to loop through all the controls on a form? I need to disable 6 text boxes when the click event happens on a command button.

    cmdLockRecord_Click()
    Dim intCounter as integer
    dim ctl as control
    set ctl = something here to indicate controls on a form....

    'lock these text boxes
    'txtLastName
    'txtPhone
    'txtFax
    'txtEmail
    'txtDomain

    'lock text boxes
    for intCounter = 0 to some code here to indicate every control on the form...
    with ctl
    .back color = vbyellow
    .enabled=false
    end with
    next intCounter

    Thanks,

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Loop through controls on a form (XP)

    Dim ctl As Control

    For Each ctl in Me.Controls
    If ctl.ControlType = acTextBox Then
    Select Case ctl.Name
    Case "txtLastName", "txtPhone","txtFax","txtEMail","txtDomain"
    ctl.Enabled = False
    Case Else
    End Select
    End If
    Next ctl

    If the control is just disabled, the backcolor is irrelevant since the control is grayed out. If you want to see the backcolor, you can set the enabled property to false and the Locked property to Yes, which will give you a normal appearing control. In that case, just add the line setting the backcolor before you set the Enabled = false and Locked = True.
    Charlotte

  3. #3
    2 Star Lounger
    Join Date
    Jun 2001
    Location
    Virginia, USA
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Loop through controls on a form (XP)

    Thanks Charlotte. Works like a carm.

  4. #4
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Loop through controls on a form (XP)

    Why use a loop, when all you have to do is:
    txtLastName.Enabled = False
    txtPhone.Enabled = False
    txtFax.Enabled = False
    txtEmail.Enabled = False
    txtDomain.Enabled = False

  5. #5
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Loop through controls on a form (XP)

    The only real reason for using a loop is to avoid having to write a bunch of statements for each control, since just disabling the controls wasn't all that needed to be done. Using a loop, you can write all the statements you need to apply to the current control object and if you change what you want to do to those objects, you only need to change the code in one place, not everywhere you put it in the routine. Directly addressing the controls may be marginally faster, but I seriously doubt the difference is noticeable.
    Charlotte

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •