Solved

Problem accessing non-sequentially indexed array elements in a Variant

Posted on 2000-03-21
3
277 Views
Last Modified: 2010-05-02
I have a control array with non-sequential indexes (i.e 3, 7, 9, 15) which is completely valid in VB (and often useful).  It is passed to a function as a Variant (apparently the only way to pass it).

As a Variant inside the function I can't figure out how to loop through the elements.

With a plain old array you can use LBound and UBound and ignore the invalid index errors that occur along the way with an On Error statement (perhaps there is a better way).  But with the Variant LBound and UBound don't work.

any ideas?
0
Comment
Question by:jleinbach
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
mcrider earned 150 total points
ID: 2640717
Here you go...

Dim Element As Variant

Load Label1(5)
Load Label1(6)
Load Label1(11)
For Each Element In Label1
    Debug.Print Element.Index
Next


Cheers!
0
 

Author Comment

by:jleinbach
ID: 2640766
This solves the problem.  Thank you.

As a follow up: Is it possible to pass the control array to the function any way *other than* as a Variant?

Also, does it make sense, if I have a control array that does not need to be passed, to assign it to a Variant anyway, just so I can use the For Each construct rather than looping from LBound to UBound and ignoring the 'Index Does Not Exist' errors along the way?

Seems to be preferable, but can you provide any addition insight.

Thanks again for the help.
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2640904
Thanks for the points! Glad I could help!

BTW, are you talking about something like this:

   Function TestIt(ObjName As Object)
       Dim Element As Variant
       For Each Element In ObjName
           Debug.Print Element.Index
       Next
   End Function

   Private Sub Command1_Click()
       Load Label1(5)
       Load Label1(6)
       Load Label1(11)
       TestIt Label1
   End Sub


Cheers!®©
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…

828 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