Test if array contains any elements?

vknierim

Registered User.
Local time
Today, 23:03
Joined
May 12, 2004
Messages
13
Hi,

does anyone have an idea how I can test if an array contains ANY elements?
What I'm trying to do is to pass an array as the first parameter of a function together with a new value for this array as the second parameter of the function. This function should append the new value to the array and return the array. The function is then called repeatedly to append new elements to the array.

The problem I can't solve is how to determine if the passed array has ANY element in it (this will be the case when the function is called for the first time). :confused: I need to determine the correct Ubound of the array to properly expand it.

Any Ideas on how to do that would be greatly appreciated.

Thanks

Volker
 
if isarray(arrayname) then
'do something
else
'do something else
end if
 
Hello,

I tried that, but the IsArray() function always returns true. The function looks like this

Code:
private function add_to_array(aryArg1() as String, strArg2 as String) as String()
Dim x as Long

If IsArray(aryArg1) then
   x = Ubound(aryArg1) +1
   Redim aryArg1(x)
else
   Redim aryArg1(0)
endif

....

And the function is called like this

Code:
Dim aryTest() as String
...
aryTest = add_to_array(aryTest, "Hello")
...

The program always stops with an error when it tries to assign

Code:
   x = Ubound(aryArg1) + 1
with an "Array Index out of bounds" error which is reasonable because the passed array hasn't been "dimensioned" yet. But I thought the IsArray() function should check for this and the "Else Part" of the code should be executed. But for some strange reason that doesn't happen.
By the way I'm using Access XP under Win 2k.

Volker
 
Hi,

I found an solution to the problem myself, wich isn't very elegant I think, but at least it works. The following code does the job: the error still occurs but it is catched by the program and then handled appropriatly.
Code:
Private Function add_to_array(aryArg1() As String, strArg2 As String) As String()
Dim x As Long 'new "size" of Array
On Error GoTo Handle_Error

x = UBound(aryArg1) + 1
ReDim Preserve aryArg1(x)
' append the new item
aryArg1(x) = strArg2

'return
add_array = arg_Array
Exit Function

Handle_Error:
Select Case Err.Number

Case 9:
    ' Ubound caused Runtime Error No. 9 ,since the array hasn't been dimensioned yet. Set the number of elements x to zero and resume with the code
    x = 0
    Resume Next
Case Else
    ' any other error is displayed on the screen
    MsgBox "Runtime Error " & Err.Number & ": " & Err.Description, vbCritical
End Select
End Function
 
Force the error:

Code:
Public Function TestArray(aData() As Variant) As Boolean
    On Error GoTo Err_TestArray
   
    Dim intCatch As Integer
    intCatch = UBound(aData()) - LBound(aData())
    TestArray = True
     
Exit_TestArray:
    Exit Function
    
Err_TestArray:
    TestArray = False
    Resume Exit_TestArray

End Function


Just pass your array to the function...

Code:
If TestArray(myArray()) Then
    MsgBox "A legal array"
Else
    MsgBox "Not dimensioned array"
End If
 
Encapsulating the array-test in a separate function is more elegant than my approach. Thanks for the help.
 

Users who are viewing this thread

Back
Top Bottom