Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to check if VB array is empty

Posted on 2006-10-20
6
Medium Priority
?
7,203 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
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!

 
LVL 28

Accepted Solution

by:
Ark earned 2000 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 46

Expert Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

721 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