By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,472 Members | 2,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,472 IT Pros & Developers. It's quick & easy.

Q on the Array function...

P: n/a
MLH
How would I modify the following to achieve a 2-dimensional array?

Dim MyWeek, MyDay
MyWeek = Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
' Return values assume lower bound set to 1 (using Option Base
' statement).
MyDay = MyWeek(2) ' MyDay contains "Tue".
MyDay = MyWeek(4) ' MyDay contains "Thu".

In other words, I want the array to hold
"Monday","Segunda-feira"
"Tuesday","Terca-feira"
"Wednesday","Quarta-feira"
"Thursday","Quinta-feira"
....
Aug 31 '06 #1
Share this Question
Share on Google+
26 Replies


P: n/a
rkc
MLH wrote:
How would I modify the following to achieve a 2-dimensional array?

Dim MyWeek, MyDay
MyWeek = Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
' Return values assume lower bound set to 1 (using Option Base
' statement).
MyDay = MyWeek(2) ' MyDay contains "Tue".
MyDay = MyWeek(4) ' MyDay contains "Thu".

In other words, I want the array to hold
"Monday","Segunda-feira"
"Tuesday","Terca-feira"
"Wednesday","Quarta-feira"
"Thursday","Quinta-feira"
Why bother?
Put the data in a table and forget the array.
Or use a collection with Monday as the key and Segunda-feira
as the value.
Or create a UDT with members for English and Portuguese and
whatever other language you want and put them in a collection.
Or just about anything but a muli-dimensional array.
Aug 31 '06 #2

P: n/a
MLH wrote:
How would I modify the following to achieve a 2-dimensional array?

Dim MyWeek, MyDay
MyWeek = Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
' Return values assume lower bound set to 1 (using Option Base
' statement).
MyDay = MyWeek(2) ' MyDay contains "Tue".
MyDay = MyWeek(4) ' MyDay contains "Thu".

In other words, I want the array to hold
"Monday","Segunda-feira"
"Tuesday","Terca-feira"
"Wednesday","Quarta-feira"
"Thursday","Quinta-feira"
...
You're not looking for a 2d array, you're looking for 2x1d arrays with
coordinated indexes, which as rkc mentioned, would be much better
represented by other means.

--
Smartin
Aug 31 '06 #3

P: n/a
MLH
What would a multi-dimensional array be useful for
and why has it continued to be supported in all
successive releases of Access?

One thing, like variables - its not a requirement to
write it to disk.Imagine the headaches of declaring
50 variables as opposed to a 5x10 array.

And if there really were a substantial good reason for using an array,
how would I modify the following to achieve a 2-dimensional array?

Dim MyWeek, MyDay
MyWeek = Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
' Return values assume lower bound set to 1 (using Option Base
' statement).
MyDay = MyWeek(2) ' MyDay contains "Tue".
MyDay = MyWeek(4) ' MyDay contains "Thu".
Aug 31 '06 #4

P: n/a
MLH
I sort-a-get what you guys are saying, but am
struggling a bit. Pardon the seeming uselessness
of the following example and allow me to illustrate
my question in more detail.

Private Sub Command0_Click()
Dim i As Integer, j As Integer, PString As String
Dim WeekDays(6, 1) 'This is the 7x2 array (at least, I thought it
was)

WeekDays(0, 0) = "Sunday"
WeekDays(0, 1) = "Domingo"
WeekDays(1, 0) = "Monday"
WeekDays(1, 1) = "Segunda-feira"
WeekDays(2, 0) = "Tuesday"
WeekDays(2, 1) = "Terca-feira"
WeekDays(3, 0) = "Wednesday"
WeekDays(3, 1) = "Quarta-feira"
WeekDays(4, 0) = "Thursday"
WeekDays(4, 1) = "Quinta-feira"
WeekDays(5, 0) = "Friday"
WeekDays(5, 1) = "Sexta-feira"
WeekDays(6, 0) = "Saturday"
WeekDays(6, 1) = "Sabado"

