Solved

Array size

Posted on 2001-06-14
5
140 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
[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
  • 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

Industry Leaders: 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!

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 In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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…

717 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