How to Detect an Empty Array

Posted on 2003-03-19
Medium Priority
Last Modified: 2010-08-05
Hi all,

This should be easy, but I cannot seem to find the answer.

I have an array which I initialize empty:

Dim MyArray() as MyUserDataType

Now in my code, I want to determine the UBOUND of MyArray.  If I do something like this:

for i = 0 to ubound(Myarray) then.....

I get an error because MyArray is empty at this point.  How can I test if MyArray is empty and I should not be testing for the ubound?  By the way, I need to use base 0 if that makes any difference.

Question by:GeneM
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
LVL 38

Accepted Solution

PaulHews earned 200 total points
ID: 8167018
Sorry, but there is no simple way other than error checking to find if your array is dimensioned.  You can do this:

    Dim x() As MyDataType
    Dim i As Long
    On Error Resume Next
    i = UBound(x)
    If Err.Number = 9 Then
        MsgBox "Empty Array"
    End If
    On Error GoTo 0

But it is best to dimension an array as soon as you can to avoid this error.  If you need to create an uninitialized array that you can test, do this:

Dim x() as MyDataType
Redim x(-1 to -1)

Now when you check the Ubound, it will return -1 indicating an uninitialized array.

Expert Comment

ID: 8167020

This should do the trick:

   On Local Error GoTo empty_array
      Dim iLocation As Integer
      iLocation = UBound(myArray)

      Exit Sub

      If Err.Number = 9 Then
        'theres no data in array
      End If

   End Sub

LVL 38

Expert Comment

ID: 8167038
By the way, for simple arrays you can wrap the error testing in a function, but it is problematic with usertype arrays because they are not passed as parameters so easily.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 8167039
On error resume next
a = Myarray(0)
If err then 'Array not initalized
   Iniyt Array
End if


John McCann

Expert Comment

ID: 8167190
The easiest thing to do, though it takes a little more memory is to alter how you define the array.

Instead of:
Dim MyArray() as MyUserDataType
Change it to:
Dim MyArray

This will make it a variant.  Then before checking the ubound do a:
if isarray(myarray) then
   'do the ubound checks.
end if

If it is still a variant, then it hasn't been set yet.  Of course, when you want to set it, you have to redim before using it, unless you are splitting data into it.

There is also a isempty function, but I don't know if you can use that for a defined array.

Author Comment

ID: 8170779
Thanks to all who answered.

I accepted Paul's solution because he was the first to talk about intercepting the error.  I also liked his solution of doing the Redim x(-1 to -1).  I didn't know it would accept negative bounds!  

The use of the variant was a good solution.  However, I personally just don't like building arrays in a variant.  I cannot defend that position.  Its just not my style! :-)

It just seems that VB should offer a way to test for an "empty" array.

Again, Thanks to all.


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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
Course of the Month9 days, 21 hours left to enroll

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