For i = LBound(WeekDays, 1) To UBound(WeekDays, 1)
For j = LBound(WeekDays, 2) To UBound(WeekDays, 2)
MsgBox WeekDays(i, j) 'MsgBox CStr(i) & ","
& CStr(j)
Next j
Next i

For i = LBound(WeekDays, 1) To UBound(WeekDays, 1)
PString = ""
For j = LBound(WeekDays, 2) To UBound(WeekDays, 2)
If PString = "" Then
PString = WeekDays(i, j)
Else
PString = PString & " ==" & WeekDays(i, j)
End If
Next j
MsgBox PString
Next i

End Sub

Is WeekDays not a 7x2 array? And, if not, what exactly is it
that makes it a 2x1d array instead?
Aug 31 '06 #5

P: n/a
rkc
MLH wrote:
What would a multi-dimensional array be useful for
and why has it continued to be supported in all
successive releases of Access?
I don't know... a checker board, I guess.
It's been supported since GW Basic I believe.
One thing, like variables - its not a requirement to
write it to disk.Imagine the headaches of declaring
50 variables as opposed to a 5x10 array.
Imagine there's no heaven. It's easy if you try. (J. Lennon)
And if there really were a substantial good reason for using an array,
how would I modify the following to achieve a 2-dimensional array?

Dim MyWeek, MyDay
MyWeek = Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
' Return values assume lower bound set to 1 (using Option Base
' statement).
MyDay = MyWeek(2) ' MyDay contains "Tue".
MyDay = MyWeek(4) ' MyDay contains "Thu".
You really can't use Array as you have used it to create a
2 dimensional array. You could create an array of arrays if
you really wanted to.

Dim varDow as Variant

varDow = Array _
(Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"), _
(Array("Segunda-feira", "Terca-feira", "Quarta-feira","Quinta-feira" _
"Sexta-feira", "blah", "blab"))

Dim varEnglish as Variant
Dim varPortuguese as Variant

varEnglish = varDow(1)
Debug.Print varEnglish(1) 'Monday
Debug.Print varEnglish(2) 'Tuesday

varPortuguese = varDow(2)
Debug.Print varPortuguese(1) 'Segunda-feira



Aug 31 '06 #6

P: n/a
On Thu, 31 Aug 2006 19:46:08 -0400, MLH <CR**@NorthState.netwrote:
>I sort-a-get what you guys are saying, but am
struggling a bit. Pardon the seeming uselessness
of the following example and allow me to illustrate
my question in more detail.

Private Sub Command0_Click()
Dim i As Integer, j As Integer, PString As String
Dim WeekDays(6, 1) 'This is the 7x2 array (at least, I thought it
was)

WeekDays(0, 0) = "Sunday"
WeekDays(0, 1) = "Domingo"
WeekDays(1, 0) = "Monday"
WeekDays(1, 1) = "Segunda-feira"
WeekDays(2, 0) = "Tuesday"
WeekDays(2, 1) = "Terca-feira"
WeekDays(3, 0) = "Wednesday"
WeekDays(3, 1) = "Quarta-feira"
WeekDays(4, 0) = "Thursday"
WeekDays(4, 1) = "Quinta-feira"
WeekDays(5, 0) = "Friday"
WeekDays(5, 1) = "Sexta-feira"
WeekDays(6, 0) = "Saturday"
WeekDays(6, 1) = "Sabado"

For i = LBound(WeekDays, 1) To UBound(WeekDays, 1)
For j = LBound(WeekDays, 2) To UBound(WeekDays, 2)
MsgBox WeekDays(i, j) 'MsgBox CStr(i) & ","
& CStr(j)
Next j
Next i

For i = LBound(WeekDays, 1) To UBound(WeekDays, 1)
PString = ""
For j = LBound(WeekDays, 2) To UBound(WeekDays, 2)
If PString = "" Then
PString = WeekDays(i, j)
Else
PString = PString & " ==" & WeekDays(i, j)
End If
Next j
MsgBox PString
Next i

