Solved

Array size

Posted on 2001-06-14
5
135 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

708 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

15 Experts available now in Live!

Get 1:1 Help Now