473,416 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Calling form's methods using a string

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
8 4949
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: headware | last post by:
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...
5
by: Dave | last post by:
does calling a regular function cost any cpu time? In other words, is it faster to write the code of two functions into main(), or is it the exact same thing as calling two functions. I know its...
3
by: James | last post by:
Has anyone written a utility to convert a C# form to C++.net? i.e. to convert "using System.Data" to "using namespace System::Data" etc
8
by: Brett Robichaud | last post by:
I understand how code-behind can handle events for a page, but can I call a code-behind method from within a <script> tag in my ASP.Net page, or can I only call methods defined in other <script>...
12
by: Ron | last post by:
Greetings, I am trying to understand the rational for Raising Events instead of just calling a sub. Could someone explain the difference between the following 2 scenarios? Why would I want to...
7
by: Jorgen Haukland, Norway | last post by:
Hi, I have created a Java webservice which runs in IBM WebSphere appserver. I take the WSDL-file and create a VS.NET WinForm application and calls the service running on my PC and everything...
4
by: Bugs | last post by:
Hi, I wonder if anyone can help me out. I'm building a vb.net application that has a form with a panel that contains several other sub forms (as a collection of controls). What I'm wanting to...
6
by: Anthony Smith | last post by:
I can call a class using "->", but it complains about the :: I see on the net where :: is used. Is there a good explanation on when to use one over the other or the differences? $help = new...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
6
by: Ole Nielsby | last post by:
VC has a __cdecl specifier which allows functions and methods to be called with varying parameter count. (I understand this is the default for functions in general but in VC, instances use...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.