none
Pass a property as parameter RRS feed

  • Question

  • Hi,

    I wanna write a sub that looks through say, all Userform1's CheckBoxes which are tagged "1" and set their Value property to True.

    Something like this:

    SetByTag (UserForm1, "CheckBox", "1", "Value", "True")

    This is what I have so far (works fine, except where the comment is):

    Sub SetByTag(UserFormName As UserForm,
    ctrlType As String, tag As String, propertyName As String,
    propertyValue As Variant)
    
    Dim ctrl As Control
    
       For Each ctrl In UserFormName.Controls
    
       If TypeName(ctrl) = ctrlType Then
          If ctrl.tag = tag Then
          ctrl. propertyName = propertyValue 'propertyName errors
          End If    
                                            
        End If
    Next ctrl
    
    End Sub

    If I change the propertyName to an actual property - hardcoded  i.e.  

    ctrl.Value = propertyValue

    then it all runs smoothly.

    How can I pass a property as a parameter?

    Thanks.



    • Edited by GGLK Friday, January 8, 2016 6:30 PM
    Friday, January 8, 2016 4:26 PM

All replies

  • You can't refer to properties that way. You'll have to do something like this:

    Sub SetByTag(UserFormName As UserForm, ctrlType As String, _
        tag As String, propertyName As String, propertyValue As Variant)
        Dim ctrl As Control
        For Each ctrl In UserFormName.Controls
            If TypeName(ctrl) = ctrlType Then
                If ctrl.tag = tag Then
                    Select Case propertyName
                        Case "Value"
                            ctrl.Value = propertyValue
                        Case "Caption"
                            ctrl.Caption = propertyValue
                        Case "ControlSource"
                            ctrl.ControlSource = propertyValue
                        Case "Height"
                            ctrl.Height = propertyValue
                        ' etc
                    End Select
                End If
            End If
        Next ctrl
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Friday, January 8, 2016 7:50 PM
  • Thanks!
    Saturday, January 9, 2016 1:13 AM
  • Thanks!
    Sunday, January 10, 2016 6:09 AM