End Sub

Is WeekDays not a 7x2 array? And, if not, what exactly is it
that makes it a 2x1d array instead?
I think what they are trying to tell you is that the Array function
returns a one dimension array. You wouldn't be able to populate a two
dimension array, using the Array function, to get the result you want.

The suggestions were to either put the data in a table and use it like
an array, or populate the array from the table, which could be done
with a single command. Or alternatively, use 2 single dimension
arrays.

I hope this helps to clear the confusion.

-=-=-=-=-=-=-=-=-=-=-=-=
Randy Harris
tech at promail dot com
Sep 1 '06 #7

P: n/a
MLH <CR**@NorthState.netwrote:
: How would I modify the following to achieve a 2-dimensional array?

: Dim MyWeek, MyDay
: MyWeek = Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
: ' Return values assume lower bound set to 1 (using Option Base
: ' statement).
: MyDay = MyWeek(2) ' MyDay contains "Tue".
: MyDay = MyWeek(4) ' MyDay contains "Thu".

: In other words, I want the array to hold
: "Monday","Segunda-feira"
: "Tuesday","Terca-feira"
: "Wednesday","Quarta-feira"
: "Thursday","Quinta-feira"
: ...

Would a one-dimensional array do?

MyWeek = Array("Mon", "Tue", "Wed", "Thu", _
"Fri", "Sat", "Sun", "Segunda-feira", "Terca-feira", ...)

EnglishTue = MyWeek(1)
PortugueseTue = MyWeek(8)

In general:
EnglishDay = MyWeek(DayNumber)
PortugueseDay = MyWeek(DayNumber + 7)

--thelma
Sep 1 '06 #8

P: n/a
rkc
MLH wrote:
I sort-a-get what you guys are saying, but am
struggling a bit. Pardon the seeming uselessness
of the following example and allow me to illustrate
my question in more detail.

Private Sub Command0_Click()
Dim i As Integer, j As Integer, PString As String
Dim WeekDays(6, 1) 'This is the 7x2 array (at least, I thought it
was)

WeekDays(0, 0) = "Sunday"
WeekDays(0, 1) = "Domingo"
WeekDays(1, 0) = "Monday"
WeekDays(1, 1) = "Segunda-feira"
WeekDays(2, 0) = "Tuesday"
WeekDays(2, 1) = "Terca-feira"
WeekDays(3, 0) = "Wednesday"
WeekDays(3, 1) = "Quarta-feira"
WeekDays(4, 0) = "Thursday"
WeekDays(4, 1) = "Quinta-feira"
WeekDays(5, 0) = "Friday"
WeekDays(5, 1) = "Sexta-feira"
WeekDays(6, 0) = "Saturday"
WeekDays(6, 1) = "Sabado"

For i = LBound(WeekDays, 1) To UBound(WeekDays, 1)
For j = LBound(WeekDays, 2) To UBound(WeekDays, 2)
MsgBox WeekDays(i, j) 'MsgBox CStr(i) & ","
& CStr(j)
Next j
Next i

For i = LBound(WeekDays, 1) To UBound(WeekDays, 1)
PString = ""
For j = LBound(WeekDays, 2) To UBound(WeekDays, 2)
If PString = "" Then
PString = WeekDays(i, j)
Else
PString = PString & " ==" & WeekDays(i, j)
End If
Next j
MsgBox PString
Next i

End Sub

Is WeekDays not a 7x2 array? And, if not, what exactly is it
that makes it a 2x1d array instead?
Weekdays is a 7x2 array, but you didn't get there by
using the Array() function.

There are also much easier ways of managing the kind
of data you would use a multi-dimensional array for.


Sep 1 '06 #9

P: n/a
MLH <CR**@NorthState.netwrote:
: Dim WeekDays(6, 1) 'This is the 7x2 array (at least, I thought it
: was)
/\ You're actually defining a 6x1 array here.
|| Weekdays(7,2) defines a 7x2 array.

--thelma

Sep 1 '06 #10

