Solved

Array size

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

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…
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…
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…
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…

823 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