Results 1 to 13 of 13

Thread: Arrays across forms?!

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2001
    Posts
    36
    I have an array within one form in my project containing no more than 60 entries which I need to use on another form in my project. I've tried using

    frmInterface.<arrayname> = <stuff>

    but that gives an error. Is there any way of referencing arrays on different forms? If so can somebody please let me know!

    TIA

    BioS

  2. #2
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Here ya go: To use this, just add a public function to your destination form and pass the array as indicated below
    Code:
    Option Explicit
    Dim intNum(1 To 5) As Integer
    
    Private Sub Command1_Click()
        Call Form2.PrintNum(intNum())
    End Sub
    
    Private Sub Form_Load()
        Dim X As Integer
        For X = 1 To 5
            intNum(X) = X
        Next
    End Sub
    
    Private Sub PrintNum(xArray() As Integer)
        Dim var As Variant
        For Each var In xArray
            Print var
        Next
    End Sub

  3. #3
    Fanatic Member PsychoMark's Avatar
    Join Date
    Feb 2001
    Location
    Netherlands
    Posts
    540
    When declaring the array in the form, declare it as: "Public Array(20) As Integer" (or however you're array is constructed, just use Public), then you can just use it as frmInterface.Array(Item)...

  4. #4
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    that will also work. I just avoid using public variables.

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2001
    Posts
    36

    Thumbs up

    Thanks both... exactly what I was looking fer

    BioS

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2001
    Posts
    36
    The public thing doesn't seem to work and I didn't really understand the top example!

    I get a compile error :

    'Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules'

    Any ideas on getting round this?

    TIA

    BioS

  7. #7
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    you can't make arrays Public variables. The way you would get around that is to declare a Public variable as a Variant, then set that variable = to the array, but that's too much work for me, I'd go with Lethal's way...
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  8. #8

    Thread Starter
    Member
    Join Date
    Jan 2001
    Posts
    36
    Any chance of an explaination of Lethal's way? does all that code go on the same form? and what is .PrintNum?

    BioS

  9. #9
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    The code I posted is just supposed to give you an example of passing arrays through multiple forms. The PrintNum function would go into your other form where you want to pass the array.

  10. #10
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    sure...

    Code:
    Option Explicit
    Dim intNum(1 To 5) As Integer
    
    Private Sub Command1_Click()
        Call Form2.PrintNum(intNum()) ' sends the entire array to the sub on form2 called PrintNum
    End Sub
    
    Private Sub Form_Load() 'loads the array up with values, obviously
        Dim X As Integer
        For X = 1 To 5
            intNum(X) = X
        Next
    End Sub
    
    'on Form2
    Private Sub PrintNum(xArray() As Integer)'takes in an array as a parameter
        Dim var As Variant
        For Each var In xArray
            Print var ' prints each item in the array on the form
        Next
    End Sub
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  11. #11
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    damn!
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  12. #12
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    hehehe.....your gettin' quicker there blade...

  13. #13

    Thread Starter
    Member
    Join Date
    Jan 2001
    Posts
    36
    Cheers guys... appreciate it

    BioS

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