Solved

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

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

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…

863 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

24 Experts available now in Live!

Get 1:1 Help Now