Solved

How to check if VB array is empty

Posted on 2006-10-20
6
7,119 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]
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA to copy paste columns form one file to other 20 86
Microsoft Access combo box help 2 38
how to know if my Checkbox is True in VB6.0? 9 41
Advice in Xamarin 21 57
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

863 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

20 Experts available now in Live!

Get 1:1 Help Now