Solved

Array size

Posted on 2001-06-14
5
138 Views
Last Modified: 2010-05-02
How do I tell if an array is empty? I use the Ubound and get a subscript out of range error. Help.

Liz
0
Comment
Question by:eware
  • 3
5 Comments
 

Accepted Solution

by:
_Rha7_ earned 50 total points
ID: 6193219

  Hope this is what u need, the key here is the On Error statement in the ALen function, go step by step thru it, On Error is usually much more useful than it may appear :)
Good luck :)

  _Rha7_


======================================================
Dim x() As Integer

Private Sub cmdAddElement_Click()
  ReDim Preserve x(ALen(x) + 1) As Integer
  MsgBox Str(ALen(x))
End Sub

Private Sub cmdDelElement_Click()
  If ALen(x) > 0 Then
    ReDim Preserve x(UBound(x) - 1) As Integer
    MsgBox ALen(x)
  Else
    MsgBox "Empty Array!"
  End If
End Sub

Function ALen(ArrayVar() As Integer)
  Dim i As Integer
  On Error GoTo IsEmpty
  i = UBound(ArrayVar)
  GoTo FunctionExit
IsEmpty:
  i = 0
FunctionExit:
  ALen = i
End Function
======================================================
0
 

Author Comment

by:eware
ID: 6193260
That did exactly what I needed it to do. Thank you very much.
0
 

Expert Comment

by:Dys060801
ID: 6198810
I was comparing the "answer" to the "question" and I'm a bit confused. The way I would have solved the problem of an empty array would be as follows:

Dim arrArray() As Integer
'Or whatever datatype your array holds

If IsEmpty(arrArray) = False Then
    ' do your code using UBound
Else
    ' MsgBox "Empty array"
    '    or
    ' raise an error using Err.Raise
End If

This has always worked for me to solve the "subscript our of range" errors. Did I miss something in the problem that would preclude using this simple solution?

Dys :)
0
 

Author Comment

by:eware
ID: 6206723
That would actually be simpler. I did not know about the IsEmpty function. I looked in help and it never talked about that.
Thanks
Liz
0
 

Author Comment

by:eware
ID: 6206817
Does this work for User Defined Type arrays? That is what I am working with.

Liz
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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 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…

856 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