Solved

Problem accessing non-sequentially indexed array elements in a Variant

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

932 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

10 Experts available now in Live!

Get 1:1 Help Now