simple and yet stupid question... Please help

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm filling an array in a function then returning the array to the calling
procedure. I need the calling procedure to know if the array has any
contents. How can I determine the count of elements of an array?
 
Sorry, I my thought processes weren't completely working when I posted this.
I tried ubound and lbound. My problem is that sometimes there are no
elements in the array and I'm getting subscript out of range or array not
initialized when i used it in a for each x in thearray.
 
Just test the variable with the IsArray function. If result is True, the
variable contains at least one element in the array.
 
I don't know what I'm doing wrong then. I tried isarray(thearray) in the
immediate window while it was suspended for debugging and it returned true
even though there where no elements in the array. I got around it by adding
to my function that returns the array to fill the first element with a no
information message so that at least one element would be present. That
works for this part of my program, but I would like to know what I was doing
wrong so I don't run into this problem in the future. I can post my original
code if you think it might help.
 
How do you Dim the array variable?

If you use this:
Dim varArray()

or this
Dim varArray(4)

or this
Dim varArray(1 To 3)

then IsArray will return positive.

If you use this
Dim varArray

then it won't show as an array until you fill it from the Split function or
some other filling excercise.

You don't tell me any more details about the code that is giving the
subscript out of range error, so I cannot speak to that.

--

Ken Snell
<MS ACCESS MVP>
 
I DIMmed the array like this

Dim arrUsersInGroup() as string

I then call a routine that fills an array and returns it like this
arrUsersInGroup = ListUsersOfGroup(arrGroupList(loop1count))
in the code below and at For Each loop2 In arrUsersInGroup
is where I got the subscript out of range when there were no elements. So I
modified ListUsersOfGroup() to return at least one element. But if I dim
the array like this dim arrUsersInGroup as string can I return an array to
it from the function ListUsersOfGroup() without an error?

loop1count = 0
loop2count = 0
For Each loop1 In arrGroupList
strWriteString = strWriteString & vbCrLf & vbTab &
arrGroupList(loop1count)
arrUsersInGroup = ListUsersOfGroup(arrGroupList(loop1count))
For Each loop2 In arrUsersInGroup
strWriteString = strWriteString & vbCrLf & vbTab & vbTab &
arrUsersInGroup(loop2count)
loop2count = loop2count + 1
Next
loop1count = loop1count + 1
loop2count = 0
Next
 
When you use this type of dim:
Dim arrUsersInGroup() as string

your code at some point must redimension the array, example:
ReDim arrUsersInGroup(15) As String

Until you do the ReDim, the array has no size and thus the subscripts will
be out of range because there is no range.

--

Ken Snell
<MS ACCESS MVP>
 
It only doen't work if the function passes back an array with no values.
The function has a private array that gets redimmed to the appropriate
value. It is that array that is passed back into the array in this sub.

Thanks for your help. I understand it a little better now.
 
Not having access to all your code, I would suggest that your function
should always redim the array variable to have at least 1 item in the array.
Or rewrite the function so that the error cannot occur at all. Or trap for
the "subscript out of range" error and ignore it while having your code do
what you want it to do in that circumstance.

--

Ken Snell
<MS ACCESS MVP>
 
Thanks, what I ended up doing was to make sure the function always returned
one element with contents that let me know there is no valid data. Thanks
for all your help.
 
Back
Top