Solved

Problem accessing non-sequentially indexed array elements in a Variant

Posted on 2000-03-21
3
285 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
[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
  • 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

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
Exit a vb6 apps when a calling it apps closes 15 61
VB6 - Scroll Mouse wheel on Picturebox 13 118
VB6 Enviroment 3 31
why PtrSafe before Function on 64bit does not fix the APIs? 2 25
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

739 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