Solved

How to check if VB array is empty

Posted on 2006-10-20
6
7,155 Views
Last Modified: 2010-05-18
Hi,
How do I check if the VB array is empty. The followinf code
If LBound(MyArray()) <> -99 Then DoesExist = True
works but throws a floating type exception.

So, how do I check if the array is empty without getting this exception.

Thanks
Doug
0
Comment
Question by:deming
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17772517
http://www.vbforums.com/showthread.php?t=375341

if not MyArray then DoesExists = false
If LBound(MyArray()) <> -99 Then DoesExist = True
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 17772613
Private Sub Form_Load()
Dim MyArray() As String
   
    If TestArray(MyArray) Then
       MsgBox "Array initialized"
    Else
        MsgBox "Array NOT initialized"
    End If
   
    ReDim MyArray(3)
   
    If TestArray(MyArray) Then
       MsgBox "Now Array is initialized"
    Else
        MsgBox "Array NOT initialized"
    End If
End Sub

Private Function TestArray(MyArray As Variant) As Boolean
On Error GoTo Trap
Dim lSize As Long
    lSize = LBound(MyArray)
    TestArray = True
Trap:
    Err.Clear
End Function
0
 
LVL 9

Expert Comment

by:Naveen Swamy
ID: 17776112
Private Function isArrayEmpty(arr as Variant)
  Dim i
  isArrayEmpty = False
  On Error Resume Next
  i = Ubound(arr) ' cause an error if array is empty
  If Err.Number > 0 Then Exit Function
  isArrayEmpty = True
End Function


something like this could help you
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Accepted Solution

by:
Ark earned 500 total points
ID: 17778014
'=========== Just for information about SafeArray structure - you don't need them for IsArrayDimmed fucntion======
Type SAFEARRAYBOUND
    cElements As Long  ' # of elements in the array dimension
    lLbound   As Long  ' lower bounds of the array dimension
End Type

Type SAFEARRAY
    cDims       As Integer          ' // Count of dimensions in this array.
    fFeatures   As Integer          ' // Flags used by the SafeArray routines documented below.
    cbElements  As Long             ' // Size of an element of the array.
                                    ' // Does not include size of
                                    ' // pointed-to data.
    cLocks      As Long             ' // Number of times the array has been
                                    ' // locked without corresponding unlock.
    pvData      As Long             ' // Pointer to the data.
    ' Should be sized to cDims:
    rgsabound() As SAFEARRAYBOUND  ' // One bound for each dimension.
End Type
'==============================================================================
Const VT_BYREF = &H4000&

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDest As Any, lpSource As Any, ByVal cBytes As Long)

Public Function IsArrayDimmed(TheArray As Variant) As Boolean
    Dim lp As Long, VType As Integer
    If Not IsArray(TheArray) Then Exit Function
    Dim nDims As Integer
   ' Get the pointer to the array descriptor (SAFEARRAY structure)
   ' NOTE: A Variant's descriptor, padding & union take up 8 bytes.
   CopyMemory ByVal VarPtr(VType), ByVal VarPtr(TheArray), 2
   CopyMemory ByVal VarPtr(lp), ByVal (VarPtr(TheArray) + 8), 4
   If lp = 0 Then Exit Function
   ' Test if lp is a pointer or a pointer to a pointer.
   If (VType And VT_BYREF) <> 0 Then
      ' Get real pointer to the array descriptor (SAFEARRAY structure)
      CopyMemory ByVal VarPtr(lp), ByVal lp, 4
   End If
   If lp = 0 Then Exit Function
   CopyMemory nDims, ByVal lp, 2
   IsArrayDimmed = CBool(nDims)
End Function


'========Using=======
Private Sub Command1_Click()
  Dim abArray()
  MsgBox IsArrayDimmed(abArray)
  ReDim abArray(2, 3)
  MsgBox IsArrayDimmed(abArray)
End Sub
0
 

Expert Comment

by:filemanager
ID: 17781332
Use the IsEmpty function.

If IsEmpty(MyArray()) = True Then
...
End If
0
 
LVL 45

Expert Comment

by:aikimark
ID: 17798540
Note: IsEmpty() only applies to non-array Variant variables.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question