Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Test for empty array

Status
Not open for further replies.

Johnny42

Technical User
Jul 13, 2004
127
CA
How can I test for an empty array ?
 
If UBound(arrayName) = 0 Then
MsgBox "Array is Empty"
Else
MsgBox "Array has " & UBound(arrayName) & " values."
End If

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
I'd take a look in the VB5/6 forum, perhaps this thread thread222-836312.

Roy-Vidar
 
I tried, however I get subscript out of range error !

Dim htst() As Variant
If qqq <> "" Then
Set rs = cnn.Execute("SELECT ARCSP.IDCUST, ARCSP.IDCUSTSHPT " & _
"FROM ARCSP " & _
"WHERE (((ARCSP.IDCUST)='" & qq & "') AND ((ARCSP.IDCUSTSHPT)='" & qqq & "'))")
If rs.EOF Then
nn = nn + 1
ReDim Preserve htst(1 To n)
htst(nn) = qq

End If
 
Is it me, or should the redim statement read "ReDim Preserve htst(1 to nn)". Wouldn't the value of n be zero here unless there's more code than shown?
 
It' not you, it's me."


Yea, you are correct in your assumptions based on the code.

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
yes s/b nn
this seems to do the trick...
"msgbox isarray(testarray1)"


Code:
If qqq <> "" Then
Set rs = cnn.Execute("SELECT ARCSP.IDCUST, ARCSP.IDCUSTSHPT " & _
"FROM ARCSP " & _
"WHERE (((ARCSP.IDCUST)='" & qq & "') AND ((ARCSP.IDCUSTSHPT)='" & qqq & "'))")
If rs.EOF Then
nn = nn + 1
ReDim Preserve htst(1 To nn)
htst(nn) = qq
testarray = htst()
msgbox isarray(testarray1)
 
There is a slight problem with the solution above:

IF
Option Base is 0
OR
0 by default (eg Dim someArr(x) As someType)

Then if the array only contains 1 element,
LBound(someArr) = UBound(someArr) = 0

BUT the array is not actually empty!
 
Ravalia
Change the test to -1
ie if ubound(vArr)=-1 then msgbox "array is empty"

Check out the link that RoyVidar posted

Happy Friday

;-)
If a man says something and there are no women there to hear him, is he still wrong? [ponder]
How do I get the best answers?
 
I agree with Loomah - Roy's link is worth looking at, but I think you need to define what you mean by an empty array before you can check for it. You can define an array as ..

Dim myArray(-1 to -1)

.. if you like - it is a one-dimensional array with a single element, index -1.

If bound checking returns a number (any number) it means the array has elements - if it doesn't, then it will return an error (which you must trap) which means you have an 'array' without elements. If you don't like that, see strongm's post in Roy's link.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
I've just been playing around with a few different ideas:
Try out this sample code (should allow the user to return empty & non-empty typed arrays and parse through them without having to check for bounds or provide separate & more costly IsEmptyArray() type functions)

Option Explicit

Public Function myLongArray(Optional ByVal bEmpty As Boolean = False) As Long()
Dim lRet() As Long
If bEmpty Then
ReDim lRet(-1 To -1)
Else
ReDim lRet(1 To 2)
lRet(1) = 123
lRet(2) = 456
End If
myLongArray = lRet
End Function

Public Function myDoubleArray(Optional ByVal bEmpty As Boolean = False) As Double()
Dim dRet() As Double
If bEmpty Then
ReDim dRet(-1 To -1)
Else
ReDim dRet(1 To 2)
dRet(1) = 1.23
dRet(2) = 4.56
End If
myDoubleArray = dRet
End Function

Public Function myStringArray(Optional ByVal bEmpty As Boolean = False) As String()
Dim sRet() As String
If bEmpty Then
sRet = Split("", ":")
Else
ReDim sRet(1 To 2)
sRet(1) = "Ajay"
sRet(2) = "Ravalia"
End If
myStringArray = sRet
End Function

Public Sub myPrinter(ByVal str As String, ByVal myArr As Variant)
Dim i As Long
Debug.Print str & " contains:"
For i = Abs(LBound(myArr)) To UBound(myArr)
Debug.Print vbTab & myArr(i)
Next i
Debug.Print "End----------------" & vbCrLf
End Sub

Public Sub myMain()
myPrinter "Non-Empty Long Array", myLongArray
myPrinter "Empty Long Array", myLongArray(True)
myPrinter "Non-Empty Double Array", myDoubleArray
myPrinter "Empty Double Array", myDoubleArray(True)
myPrinter "Non-Empty String Array", myStringArray
myPrinter "Empty String Array", myStringArray(True)

End Sub


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top