Adding controls to a collection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zswanson
    New Member
    • Jul 2007
    • 9

    #1

    Adding controls to a collection

    Hello all,

    I'm trying to change the propeties of 4 controls all at once using a collections object. Is this correct? I have to add them to a custom collection first, because I don't want all the controls (text boxes) on the form to change, only certain ones.

    How can I get it to work?

    Here's my code:

    Code:
    Private Sub lblEditInfo_Click()
        
        Dim colTextBoxes As Collection
        Dim counter As Integer
        
    
        
        colTextBoxes.Add Me.Controls("txtPayorName"), 1
        colTextBoxes.Add Me.Controls("txtName"), 2
        colTextBoxes.Add Me.Controls("txtNickname"), 3
        colTextBoxes.Add Me.Controls("txtExternalID"), 4
        
        For counter = 1 To colTextBoxes.Count
        
            With colTextBoxes(counter)
                .BackColor = "15783845"
                .Locked = False
                .ForeColor = "0"
            End With
        
        Next
        
    End Sub
  • zswanson
    New Member
    • Jul 2007
    • 9

    #2
    I kind of sovled my own problem by doing it through a split array instead. But if anyone knows how to do it the collection way, that would be better.

    Code:
    Private Sub lblEditInfo_Click()
        
        Dim arrTextNames As Variant
        Dim counter As Integer
        
        
        arrTextNames = Split("txtName;txtNickname;txtExternalID", ";")
        
        For counter = 0 To UBound(arrTextNames)
        
            With Me.Controls(arrTextNames(counter))
                .BackColor = "16777215"
                .Locked = False
                .ForeColor = "0"
            End With
        
        Next
        
        txtExternalID.SetFocus
        txtExternalID.SelStart = (Len(txtName.Value))
        
    End Sub

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      What version of VB is this? If it's VB6, you might be better off simply making them a control array on the form.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Another thought that comes to mind is, why not just do the four controls individually, but by passing them to a Sub or Function which does the work? In other words, something like this...

        [CODE=vb]SetupTextBox txtPayorName
        SetupTextBox txtName
        SetupTextBox txtNickname
        SetupTextBox txtExternalID


        Private Sub SetupTextBox(ct l As TextBox)
        With ctl
        .BackColor = 15783845
        .Locked = False
        .ForeColor = 0
        End With
        End Sub[/CODE]

        Comment

        • zswanson
          New Member
          • Jul 2007
          • 9

          #5
          I'm using whatever version is with Access 2003. I wanted to be able to use the function from any form. I figured it out. I didn't use a control array, I can't say I know how. What I did is below if anyone cares. It's kind of messy but it does the job using what I know how to do:

          Code:
          Public Sub subFlashControls(strControlNames As String, intSwitch As Integer, _
                                      strFormMainName As String, _
                                      Optional strFormSubName As String)
              
              Dim arrTextNames As Variant
              Dim counter As Integer
          
          
              'this function switches between colors & locked states based on the values
              'passed in (use a lot in payors center)
              'if intSwitch = 1, then it is switching from locked state to unlocked state
              'if intSwitch = 2, then it is switching from unlocked state to locked state
              'it also looks to see if a subform name has been passed
              'if so, then it refers to the form accordingly (if it is a subform)
              'if a subform, the name passed should be the name of the subform OBJECT,
              'not the actaul subform (since subforms aren't paret of the FORM collection)
          
          
          
              
              'generate array
              arrTextNames = Split(strControlNames, ";")
          
          
              If intSwitch = 1 Then    'unlock controls
                  For counter = 0 To UBound(arrTextNames)
                      'if this is a subform:
                      If Nz(strFormSubName, "") <> "" Then
                          With Forms(strFormMainName)(strFormSubName).Form(arrTextNames(counter))
                              .BackColor = "0"
                              .Locked = True
                              .ForeColor = "15783845"
                          End With
                      'if this isn't a subform
                      Else
                          With Forms(strFormMainName)(arrTextNames(counter))
                              .BackColor = "0"
                              .Locked = True
                              .ForeColor = "15783845"
                          End With
                      End If
                  Next
              ElseIf intSwitch = 2 Then    'lock controls
                  For counter = 0 To UBound(arrTextNames)
                      'if this is a subform
                      If Nz(strFormSubName, "") <> "" Then
                          With Forms(strFormMainName)!sfmMain.Form(arrTextNames(counter))
                              .BackColor = "16777215"
                              .Locked = False
                              .ForeColor = "0"
                          End With
                      'if this isn't a subform
                      Else
                          With Forms(strFormMainName)(arrTextNames(counter))
                              .BackColor = "16777215"
                              .Locked = False
                              .ForeColor = "0"
                          End With
                      End If
                  Next
              End If
          
          
          End Sub

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Nice one.

            By the way, I don't think Access supports control arrays at all.

            Comment

            Working...