Solved

How to check if VB array is empty

Posted on 2006-10-20
6
7,164 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

734 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