Solved

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

Posted on 2014-03-27
9
162 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 46

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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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 46

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 46

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 46

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
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…
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…

776 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