February 4th, 2003, 05:07 AM
Example for CallByName
Could you give me a code example for CallByName please ?
I know this appears in the Most FAQ section, but nowhere I could find an example.
February 4th, 2003, 06:20 AM
A few examples
Call the setfocus method of a textbox named text2
Set the Enabled property of the textbox
CallByName Text2, "setfocus", VbMethod
Get the text of the textbox
CallByName Text2, "Enabled", VbLet, False
Hope this helps
MsgBox CallByName(Text2, "Text", VbGet)
February 4th, 2003, 06:30 AM
In the following example, the first line uses CallByName
to set the MousePointer property of a text box, the second line gets the value of the MousePointer property, and the third line invokes the Move method to move the text box:
Text1, "MousePointer", vbLet, vbCrosshair
Result = CallByName
(Text1, "MousePointer", vbGet)
Text1, "Move", vbMethod, 100, 100
Please rate this post if you find it helpful
February 4th, 2003, 09:16 PM
Thank you guys,
Now I understand the purpose of CallByName.
Also, I would like to know if there is a way to call user-defined functions in a similar fashion. If yes, kindly enlighten me on it.
February 4th, 2003, 10:06 PM
Wraps the user function as public function in a Class Module then instantiate the class in a variable then use callbyname
dim c as class1
set c=new class1
callbyname c,"FunctionName",vbmethod, Param1, Param2,....
February 4th, 2003, 11:53 PM
I tried your suggestion.
Following is my class file :-
Public Function mA(a As Integer, b As Integer, ByRef res As Integer)
res = a + b
Public Function mB(a As Integer, b As Integer, ByRef res As Integer)
res = a - b
And my VB program is as follows :-
Private Sub Command1_Click()
Dim c As prjCBN.clsCBN, sum%, dif%
Set c = New prjCBN.clsCBN
CallByName c, "mA", VbMethod, 2, 4, sum
MsgBox "sum : " & sum
CallByName c, "mB", VbMethod, 32, 7, dif
MsgBox "dif : " & dif
Set c = Nothing
It works well as expected. BUT, what if my mA or mB method were to return a value instead of assigning the result to a ByRef variable ? How would I be able to handle the return value using CallByName ?
In the class file I made the function return a value, and then in VB code tried something like -
x= CallByName(c, "mA", VbMethod, 2, 4)
BUT, it displayed a '..does not support..' error
February 5th, 2003, 12:36 AM
First modify the class function by removing the byref param
and set the return value for the function :
Public Function mA(a As Integer, b As Integer)
mA= a + b
Call it in the form
Dim c As Class1, x
Set c = New Class1
x = CallByName(c, "mA", VbMethod, 2, 4)
Set c = Nothing
February 5th, 2003, 01:01 AM
thanks dude, now it works
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!