• Loop through controls on a form (XP)

    Author
    Topic
    #387121

    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,

    Viewing 1 reply thread
    Author
    Replies
    • #674470

      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.

    • #674581

      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

      • #674633

        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.

    Viewing 1 reply thread
    Reply To: Reply #674473 in Loop through controls on a form (XP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel