Visual Basic

Moderators: None (Apply to moderate this forum)
Number of threads: 18013
Number of posts: 55386

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
calling procedures dynamically Posted by siskomike on 10 Jun 2006 at 12:44 PM
how can i call procedures faster when i have lets say 2 procedures with same name but with a number on the end
the code i have in the command button is wrong syntax, is there anyway i can change that around or do something different?


Private Sub Level1()
'code
end sub

Private Sub Level2()
'code
end sub()

Private sub command1_click()
call "level" & intLevel 'int level being a number between 1 and 2
end sub
Report
Re: calling procedures dynamically Posted by Barkeeper on 12 Jun 2006 at 3:33 AM
Private Sub Level(Byval Index as Integer)
'Code
End Sub

Private sub command1_click()
call level(intLevel) 'int level being a number between 1 and 2
end sub

Zvoni

: how can i call procedures faster when i have lets say 2 procedures with same name but with a number on the end
: the code i have in the command button is wrong syntax, is there anyway i can change that around or do something different?
:
:
: Private Sub Level1()
: 'code
: end sub
:
: Private Sub Level2()
: 'code
: end sub()
:
: Private sub command1_click()
: call "level" & intLevel 'int level being a number between 1 and 2
: end sub
:

------------------------------------------
Only stupidity of mankind and the universe
are infinite, but i'm not sure concerning
the universe. A. Einstein

Report
Zvoni. Your idea is only passing a value into one SUB. Posted by DrMarten on 13 Jun 2006 at 1:00 AM
Try this way:>>
The CALL keyword is optional.

Dim level As Integer

Select Case level

Case 1
Level1()

Case 2
Level2()

Case 3
Level3()

Case 4
Level4()

Case 5
Level5()

Case 6
Level6()

Case 7
Level7()

Case 8
Level8

Case 9
Level9()

Case 10
Level10()

End Select


Regards,

Dr M.




Here is another example which uses the Select Case statement to evaluate the value of a variable. The second Case clause contains the value of the variable being evaluated, and therefore only the statement associated with it is executed.
Number = 8	' Initialize variable.
Select Case Number	' Evaluate Number.
Case 1 To 5	' Number between 1 and 5.
	Debug.Print "Between 1 and 5"
' The following is the only Case clause that evaluates to True.
Case 6, 7, 8	' Number between 6 and 8.
	Debug.Print "Between 6 and 8"
Case Is > 8 And Number < 11	' Number is 9 or 10.
Debug.Print "Greater than 8"
Case Else	' Other values.
	Debug.Print "Not between 1 and 10"
End Select


Report
Re: Zvoni. Your idea is only passing a value into one SUB. Posted by ShawnHollon on 20 Jun 2006 at 10:59 AM
There is a function in the VBA libraries called "CallByName". You can pass in a String that will call the function you need to call.


: Try this way:>>
: The CALL keyword is optional.
:
:
: Dim level As Integer
: 
: Select Case level
: 
: Case 1
: Level1()
: 
: Case 2
: Level2()
: 
: Case 3
: Level3()
: 
: Case 4
: Level4()
: 
: Case 5
: Level5()
: 
: Case 6
: Level6()
: 
: Case 7
: Level7()
: 
: Case 8
: Level8
: 
: Case 9
: Level9()
: 
: Case 10
: Level10()
: 
: End Select
: 

:
: Regards,
:
: Dr M.
:
:
:
:
: Here is another example which uses the Select Case statement to evaluate the value of a variable. The second Case clause contains the value of the variable being evaluated, and therefore only the statement associated with it is executed.
:
: Number = 8	' Initialize variable.
: Select Case Number	' Evaluate Number.
: Case 1 To 5	' Number between 1 and 5.
: 	Debug.Print "Between 1 and 5"
: ' The following is the only Case clause that evaluates to True.
: Case 6, 7, 8	' Number between 6 and 8.
: 	Debug.Print "Between 6 and 8"
: Case Is > 8 And Number < 11	' Number is 9 or 10.
: Debug.Print "Greater than 8"
: Case Else	' Other values.
: 	Debug.Print "Not between 1 and 10"
: End Select
: 

:
:

Report
Re: Zvoni. Your idea is only passing a value into one SUB. Posted by infidel on 21 Jun 2006 at 7:14 AM
: There is a function in the VBA libraries called "CallByName". You can pass in a String that will call the function you need to call.

CallByName calls object methods, not regular functions.


infidel

$ select * from users where clue > 0
no rows returned


Report
Re: Zvoni. Your idea is only passing a value into one SUB. Posted by ShawnHollon on 21 Jun 2006 at 9:13 AM
True, but if your function is in a module, class, or even form, technically, they are objects and you would just have to reference the name of the form, module, class for which the function is in and you have then supplied the object variable.

: : There is a function in the VBA libraries called "CallByName". You can pass in a String that will call the function you need to call.
:
: CallByName calls object methods, not regular functions.
:
:
: infidel
:
:
: $ select * from users where clue > 0
: no rows returned
: 

:
:

Report
Re: Zvoni. Your idea is only passing a value into one SUB. Posted by ShawnHollon on 21 Jun 2006 at 9:17 AM
here is an example of a basic form using the call by name function within itself


Private Sub Command1_Click()

MsgBox CallByName(Form1, "ToldYouSo", VbMethod)
End Sub



Public Function ToldYouSo() As String
ToldYouSo = "Hi"
End Function




: True, but if your function is in a module, class, or even form, technically, they are objects and you would just have to reference the name of the form, module, class for which the function is in and you have then supplied the object variable.
:
: : : There is a function in the VBA libraries called "CallByName". You can pass in a String that will call the function you need to call.
: :
: : CallByName calls object methods, not regular functions.
: :
: :
: : infidel
: :
: :
: : $ select * from users where clue > 0
: : no rows returned
: : 

: :
: :
:
:

Report
Re: Zvoni. Your idea is only passing a value into one SUB. Posted by infidel on 21 Jun 2006 at 10:47 AM
: : True, but if your function is in a module, class, or even form, technically, they are objects and you would just have to reference the name of the form, module, class for which the function is in and you have then supplied the object variable.

It doesn't work for modules, and it doesn't technically work on classes either, but rather *instances* of classes. It works on the forms because if you reference a form name in VB, then VB automatically loads it.


infidel

$ select * from users where clue > 0
no rows returned





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.