Post by BillyI want to use UBound to check the upper bound of an array, so I can loop
round the array and get the information, but if the array is empty, I get an
error
"Subscript out of range"
So...bearing in mind I need to loop through the array, how do I check if the
array is populated or not. I don't reallyw ant to put an empty index in
there...kind of defetas the object of the ubound...
Thx
Check out
http://www.codehound.com/groups/default.asp?t=1,14,3,***@tkmsftngp07_14
but as it contains a caveat, just copy the code underneath, based on the
link mentioned and http://www.vbadvance.com/arrays.htm (down at the
moment?).
<code>
Private Const S_OK As Long = &H0
Private Const DISP_E_BADINDEX As Long = &H8002000B
Private Const E_INVALIDARG As Long = &H80070057
Private Const VT_BYREF As Integer = &H4000
Private Const VARIANT_DATA_OFFSET As Long = 8
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" ( _
ByVal psa As Long) As Long
Private Declare Function SafeArrayGetLBound Lib "oleaut32.dll" ( _
ByVal psa As Long, ByVal nDim As Long, ByRef plLbound As Long) As Long
Private Declare Function SafeArrayGetUBound Lib "oleaut32.dll" ( _
ByVal psa As Long, ByVal nDim As Long, ByRef plUbound As Long) As Long
'-------------------------------------------------------------------------------------------'
Public Function LBoundEx(ByRef vArray As Variant, _
Optional ByVal lDimension As Long = 1) As Long
Dim iDataType As Integer
Dim psa As Long
Dim lRetval As Long
'Make sure an array was passed in:
If IsArray(vArray) Then
'Try to get the pointer:
CopyMemory psa, ByVal VarPtr(vArray) + VARIANT_DATA_OFFSET, 4
If psa Then
'If ByRef then dereference the pointer to get the actual
"pointer:
CopyMemory iDataType, vArray, 2
If iDataType And VT_BYREF Then CopyMemory psa, ByVal psa, 4
If psa Then
'Make sure this is a valid array dimension:
If lDimension > 0 And _
lDimension <= SafeArrayGetDim(psa) Then
'Get the LBound:
lRetval = SafeArrayGetLBound( _
psa, lDimension, LBoundEx)
If lRetval <> S_OK Then LBoundEx = -1
Else
Err.Raise vbObjectError Or 10000, _
"LBoundEx", "Invalid Dimension"
End If
Else
LBoundEx = -1
End If
Else
LBoundEx = -1
End If
Else
Err.Raise vbObjectError Or 10000, "LBoundEx", "Not an array"
End If
End Function
Public Function UBoundEx(ByRef vArray As Variant, _
Optional ByVal lDimension As Long = 1) As Long
Dim iDataType As Integer
Dim psa As Long
Dim lRetval As Long
'Make sure an array was passed in:
If IsArray(vArray) Then
'Try to get the pointer:
CopyMemory psa, ByVal VarPtr(vArray) + VARIANT_DATA_OFFSET, 4
If psa Then
'If ByRef then dereference the pointer to get the actual
'pointer:
CopyMemory iDataType, vArray, 2
If iDataType And VT_BYREF Then CopyMemory psa, ByVal psa, 4
If psa Then
'Make sure this is a valid array dimension:
If lDimension > 0 And _
lDimension <= SafeArrayGetDim(psa) Then
'Get the UBound:
lRetval = SafeArrayGetUBound( _
psa, lDimension, UBoundEx)
If lRetval <> S_OK Then UBoundEx = -1
Else
Err.Raise vbObjectError Or 10000, _
"UBoundEx", "Invalid Dimension"
End If
Else
UBoundEx = -1
End If
Else
UBoundEx = -1
End If
Else
Err.Raise vbObjectError Or 10000, "UBoundEx", "Not an array"
End If
End Function
</code>
Sinna