View Full Version : VBA Arrays


Gliff
01-24-2008, 12:31 PM
I've got myself into a slight pickle. I'm creating a form where there are a list of places in one listbox. A user selects a place and can add it using a button to a second listbox. Now, when the user has finished adding places to the second listbox, I need to put it into an array.

I've tried my best but to no avail - I'm still a relative VBA amateur. The name of the second Listbox is 'List2' - all I need is a simple array to store the values held in List2.

If anyone has anyyy clue, which i'm sure someone does (This seems like a fairly basic Access problem), i'd be very appreciative if they can show me a generic code I can manipulate. I'm not expecting the work to be done for me, just a bit of a kick in the right direction would be very helpful!

Thanks!

pono1
01-24-2008, 08:38 PM
Roughly...

Function FillArray(ListName As ListBox, ColumnIndex As Integer) As Variant

Dim item As String: item = ""
Dim i As Long

For i = 0 To ListName.ListCount - 1
item = item & ListName.Column(ColumnIndex, i) & ","
Next

item = Left(item, Len(item) - 1)
FillArray = Split(item, ",")

End Function

Regards,
Tim

Gliff
01-25-2008, 11:28 AM
I've managed to make it work except I keep getting error messages when I place the FillArray function. If it's of further significance, I am creating an array within a button command.

Gliff
01-26-2008, 07:42 AM
Aha, OK, I think I have the array working, but it's difficult to know if it's doing what I need it to do. How could I access each item which is stored in the array?

pono1
02-01-2008, 06:34 AM
Assuming it's a 1-dimensional array you can run through it and print out values to the Immediate window.


Dim i As Integer
For i = lbound(MyArray) To ubound(MyArray)
Debug.Print MyArray(i)
Next i


Maybe better: just set a breakpoint and use the Locals window to see the array's values.

Regards,
Tim

ajetrumpet
02-02-2008, 02:13 PM
Roughly...

Function FillArray(ListName As ListBox, ColumnIndex As Integer) As Variant

Dim item As String: item = ""
Dim i As Long

For i = 0 To ListName.ListCount - 1
item = item & ListName.Column(ColumnIndex, i) & ","
Next

item = Left(item, Len(item) - 1)
FillArray = Split(item, ",")

End Function

Regards,
TimPono,

For some reason, I recognize this stuff... :D :D :D

pono1
02-02-2008, 09:17 PM
I would guess so...:)

Regards,
Tim