Results 1 to 6 of 6

Thread: Passing arrays of controls

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    London
    Posts
    290
    Hi.

    I can pass an array of a variable created in the function i`m passing from, but i cant pass an array from a form. Ie.

    dim test(10) as SSCommand ' sheridan active threed control
    call AFunc(test)

    works ok, but if `test` is the name of a control on a form, i get :

    `Type mismatch: array or user-defined type expected`

    Is there any reason why controls (from forms) are treated differently?

    Thanks,
    Alex.

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    I don't know if anyone has ever figured out a way to do what you want, but the reason you see the different behavior is that an array of controls (a control array) is not the same as an array of data.

  3. #3
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    First of all your array is declared on General Declaration, therefore is accessible to every other function or sub.
    Make sure you have parameter in the function defined with the different name. For example if I have a parameter in my function or sub I always follow this naming convention:
    Code:
    'p_Parameter: - p stands for parameter
    Public Function MyFunction(p_Parameter As DataType) As Boolean
    
    End Function

  4. #4
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    MartinLiss is correct

    In that the two Arrays are quite different. I think you will find that Control arrays are collections in fact.

    You may be able to emulate this by the following code.

    Code:
    'in a module:
    Option Explicit
    
    Public Function AFunc(vdata As Variant) As String
      Dim c As Integer
      Dim res As String
      Dim val As String
      Dim myControl As Control
      
      Select Case TypeName(vdata)
      Case "Object" ' possibly a collection but be careful
        
        For Each myControl In vdata
          ' is this item a text box?
          If TypeName(myControl) = "TextBox" Then
            val = myControl.Text
            res = res & vdata(c).Text
            If c < vdata.Count Then res = res & vbCrLf
          End If
        Next
        
      Case "String()" ' it's an array of string
        For c = LBound(vdata) To UBound(vdata)
          val = vdata(c)
          res = res & vdata(c)
          If c < UBound(vdata) Then res = res & vbCrLf
        Next
      
      Case Else
        Debug.Print TypeName(vdata)
      End Select
      
      AFunc = res
    End Function
    Code:
    'in form1 which has a control array of text boxes called Text1
    ' it also has 2 command buttons and a multiline text box called text2
    
    Option Explicit
    
    Private Sub Command1_Click()
      Text2.Text = AFunc(Text1)
    End Sub
    
    Private Sub Command2_Click()
      Dim testData(5) As String
      Dim c As Integer
      For c = 0 To 5
        testData(c) = "test data " & c
        
      Next
      Text2.Text = AFunc(testData)
    End Sub
    I think there is enough in there to get you going.

    Cheers
    Paul Lewis

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    London
    Posts
    290
    Martin+Serge: I WAS passing a control, from a form (not just data). (SSCommand is a control).

    PaulLewis: Thanks a lot for the example code! Didnt know about TypeName - i`ve been putting chars into the Tag field for identification purposes! Anyway, i added an array of SSCommand controls to the form, and a command3 button and added a `SSCommand` case (acually elseif) to the container code, and it worked fine, so i`ll go back now and see how that differs from what i`m doing at the moment in my code.

    Thanks,
    Alex.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    London
    Posts
    290
    Yeah, that works. The trick is not to receive an array, but a single instance of a varient. So much for typing in VB!

    Thanks again,
    Alex.

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