?
Solved

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

Posted on 2014-03-27
9
Medium Priority
?
167 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 49

Accepted Solution

by:
Martin Liss earned 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 49

Assisted Solution

by:Martin Liss
Martin Liss earned 2000 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 49

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 49

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month11 days, 17 hours left to enroll

752 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