P: n/a
Thelma Lubkin wrote:
MLH <CR**@NorthState.netwrote:
Dim WeekDays(6, 1) 'This is the 7x2 array (at least, I thought it
was)
/\ You're actually defining a 6x1 array here.
|| Weekdays(7,2) defines a 7x2 array.

--thelma
The array bounds start at 0,0 so (6, 1) actually is 7 rows and 2 columns.

--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com
Sep 1 '06 #11

P: n/a
Rick Brandt <ri*********@hotmail.comwrote:
: Thelma Lubkin wrote:
:MLH <CR**@NorthState.netwrote:
: Dim WeekDays(6, 1) 'This is the 7x2 array (at least, I thought it
: was)
: /\ You're actually defining a 6x1 array here.
: || Weekdays(7,2) defines a 7x2 array.
:>
: --thelma

: The array bounds start at 0,0 so (6, 1) actually is 7 rows and 2 columns.

Thank you. I thought the term 'Dim' was an abbreviation for
'Dimension', which would indicate the size of the array. But
apparently it's not and knowing the array's 'Dim' is not enough
to tell you its size. (you also need to know the currently
defined lower bound option). This seems strange to me.
--thelma
: --
: Rick Brandt, Microsoft Access MVP
: Email (as appropriate) to...
: RBrandt at Hunter dot com
Sep 1 '06 #12

P: n/a
MLH
Yes, you're quite right. Its clear
now. Trying to circumvent that
is clearly a waste of time. I'll stick
to the Dim statemnt for multi-dim-
ensional arrays.

Thx 2U all.
Sep 1 '06 #13

P: n/a
MLH
Took me a while, but I finally
got it. Many thx.
Sep 1 '06 #14

P: n/a
MLH
On 1 Sep 2006 05:46:27 GMT, Thelma Lubkin <th****@alpha2.csd.uwm.edu>
wrote:
>MLH <CR**@NorthState.netwrote:
: Dim WeekDays(6, 1) 'This is the 7x2 array (at least, I thought it
: was)
/\ You're actually defining a 6x1 array here.
|| Weekdays(7,2) defines a 7x2 array.

--thelma
In my experience, it has resulted in a 8x3 array - but I always
have option base zero as a default in my procedures
Sep 1 '06 #15

P: n/a
MLH
Yeah, I think it would. Perfectly good
suggestion. Thx.
Sep 1 '06 #16

P: n/a

It wouldn't seem so strange if you were aware that historically programmers
count from 0 not 1.

You can use the Option Base statement which sets the default lower bound for
a module, the options are only 0 or 1 though, you are better off, for
documentation purposes, defining lower and upper bounds. e.g.
Dim WeekDays(0 to 6, 0 to 1)

Interestingly you can get an upper bound which is smaller than the lower
bound. The following code

Dim varX As Variant

varX = Array()

Debug.Print "Lower Bound = "; LBound(varX)
Debug.Print "Upper Bound = "; UBound(varX)
Erase varX

prints
Lower Bound = 0
Upper Bound = -1

This is a quite useful technique as you can test the condition that the
upper bound is smaller than the lower bound to see if you have any elements
in your array. e.g.

Dim varX As Variant
Dim intX As Integer
Dim strRet As String

varX = Array()

For intX = 0 To 3
strRet = InputBox("enter something")
If Len(strRet) 0 Then
ReDim Preserve varX(0 To UBound(varX) + 1)
varX(UBound(varX)) = strRet
strRet = ""
End If
Next

If UBound(varX) < LBound(varX) Then
MsgBox "You didn't enter any values"
Else
MsgBox "You entered " & UBound(varX) - LBound(varX) + 1 & " value(s)"
End If

Erase varX

--

Terry Kreft
"Thelma Lubkin" <th****@alpha2.csd.uwm.eduwrote in message
news:ed**********@uwm.edu...
Rick Brandt <ri*********@hotmail.comwrote:
: Thelma Lubkin wrote:
:MLH <CR**@NorthState.netwrote:
: Dim WeekDays(6, 1) 'This is the 7x2 array (at least, I thought it
: was)
: /\ You're actually defining a 6x1 array here.
: || Weekdays(7,2) defines a 7x2 array.
:>
: --thelma

