how to discern empty array in code

P

Phil Stokes

I have an array that draws its data from a recordset using various criteria.
On occasion there is no data in the recordset that matches the criteria.

I am trying to code an if statement that will run through some code if there
are entries in the array.

I have tried IsNull(), IsEmpty() IsArray() and none work

The information "subscripts out of range" appears for the array when any of
the above are tried.

thanks

Phil Stokes
 
J

Jean-Guy Marcil

Phil Stokes was telling us:
Phil Stokes nous racontait que :
I have an array that draws its data from a recordset using various
criteria. On occasion there is no data in the recordset that matches
the criteria.

I am trying to code an if statement that will run through some code
if there are entries in the array.

I have tried IsNull(), IsEmpty() IsArray() and none work

The information "subscripts out of range" appears for the array when
any of the above are tried.

Try this code.
(Uncomment
'ReDim myArray(4)
'For i = 0 To 4
' myArray(i) = "Item # " & i
'Next
to test with a "non null" array.)

'_______________________________________
Dim MyArray() As String
Dim i As Long

'ReDim myArray(4)
'For i = 0 To 4
' myArray(i) = "Item # " & i
'Next

If (Not MyArray) = True Then
MsgBox "The array is empty"
Exit Sub
Else
i = UBound(MyArray)
MsgBox "The array has " & i + 1 & " elements"
'_______________________________________
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
[email protected]
Word MVP site: http://www.word.mvps.org
 
O

Ol

Try this:

For i = LBound(YourArrayName) To UBound(YourArrayName)
'do something
next i

And youll never see "subscripts out of range" message again :)
 
J

Jean-Guy Marcil

Ol was telling us:
Ol nous racontait que :
Try this:

For i = LBound(YourArrayName) To UBound(YourArrayName)
'do something
next i

And youll never see "subscripts out of range" message again :)

Yes you will.... When the Array is "null", i.e. Declared but not yet
dimensionned, as in:

'_______________________________________
Dim MyArray() As String
Dim I as Long

For i = LBound(MyArray) To UBound(MyArray)
'do something
Next i
'_______________________________________

Whereas, the following prevents the error:

'_______________________________________
Dim MyArray() As String
Dim i As Long

If (Not MyArray) = True Then
MsgBox "The array is empty"
Exit Sub
Else
For i = LBound(MyArray) To UBound(MyArray)
'do something
Next i
End If
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
[email protected]
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

Jean-Guy Marcil was telling us:
Jean-Guy Marcil nous racontait que :

Sorry, it is missing the
End If
right at the end...

Too fast on the Coy/Paste/Send message...
Try this code.
(Uncomment
'ReDim myArray(4)
'For i = 0 To 4
' myArray(i) = "Item # " & i
'Next
to test with a "non null" array.)

'_______________________________________
Dim MyArray() As String
Dim i As Long

'ReDim myArray(4)
'For i = 0 To 4
' myArray(i) = "Item # " & i
'Next

If (Not MyArray) = True Then
MsgBox "The array is empty"
Exit Sub
Else
i = UBound(MyArray)
MsgBox "The array has " & i + 1 & " elements"

End If
'_______________________________________
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
[email protected]
Word MVP site: http://www.word.mvps.org

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
[email protected]
Word MVP site: http://www.word.mvps.org
 
Top