Arrays

Mcgrco

Registered User.
Local time
Today, 20:56
Joined
Jun 19, 2001
Messages
118
I seem to have developed a mental block when it comes to understanding arrays. Does anyone one know where i can get some simple working examples. MS help is a little confusing.

Thanks
 
What exactly are you looking to do?

Simple creation is just:

Dim MyArray (25,15)
 
Here's a quick rundown on arrays:

Say you want to have a several values held in variables of the same type which you can call from code.

You could give each variable a unique name like:

Dim Name1 as integer, Name2 as integer, Name3 as integer, Name4 as integer

and then your code would use them like

Name1 = Name1 + 1
Name2 = Name2 + 1
Name3 = Name3 + 1
Name4 = Name4 + 1

and whenever you wanted to use them, your code would have to specifically call each variable.

Instead, you could make an array, which is just a simpler way of naming a bunch of variables so that your code can call them through algorythms. If you wanted to have an array that's 4 deep you'd use:

Dim Name(4) as integer

and you would use them like

For X = 1 to 4
Name(X) = Name(X) + 1
Next X

You could add another dimension:

Dim Name(4,2)

For X = 1 to 4
For Y = 1 to 2
Name(X,Y) + X*Y
Next Y
Next X

and so on.
 
aRRAYS

Thanks Guys,

Not being from a IT backround im failing to understand its uses. Ive been coding VBA for over a year now so i felt it was something I should understand.

I was hoping that someone would have a simple working version of an array ,so I could see where and how its applied. I unfortuantly learn by example. If anyone has something like this then I would apprciate it.



The quest goes on. Im sure, like all microsoft querks it will dawn on me when I least expect it.

thanks
 
Arrays are helpful if you have a list of items that you need to pass from one space to another. The file dialog box is a good example. If you allow multiselect then you need to be able to have a single variable that can except multiple instances. The following is a snippet of code that pulls a list of files selected by the user in the dialog box:

With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = True
.InitialFileName = "C:\My Documents\*.*"
If .Show = -1 Then
numOfFiles = .SelectedItems.Count
' Fill the array with selected files.
' SelectedItems() is an array used by the file dialog.
For lngCount = 1 To numOfFiles
strArray(lngCount) = .SelectedItems(lngCount)
Next lngCount
Else
End
End If
End With

Then to open the file in the selected program (in my example its a program called monarch) you just re-iterate through the array:

For lngCount = 1 To numOfFiles
openfile = MonarchObj.SetReportFile(strArray(lngCount), True)
MonarchObj.DisplayWindow (2)
Next lngCount

This is a fairly staightforward example. If you really want to figure out arrays I would suggest trying your hand at making a card game (that's how I figured them out). The card deck is the array and it is filled with the cards. You can then shuffle them to another array arrShuffled() and from there hand them out to your players, arrPlayer1() arrPlayer2().

Anyway, hope this example helps.

Peace
 
aRRAYS

Thanks,

Your help is much appreciated.

I just dont get how you use it. The array is indexed so how do you link that to a recordset ect.
 
A recordset is itself (in affect) an array. It's a single item that holds many variables. You can create an array that holds information that you pull from the recordset. A table is also an array. Lets say you create an array:

Dim myArray(5, 10)

This in affect is a table. You would say that the table has 5 columns and with each column there are ten rows.

So if you think of it as an excel spreadsheet A2 would be:

myArray(1, 2)

E8 would be:

myArray(5, 8)

you could create a cubed space using an array by having the levels:

Dim myArrayCube(3, 3, 3) as Object

The first number would be the level your in: front, middle or back.
The second number would be the Column your in: Top, Middle, Bottom
The third number would be the Row: left, middle or right

If you wanted the space in the center of the cube you would use:

myArrayCube(2, 2, 2)


Second level, second column, second row.

Or if you want to think of it in terms of Access. Second Table's Second Column's Second row.

How's that?
 
Thanks again,

Im stating to understand the concept. Im only used to Access VBA but your recordset decription helped me visualized an array.

I think I need to play around with it some more.

Thanks again for your help
 

Users who are viewing this thread

Back
Top Bottom