By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,079 Members | 940 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,079 IT Pros & Developers. It's quick & easy.

Calling form's methods using a string

P: n/a
I know that you can call the method of one from from inside another
form by doing something like this

Forms("MyForm").MyFunction(12, 34)

However, you have to know that MyForm has a function called
MyFunction. Can you specify a string for the function name like you
can with the form name? That is can I do something like

Forms("MyForm")."MyFunction"

and still supply some arguments to the funciton somehow?

Thanks,
Dave
Nov 12 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 10 Apr 2004 14:38:46 -0700, he******@aol.com (headware) wrote:

Check out Eval in the help file.
-Tom.
I know that you can call the method of one from from inside another
form by doing something like this

Forms("MyForm").MyFunction(12, 34)

However, you have to know that MyForm has a function called
MyFunction. Can you specify a string for the function name like you
can with the form name? That is can I do something like

Forms("MyForm")."MyFunction"

and still supply some arguments to the funciton somehow?

Thanks,
Dave


Nov 12 '05 #2

P: n/a
Tom,

I looked into the Eval function but either I don't understand enough
about how to use it or it's not going to do what I need it to. I'm
attempting to create a function call like

Forms("MyForm").MyFunction #4/12/2004#

which will call a sub-routine in another form and pass it a date
value. The problem is that the Eval function keeps reporting a syntax
error. I try to pass in the form name and function name into the
calling form through the OpenArgs parameter, parse them out, and call
the function from that form.

The calling form contains the following code:

DoCmd.OpenForm "frmCaller", OpenArgs:=Me.Name & ";MyCallback"

The form that invokes the callback function (frmCaller) does the
following with the data in the OpenArgs variable

Dim args As String
Dim sepIdx As Integer
Dim formName, callback As String

args = Me.OpenArgs 'contains the form name and function name to call
sepIdx = InStr(args, ";")
formName = Mid(args, 1, sepIdx - 1) 'parse out form name
callback = Mid(args, sepIdx + 1, Len(args) - sepIdx) 'parse out
callback

