Results 1 to 4 of 4

Thread: Passing control array as parameter

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Location
    Australia
    Posts
    115

    Post

    I'm trying to write a common function which sets the caption property of label arrays.

    eg
    On my form, I have label1(0) and label(1) and want to set the caption with 'Caption1' and 'Caption2' respectively with the following function call:

    SetCaption Label1, "Caption1", "Caption2"

    So how can I declare the function?

    Public Sub SetCaption(LabelCtrl as ???, ParamArray Data() as Variant)

    for i = 0 to ubound(data)
    LabelCtrl(i).Caption = data(i)
    next
    End Sub

    TIA
    Carolyn

  2. #2
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845

    Post

    this works:
    Code:
    Option Explicit
    Dim captions(4) As String
    
    Private Sub Command1_Click()
    SetCaption Label1, captions
    
    End Sub
    
    Private Sub Form_Load()
    captions(0) = "hello"
    captions(1) = "hello"
    captions(2) = "hello"
    captions(3) = "hello"
    End Sub
    Public Sub SetCaption(LabelCtrl As Variant, Data() As String)
    Dim i As Integer
    For i = 0 To UBound(Data) - 1
    LabelCtrl(i).Caption = Data(i)
    Next
    End Sub
    ------------------
    Mark Sreeves
    Analyst Programmer

    [email protected]
    A BMW Group Company



  3. #3
    New Member
    Join Date
    Jan 2012
    Posts
    3

    Thumbs up Re: Passing control array as parameter

    Quote Originally Posted by mark sreeves View Post
    this works:
    Code:
    option explicit
    dim captions(4) as string
    
    private sub command1_click()
    setcaption label1, captions
    
    end sub
    
    private sub form_load()
    captions(0) = "hello"
    captions(1) = "hello"
    captions(2) = "hello"
    captions(3) = "hello"
    end sub
    public sub setcaption(labelctrl as variant, data() as string)
    dim i as integer
    for i = 0 to ubound(data) - 1
    labelctrl(i).caption = data(i)
    next
    end sub
    ------------------
    mark sreeves
    analyst programmer

    [email protected]
    a bmw group company
    yahooo!!!! You saved me!!!!!!

  4. #4
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Passing control array as parameter

    Another solution to a 13 year old unresolved problem:

    Code:
    Private Sub SetCaptions(ByVal LabelArray As Object, ParamArray Captions() As Variant)
        Dim i As Integer, LB As Integer, UB As Integer
    
        LB = LabelArray.LBound
        UB = LabelArray.UBound
    
        If LB = LBound(Captions) Then
            If UB <= UBound(Captions) Then
                On Error Resume Next
                For i = LB To UB
                    LabelArray(i).Caption = Captions(i)
                Next
            End If
        End If
    End Sub
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

Posting Permissions

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



Click Here to Expand Forum to Full Width