Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deciding which sub to run 1

Status
Not open for further replies.

abaldwin

Programmer
Mar 27, 2001
798
0
0
US
Give VB 6, and 4 subs

Private Sub Update1()
'bla bla bla bla bla
end sub

Private Sub Update2()
'bla bla bla bla bla
end sub

Private Sub Update3()
'bla bla bla bla bla
End Sub

----->Here is the sub I need some help with
Private Sub DoUpdates
Dim iCounter as integer
dim iMax as integer
dim stString as String

'Have some code here to figure out which subs to run

'Will be running Update1, Update2, Update3....in successive
'order. But will not be runnign them out of order. i.e. 1 and 3......Just 1 then 2 then 3 or just 1 then 2 or just 1

for icounter = 1 to imax
---->Here is where I need help
call Updates & cstr(icounter)
next icounter
end sub

Basically want to know if it is possible to call a sub that I dynamically create the sub name.

I do not want to have to do a case on my Imax and do individual call statements to the subs.

On three subs it would not be bad but on 45 it would be

call updates 1
call updates 2
etc etc call updates 45

and then another case where I could start at 40 and have to call updates 40
call updates 41
et etc call updates 45

Thanks


Andy Baldwin

"Testing is the most overlooked programming language on the books!
 

Well, one simple solution would just be to call the next subroutine from within the previous instead of the original. In other words, 1 calls 2, 2 calls 3, 3 calls 4, etc. That way, if the original calls 3, it will get 3 and 4 but not 1 and 2.

-- Jeff
 
CallByName might work in this case if it is not a problem to have the subs be public.

-- Jeff
 
This is only possible if your subroutines are public methods of a class module (like your form).

If your code meets these requirements, you can call your subroutines by name as public methods of your class using CallByName function (VB6 only). See the following code.
___
[tt]
Private Sub Form_Load()
Dim N As Long
For N = 1 To 3
CallByName Me, "Update" & N, VbMethod
Next
End Sub

Public Sub Update1()
MsgBox "Update 1"
End Sub

Public Sub Update2()
MsgBox "Update 2"
End Sub

Public Sub Update3()
MsgBox "Update 3"
End Sub[/tt]
 
You could set each function up with arguments regarding how far along the track of functions to follow and where to start. For example:

sub CallingSub()
Dim n As Long ' Number of subs to call
Dim m As Long ' Starting point of subs to use - 1
...
SubSub1 n + m, m
...
end sub

------

Private Sub SubSub1(nToCall As Long, nOffset as Long)

If nOffset<1 Then
... Do Stuff ...
End If

If nToCall Then SubSub2(nToCall - 1, nOffset - 1)

End Sub

------

Private Sub SubSub2(nToCall As Long, nOffset as Long)

If nOffset<1 Then
... Do Stuff ...
End If

If nToCall Then SubSub3(nToCall - 1, nOffset - 1)

End Sub


This way, you never have to worry about loops, or calling functions that don't exist.
 
Thanks gmmastros. Exactly what I was looking for. Did not even know that function existed. Have had no use for this type of thing in MANY years.

Thanks again.

Andy Baldwin

"Testing is the most overlooked programming language on the books!
 
Truth is... I've never used it in production code either. I've never needed it. I'm glad this has helped you.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I have never needed anything like this as well. New project is building an access db on the local machine during install. Depending on what version you have and what version is being installed there are updates to run to your existing db or for creating the original db.

Since the db might already be there, it will have a version in a certain table. We are going to loop through and simply run updates until the db version is brought up to the matching version you are installing.

Thanks again for the code. Never new that function existed.


Andy Baldwin

"Testing is the most overlooked programming language on the books!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top