Solved

VB6 - Run Same Block Of Code Mutliple Times with Different Variable

Posted on 2014-03-27
9
166 Views
Last Modified: 2014-04-01
Good Day Experts!

I have a situation here with a VB6 project that I need some advice on as I am more versed in VB.Net.  

Behind the button click on the MainForm a variable is passed to a routine.  That variable is set with a hard-coded value since this was in all reality not going to change.  

Fast-forward...now it is changing.  

I need to run this routine for 5 values now.  What is the best way to hold those 5 values and loop through them calling the routine each time?

Thanks for your suggestions,
jimbo99999
0
Comment
Question by:Jimbo99999
[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
  • 4
  • 4
9 Comments
 
LVL 47

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 39959286
For just 5 values the easiest thing to do would be

MySub 22
MySub 74
MySub 3
MySub 14
MySub 88

Open in new window


Private Sub MySub(MyVar As Integer)

' Do something with MyVar

End Sub

Open in new window

0
 

Author Comment

by:Jimbo99999
ID: 39959310
I did not go down that path.  Good idea...just call the routine 5 times in a row changing the variable each time.
0
 
LVL 12

Expert Comment

by:jkaios
ID: 39960430
Another thing is to use a loop.

For ii = 1 to 5
   MySub ii
Next

Private Sub MySub(MyVar As Integer)
   Debug.Print MyVar
End Sub

Open in new window

0
Industry Leaders: 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!

 

Author Comment

by:Jimbo99999
ID: 39961375
Hey There

I thought about using the loop approach, but I could not figure out what to use to hold the values to iterate through.

Thanks,
jimbo99999
0
 
LVL 47

Assisted Solution

by:Martin Liss
Martin Liss earned 500 total points
ID: 39961963
If you really want to hold the values (in an array), then you can do this but I don't see the need.
Private Sub Form_Load()
Dim MyArray
Dim intIndex As Integer
MyArray = Array(22, 74, 3, 14, 88)

For intIndex = LBound(MyArray) To UBound(MyArray)
    MySub MyArray(intIndex)
Next
End Sub


Private Sub MySub(MyVar)

' Do something with MyVar

End Sub

Open in new window

0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 39965041
Did either of my suggestions resolve this for you?
0
 

Author Comment

by:Jimbo99999
ID: 39969120
Hello Everyone!

I appreciate your responses.

I can finally get back to this project after all the fire-fighting and Administrative HelpDesk type stuff the past few days...it has been hectic.

Currently I am looking at the code(another indivdual wrote it who is no longer here) better to see which method to use.

Thanks for your patience,
jimbo99999
0
 

Author Closing Comment

by:Jimbo99999
ID: 39970448
Thank you guys for the help.  I ended up calling multiple times.  Since I am not a Classic VB6 coder, I do like the Array sample that MartinLiss gave...I will keep in my archives for reference.

Thanks,
jimbo99999
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 39970461
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
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…

688 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