: The array bounds start at 0,0 so (6, 1) actually is 7 rows and 2
columns.
>
Thank you. I thought the term 'Dim' was an abbreviation for
'Dimension', which would indicate the size of the array. But
apparently it's not and knowing the array's 'Dim' is not enough
to tell you its size. (you also need to know the currently
defined lower bound option). This seems strange to me.
--thelma
: --
: Rick Brandt, Microsoft Access MVP
: Email (as appropriate) to...
: RBrandt at Hunter dot com


Sep 1 '06 #17

P: n/a
Terry Kreft <te*********@mps.co.ukwrote:

: It wouldn't seem so strange if you were aware that historically programmers
: count from 0 not 1.

No, that's not why. I programmed in C and C++ for enough years
to think that arrays naturally start at 0.

But I also go further back in history, to Fortran, before
modern Fortran, before even Fortran70. When I declared an
array as dimension(7,2) in that ancient tongue, it meant
that its first index had 7 elements and its second index 2:
'dimension' was used to set the size of the array,
not its upper bound [all array indices started at 1, so of
course you were also defining its upper bound, but that was
irrelevant since no choice existed in the matter.]

--thelma

<snip interesting example>

: Terry Kreft
:: The array bounds start at 0,0 so (6, 1) actually is 7 rows and 2
: columns.
:>
: Thank you. I thought the term 'Dim' was an abbreviation for
: 'Dimension', which would indicate the size of the array. But
: apparently it's not and knowing the array's 'Dim' is not enough
: to tell you its size. (you also need to know the currently
: defined lower bound option). This seems strange to me.
: --thelma
:: --
:: Rick Brandt, Microsoft Access MVP
:: Email (as appropriate) to...
:: RBrandt at Hunter dot com
:>
:>
Sep 2 '06 #18

P: n/a
MLH wrote:
What would a multi-dimensional array be useful for
and why has it continued to be supported in all
successive releases of Access?
Arrays certainly have their purpose. They are useful for populating and
analyzing data in a bona fide matrix. A 2d array might be better off in
a table or recordset, but if the demand for the data is temporal, or
needs sophisticated analysis, it might not. A 3d array would be a sore
proposition to represent in tables (I'd bet it's possible, but I can't
think about it right now). I've never had a need for a 4d array. I
suppose somebody has?

If you would like to see a good implementation of a 2d array have a look
at this:
http://www.accessmvp.com/DJSteele/Access/AA200504.zip
Then look at the PDF, pages 4-5. It's a bit technical, but I think this
is a good case.

When you have essentially two lists with a common marker or ID in a
database environment, this does not seem a good case for an array. This
is representative of table structure, with one column as the common
identifier, and another as the dependent data.

HTH

--
Smartin
Sep 2 '06 #19

P: n/a
Ah, well, your initial statement seemed to display a level of ignorance (in
the true sense of the word, not as an insult) to justify my response.
--

Terry Kreft
"Thelma Lubkin" <th****@alpha2.csd.uwm.eduwrote in message
news:ed**********@uwm.edu...
Terry Kreft <te*********@mps.co.ukwrote:

: It wouldn't seem so strange if you were aware that historically
programmers
: count from 0 not 1.

No, that's not why. I programmed in C and C++ for enough years
to think that arrays naturally start at 0.

But I also go further back in history, to Fortran, before
modern Fortran, before even Fortran70. When I declared an
array as dimension(7,2) in that ancient tongue, it meant
that its first index had 7 elements and its second index 2:
'dimension' was used to set the size of the array,
not its upper bound [all array indices started at 1, so of
course you were also defining its upper bound, but that was
irrelevant since no choice existed in the matter.]

--thelma

<snip interesting example>

