Solved

Problem accessing non-sequentially indexed array elements in a Variant

Posted on 2000-03-21
3
260 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

707 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

14 Experts available now in Live!

Get 1:1 Help Now