Solved

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

Posted on 2014-03-27
9
158 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
  • 4
  • 4
9 Comments
 
LVL 45

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
 

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

 
LVL 45

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 45

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 45

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

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

758 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

18 Experts available now in Live!

Get 1:1 Help Now