Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB6: How do I detect if an array is empty?

Posted on 1999-07-15
8
Medium Priority
?
1,285 Views
Last Modified: 2008-02-01
I have a type with a bunch of string arrays in it like so (shortened for viewing pleasure):

Private Type TCriteria
    provider() As String
End Type

Then, various strings are passed to me and I use the Split command to parse them out and put the results into these arrays.

So, depending on the strings passed to me, I will load up anywhere from none to all of the arrays.

Is there a way to tell if one of these arrays has NOT been loaded up yet?

I've tried IsEmpty() IsNull() and they only work on Variants.

Any attempt to reference this array gives a "subscript out of range" error.

any hints?

thanks,
j
0
Comment
Question by:greendot
[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
8 Comments
 
LVL 3

Expert Comment

by:jjmartin
ID: 1525023
The UBound and LBound will return the upper and lower boundries of a given array.  So, if Ubound(MyArray) = LBound(MyArray) then it is empty.
0
 
LVL 3

Expert Comment

by:jjmartin
ID: 1525024
Or try, UBound(MyArray) = 0 then there are no elements in the array.
0
 
LVL 2

Author Comment

by:greendot
ID: 1525025
not if it's a single element array...

Plus, LBound and UBound return the "subscript out of range" exception as well.

I think I may just make a function call that tries to access LBound and if it fails then I know it's empty.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 5

Expert Comment

by:KDivad
ID: 1525026
If Ubound(MyArray) = LBound(MyArray) then the array has a single element.
0
 
LVL 22

Expert Comment

by:ture
ID: 1525027
Dim ub, v As TCriteria

ub=-1
On Error Resume Next
ub = Ubound(v.Provider)
On Error Goto 0

If ub = -1 Then
  MsgBox "It's really empty"
Else
  MsgBox "Thera are " & ub + 1 & "items in the array"
End If

Ture Magnusson
Karlstad, Sweden
0
 
LVL 2

Author Comment

by:greendot
ID: 1525028
I made the function:

Function IsArrayEmpty(va As Variant) As Boolean
   Dim i As Long
   On Error Resume Next
   i = LBound(va, 1)
   IsArrayEmpty = (Err <> 0)
   Err = 0
End Function

This seems to work fine.  If an array gets passed, it gets evaluated.
If anything else gets passed, it fails the LBound attempt.

Is there anything built-in to VB to do the same thing?

0
 
LVL 2

Expert Comment

by:s_lavie
ID: 1525029
go on with your function..., its seems just fine
0
 
LVL 1

Accepted Solution

by:
jcastr earned 200 total points
ID: 1525030
Just handle your error

On error goto ErrH

     blah blah blah
     blah blah blah

Finish:
     Exit Sub

ErrH:
     If Err = 9 then 'Subscript out of range
          Msgbox "Some data is missing",vbInformation
     else
          Msgbox Error,vbInformation 'Or your custom message
     end if

     Resume Fin



0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

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 While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code 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…
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…
Suggested Courses

661 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