Eval "Forms(""" & formName & """)." & callback & " #1/1/2004#"

I get a message box saying "The expression you entered contains
invalid syntax". I was looking at the documentation on the Eval
function and it seemed to indicate that the routine called should
return a value. So I made the callback routine return a meaningless
value. Executing that code just caused Access to crash. The callback
function sets the value of a certain control in another form. Could
that be the problem? I'm stumped.

Dave

Tom van Stiphout <to*****@no.spam.cox.net> wrote in message news:<lc********************************@4ax.com>. ..
On 10 Apr 2004 14:38:46 -0700, he******@aol.com (headware) wrote:

Check out Eval in the help file.
-Tom.
I know that you can call the method of one from from inside another
form by doing something like this

Forms("MyForm").MyFunction(12, 34)

However, you have to know that MyForm has a function called
MyFunction. Can you specify a string for the function name like you
can with the form name? That is can I do something like

Forms("MyForm")."MyFunction"

and still supply some arguments to the funciton somehow?

Thanks,
Dave

Nov 12 '05 #3

P: n/a
On Apr 10 2004, 05:38 pm, he******@aol.com (headware) wrote in
news:e3**************************@posting.google.c om:
I know that you can call the method of one from from inside another
form by doing something like this

Forms("MyForm").MyFunction(12, 34)

However, you have to know that MyForm has a function called
MyFunction. Can you specify a string for the function name like you
can with the form name? That is can I do something like

Forms("MyForm")."MyFunction"

and still supply some arguments to the funciton somehow?


If you are using Access 2002 or 2003, you can use CallByName function (not
always - doesn't work when called from commandbars). Otherwise, Eval might
work, but beware of a long standing bug that causes form code called in
this way to execute multiple times. I'd say the whole thing is not worth
the effort in earlier versions of Access.

--
remove a 9 to reply by email
Nov 12 '05 #4

P: n/a
On 12 Apr 2004 18:05:49 -0700, he******@aol.com (headware) wrote:

Try this:
In the Northwind sample application, add this to the Employees form:
Public Function MyFunction(ByVal d As Date)
MsgBox "You passed in: " & d
MyFunction = 0
End Function

Add a button to the Orders form, with this in the click event:
Private Sub cmdTest1_Click()
Debug.Print Eval("Forms!Employees.MyFunction(#4/12/2004#)")
End Sub

As Dimitri points out, your function may be called twice. Semaphores
can be used to guard against that.

-Tom.

<clip>

Nov 12 '05 #5

P: n/a
he******@aol.com (headware) wrote in news:e3f4b0ae.0404121705.76caaf47
@posting.google.com:
I looked into the Eval function but either I don't understand enough
about how to use it or it's not going to do what I need it to. I'm
attempting to create a function call like

Forms("MyForm").MyFunction #4/12/2004#


The syntax you must use is similar to this:

result = Form_MyForm.MyFunction(#4/12/2004#)

MyForm must be open (although it can be hidden) at the time of the call, or
an error will occur.

Unfortunately, from what I can tell, eval will not work with this. Eval
seems to use an execution environment that does not have access to the
Form_MyForm class.
Nov 12 '05 #6

P: n/a
Thanks for the reply but I don't think CallByName will work in this
case. CallByName apparently expects you to supply the name of the
object in which the function resides and it won't take a string that
contains the name. For example if I want to call a function named
"MyFunction" on a form called "MyForm" I would have to use

CallByName Form_MyForm, "MyFunction", vbMethod

I can't hardcode the form name. It needs to work with variables such
as

Private Sub Test(frmName as String, functName as String)

CallByName "Form_" & frmName, functName, vbMethod

End Sub

Dimitri Furman <df*****@cloud99.net> wrote in message news:<Xn****************************@127.0.0.1>...
On Apr 10 2004, 05:38 pm, he******@aol.com (headware) wrote in
news:e3**************************@posting.google.c om:
I know that you can call the method of one from from inside another
form by doing something like this

Forms("MyForm").MyFunction(12, 34)

However, you have to know that MyForm has a function called
MyFunction. Can you specify a string for the function name like you
can with the form name? That is can I do something like

Forms("MyForm")."MyFunction"

and still supply some arguments to the funciton somehow?


If you are using Access 2002 or 2003, you can use CallByName function (not
always - doesn't work when called from commandbars). Otherwise, Eval might
work, but beware of a long standing bug that causes form code called in
this way to execute multiple times. I'd say the whole thing is not worth
the effort in earlier versions of Access.

Nov 12 '05 #7

P: n/a
On Apr 18 2004, 05:40 pm, he******@aol.com (headware) wrote in
news:e3**************************@posting.google.c om:
Thanks for the reply but I don't think CallByName will work in this
case. CallByName apparently expects you to supply the name of the
object in which the function resides and it won't take a string that
contains the name. For example if I want to call a function named
"MyFunction" on a form called "MyForm" I would have to use

CallByName Form_MyForm, "MyFunction", vbMethod

I can't hardcode the form name.


You don't have to. Something like this should do it:

CallByName Forms("MyForm"), "MyFunction", vbMethod

--
remove a 9 to reply by email
Nov 12 '05 #8

P: n/a
You're absolutely right. I don't know what I was doing wrong before
but this does in fact work. Thanks!

Dave

Dimitri Furman <df*****@cloud99.net> wrote in message news:<Xn****************************@127.0.0.1>...
On Apr 18 2004, 05:40 pm, he******@aol.com (headware) wrote in
news:e3**************************@posting.google.c om:
Thanks for the reply but I don't think CallByName will work in this
case. CallByName apparently expects you to supply the name of the
object in which the function resides and it won't take a string that
contains the name. For example if I want to call a function named
"MyFunction" on a form called "MyForm" I would have to use

CallByName Form_MyForm, "MyFunction", vbMethod

I can't hardcode the form name.


You don't have to. Something like this should do it:

CallByName Forms("MyForm"), "MyFunction", vbMethod

Nov 12 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.