Solved

How to check if VB array is empty

Posted on 2006-10-20
6
7,101 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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 27

Accepted Solution

by:
Ark earned 500 total points
Comment Utility
'=========== 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
Comment Utility
Use the IsEmpty function.

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

Expert Comment

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now