help with VBA syntax

  • Thread starter Thread starter Wim
  • Start date Start date
W

Wim

Hi all,

I'm a fairly experienced Clipper, Pascal, C-programmer,
but I don't seem to grasp all of the finesses of VBA.

I'd like to initialise and fill an array, but I don't see
how.

I want to create an array containing addresses, i.e. :

A(0,0) = "Name" ' string
A(0,1) = "Street" ' string
A(0,2) = "DateOfBirth" ' date
A(0,3) = "NumberOfChildren" ' numeric, default 0
A(0,4) = "Divorced" ' logical, default FALSE
A(1,0) = NameOfRecordset ' string
A(1,1) = AppendMode ' logical, default FALSE
....

Can anyone help me with the syntax please ?

Thank you in advance,

Wim
 
It looks to me a Class is much more suitable than an Array.

Check any VBA Book you have on Class Modules.
 
Thank you for giving a straightforward answer. I'll give
it a try and let you know if I succeeded,

Wim
 
Wim said:
I was referring to the array type used in Clipper :
multidimensional, and each "cell" can be filled with
whatever data type one wants, e.g. A(0,0) can be a string,
A(1,0) can be a logical value, A(1,1) can be a string.

Ah, ok. Since you had mentioned other languages, those languages in general
force you to dimension arrays of one data type. For example:

dim arMyIntegers(20) as integer
dim i as integer

The above would result in a array that holds integers. The arrays in VB are
zero based, so you get:

for i = 0 to 20
arMyIntegers(i) = i
next i

The above would course give us an array now filled with the numbers 0 to 20
in the array.

However, in your case, you want to put *different* types of data into the
array. So, answer was, yes, you can use an array of type Variant

Dim arMyValues(20) As Variant

Since you have been exposed to languages like Pascal where they are STRONGLY
typed, then you certainly do realize that you generally can't just stuff any
type of value into a variable, but ONLY those of the correct type. The
exception of course is when you use a type of "Variant"

Further, if you remember the Pascal days, you can dim arrays with a starting
range, so you are NOT stuck with zero based arrays. So, the above could be

Dim arMyValues(1 To 20) As Variant

The above would result in a non zero based array. You can even use:

Dim arMyValues(10 to 20) As Variant

The above would only allow elements from 10 to 20!

And, if you want another dimension, you can go get:

Dim arMyValues(1, 1 To 20) As Variant

Since all dim defs are zero based, then the above really is the same as

Dim arMyValues(0 To 1, 1 To 20) As Variant

So, with the above, you can stuff you values into the array.

arMyValues(0,1) = "hello"
arMyValues(0,2) = 123

This is of course a very easy way to pass data to and from
a function.

True, but only when the data is already in an array. If you have to pull
data from a table into the array, then it is too much work. So, while the VB
langue has an a ability to deal with arrays like most programming languages,
we don't hardily EVER NEED THEM when it comes to data management.

I suppose this is kind like walking into a clipper new group, and asking
about how do you use punched cards with clipper? Most people will kind of
have a blank stares at you, or simply mention that we don't use those things
very much anymore.

Thus, if you are wondering why so many people are suggesting to use
something else in place of the array, the simple answer is that things have
changed a lot since the old days like clipper. So, we still *can* use
arrays, but for passing a bunch of data from a table or routine to another
function we have better choices. So, choosing arrays are just not the first
instinct like they were when i used FoxPro. However, in most of my ms-access
appcltions there still is few arrays used, so it is not like they are dead
or something either!
You initialise an array and then you can gradually grow it
or shrink it.

Hum, I don't remember how you make arrays grow in clipper (I did a stint in
FoxPro for about 2 years in the DOS days, so I know the dbase language..but
it has been 10 years!). In VB , you have to use the re-dim command to
increase the size of an array. YOU ALSO MUST declare the array as dynamic.

So, you can type (cut/paste!) the following into a code module, and run it.
From the debug window, you can just type in test5 to run the code (note that
the debug window is much like your command prompt, or so called "dot" prompt
in the old dbase days. After you put the code into a module and save it,
then you can re-open the module, and just whack ctrl-g for the command
prompt window.

Public Sub test5()

Dim aValues() As Variant
Dim i As Integer

For i = 1 To 10

ReDim Preserve aValues(i) As Variant
aValues(i) = "this is interation " & i

Next i

For i = 1 To 10
Debug.Print aValues(i)
Next i

End Sub
So, if I did understand your answer, it is not possible to
work like that in VBA.

Yes, you most certainly can use arrays as shown above. However it is usually
clumsy to use any array when need to pass data. We have other approaches.
However, depending on what you are trying to accomplish, the use of an array
can be just fine.

Also, here is a on-line book reference. This one applies to a97, but is
equally useful for the newer versions:

http://www.microsoft.com/accessdev/articles/bapp97/toc.htm
 
Back
Top