Solved

vba arrays

Posted on 2003-10-30
10
5,103 Views
Last Modified: 2009-01-23
how do I declare a global array and add values to it programmatically from one of the subforms?
0
Comment
Question by:YZlat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 11

Expert Comment

by:joekendall
ID: 9649998
You can declare a global array in a module.

Public myArray(10) As String

To add values to it, you would need to do something like this.

Dim n As Long

For n = 1 to 10
'This will fill the array with the value of a textbox on the subform
    myArray(n) = Forms!mySubform!myTextBox
Next

Joe
0
 
LVL 39

Expert Comment

by:stevbe
ID: 9649999
In a standard module ...

Public pvarArray()

in the form code

this will retain the data that already exists in the array and add 1 more element for you to store new data in.
ReDim Preserve pvarArray(UBound(pvarArray) + 1)

then to add a new value
pvarArray(UBound(pvarArray)) = "YourValueHere"

Steve
0
 
LVL 35

Author Comment

by:YZlat
ID: 9650087
Steve, I get an error in this line

ReDim Preserve pvarArray(UBound(pvarArray) + 1)

run-time error '9' Subscript out of range
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 39

Expert Comment

by:stevbe
ID: 9650191
genric all purpose function ....

Public pvarArray As Variant

Public Function AddToArray(NewValue As Variant)
    If IsArray(pvarArray) Then
        ReDim Preserve pvarArray(UBound(pvarArray) + 1)
        pvarArray(UBound(pvarArray)) = NewValue
    Else
        pvarArray = Array(NewValue)
    End If
End Function

Steve
0
 
LVL 35

Author Comment

by:YZlat
ID: 9650934
that did not work either
0
 
LVL 35

Author Comment

by:YZlat
ID: 9650991
I tried a different approach:
I declared Public myArray as Variant in one of the public modules.
Then in my code for the subform I put
 ReDim Preserve myArray(UBound(myArray) + 1)
myArray = Array(myValue)

but then i started to get a type mismatch error in line ReDim Preserve myArray(UBound(myArray) + 1)
0
 
LVL 11

Expert Comment

by:joekendall
ID: 9651064
YZlat:

Did you try my example? If you want a dynamic array, you can change it this way.

Public myArray() As String

To add values to it, you would need to do something like this.

Dim n As Long

For n = 1 to 10
'This will fill the array with the value of a textbox on the subform
    Redim Preserve myArray(n)
    myArray(n) = Forms!mySubform!myTextBox
Next

Joe
0
 
LVL 39

Expert Comment

by:stevbe
ID: 9651082
that is because a variant is not an array until you make it one as I coded for specifically in my function. Wwe can expand it to handle any publically declared variant variable ...

Public Function AddToArray(PublicArray As Variant, NewValue As Variant)
    If IsArray(PublicArray) Then
        ReDim Preserve PublicArray(UBound(PublicArray) + 1)
        PublicArray(UBound(PublicArray)) = NewValue
    Else
        PublicArray = Array(NewValue)
    End If
End Function

then in your form you pass the array to add the value to and the value to be added to the AddToArray function.

AddToArray(myArray, myValue)

Steve
0
 
LVL 35

Author Comment

by:YZlat
ID: 9651216
joekendall, I guess I did not make it very clear what I need. I have a form and a subform. A subform ia a datasheet form and when the user clicks on one cell, the contents of the cell should be added to an array. The user should be able to add multiple items.
Then when the button clicked, the contents of an array will be manipulated further.
And if the form is closed or reloaded I want the array to empty itself.
0
 
LVL 11

Accepted Solution

by:
joekendall earned 35 total points
ID: 9651261
In a module add this code.

Public myArray() As String
Public nCounter As Long

Do you have the Click event set up? If so, just add this code.

nCounter = nCounter+1
Redim Preserve myArray(nCounter)
myArray(nCounter) = Forms!mySubform!myTextBox

To clear the array, you can do it this way.

Redim myArray(0)

Joe
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question