Solved

Array size

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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
Macro: print each sheet to pdf using ExportAsFixedFormat 11 64
Using "ScreenUpdating" 6 84
TT Auto Dashboard 13 101
transition to visual .net from vb6 5 62
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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

733 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