: Terry Kreft
:: The array bounds start at 0,0 so (6, 1) actually is 7 rows and 2
: columns.
:>
: Thank you. I thought the term 'Dim' was an abbreviation for
: 'Dimension', which would indicate the size of the array. But
: apparently it's not and knowing the array's 'Dim' is not enough
: to tell you its size. (you also need to know the currently
: defined lower bound option). This seems strange to me.
: --thelma
:: --
:: Rick Brandt, Microsoft Access MVP
:: Email (as appropriate) to...
:: RBrandt at Hunter dot com
:>
:>


Sep 2 '06 #20

P: n/a
MLH
>Arrays certainly have their purpose. They are useful for populating and
analyzing data in a bona fide matrix. A 2d array might be better off in
a table or recordset, but if the demand for the data is temporal, or
needs sophisticated analysis, it might not. A 3d array would be a sore
proposition to represent in tables (I'd bet it's possible, but I can't
think about it right now). I've never had a need for a 4d array. I
suppose somebody has?
Only once - length, height, width & temperature data.
Sep 5 '06 #21

P: n/a
That should still only be a 2d array.

(0 to 3, 0 to NumRecords)

--

Terry Kreft
"MLH" <CR**@NorthState.netwrote in message
news:f3********************************@4ax.com...
>
Arrays certainly have their purpose. They are useful for populating and
analyzing data in a bona fide matrix. A 2d array might be better off in
a table or recordset, but if the demand for the data is temporal, or
needs sophisticated analysis, it might not. A 3d array would be a sore
proposition to represent in tables (I'd bet it's possible, but I can't
think about it right now). I've never had a need for a 4d array. I
suppose somebody has?
Only once - length, height, width & temperature data.

Sep 6 '06 #22

P: n/a
MLH
Unless your plotting physical effect of coefficient of thermal
expansion over a 100 degree range for many combinations
of length, width height of a given material. Then you've got
yourself a 3-d data block.
Sep 6 '06 #23

P: n/a
Terry Kreft <te*********@mps.co.ukwrote:
: That should still only be a 2d array.

: (0 to 3, 0 to NumRecords)

How about simulations, where you're generating massive amounts
of random data on the fly?
--thelma
: --

: Terry Kreft
: "MLH" <CR**@NorthState.netwrote in message
: news:f3********************************@4ax.com...
:Only once - length, height, width & temperature data.
Sep 6 '06 #24

P: n/a
Yes, but that isn't what you said originally, when you claimed a four
dimenional array.
--

Terry Kreft
"MLH" <CR**@NorthState.netwrote in message
news:qt********************************@4ax.com...
Unless your plotting physical effect of coefficient of thermal
expansion over a 100 degree range for many combinations
of length, width height of a given material. Then you've got
yourself a 3-d data block.

Sep 7 '06 #25

P: n/a
That would depend on what your simulating.

If you reference MLH's reply you'll see he's now modified his problem space
so that he would now need a three dimensional array.

Of course that doesn't alter the fact that his original description would
have been satisfied by a two dimensional array.
--

Terry Kreft
"Thelma Lubkin" <th****@alpha2.csd.uwm.eduwrote in message
news:ed**********@uwm.edu...
Terry Kreft <te*********@mps.co.ukwrote:
: That should still only be a 2d array.

: (0 to 3, 0 to NumRecords)

How about simulations, where you're generating massive amounts
of random data on the fly?
--thelma
: --

: Terry Kreft
: "MLH" <CR**@NorthState.netwrote in message
: news:f3********************************@4ax.com...
:Only once - length, height, width & temperature data.


Sep 7 '06 #26

P: n/a
MLH
On Thu, 7 Sep 2006 07:53:07 +0100, "Terry Kreft"
<te*********@mps.co.ukwrote:
>Yes, but that isn't what you said originally, when you claimed a four
dimenional array.
Yeah, you're right. I went back 'n read it.
Big oops!
Sep 8 '06 #27

This discussion thread is closed

Replies have been disabled for this discussion.