Determine form from which a procedure was called

kballing

I do stuff
Local time
Today, 07:08
Joined
Nov 17, 2009
Messages
51
I have a public function, Foobar, in a module that can be called from FormA, FormB, or FormC. Each of these forms contains a field we'll call Output. The output of Foobar is in part determined by where the function was called from. I.E.

Public Function Foobar (arg1, arg2)

if CalledFrom(FormA) then
Forms.FormA.Output = "blahA"
elseif CalledFrom(FormB) then
Forms.FormB.Output = "blahB"
elseif CalledFrom(FormC) then
Forms.FormC.Output = "blahC"
End If

Another approach would be something like

for each thing in CallingForm.Controls
if thing.name = "Output"
thing = "blah"
End if
next thing


Is there a neat way to determine which Form has called Foobar without including this as an argument? I have tried Screen.ActiveForm, but calling the function seems to take the focus off of the original form and I get an error.
 
Is there a neat way to determine which Form has called Foobar without including this as an argument?

probably, but trying soemthing like screen.activeform you might find will give you even more trouble then simply writing :
PHP:
=foobar("formname")
everytime you want the function called. if you do it this way, then the function only needs it's code one time, and for example, syntax like:
PHP:
forms(arg).controls(control)
could be used over and over again successfully. do what you want, but as an opinion, i would pass the argument everytime
 
Or you can include the form in the arguments of the function

Function X(frm As Form, strSomethinElse As String) As Whatever
....
End Function


And then call it by adding the function call to your VBA;

Call x(Me, Me.SomeControl)

Same code could be used on any form.
 
Yeah, I can do that. It is a lot simpler, but not nearly as crafty. If anyone wants to make a mountain out of a molehill, I'm still curious.

In addition to the details above, one of the forms has quite a few subforms with subforms. I was trying find a different approach to puting in:
foobar(me.parent.parent.parent.name, arg, arg)
 
Y
In addition to the details above, one of the forms has quite a few subforms with subforms. I was trying find a different approach to puting in:
foobar(me.parent.parent.parent.name, arg, arg)

if you want my opinion, nested subforms like that are never necessary. moreover, making mountains out of molehills are frowned upon among programmers i'm sure. why more work ?
 
>>If anyone wants to make a mountain out of a molehill, I'm still curious.<<

It appears that the mountain has already been made.
Depending on the circumstances, perhaps a molehill out of a mountain: -

Code:
Public Function Foobar(ByRef vntArg1 As Variant, _
                       ByRef vntArg2 As Variant) As Variant

    [color=green]' Return by Function name.[/color]
    Foobar = vntArg1 + vntArg2
    [color=green]' XOR[/color]
    Foobar = vntArg1 & vntArg2

End Function

[color=green]' XOR[/color]

Public Sub Foobar(ByRef ctlControl As Access.Control, _
                  ByRef vntArg1 As Variant, _
                  ByRef vntArg2 As Variant)

    [color=green]' Return by Reference.[/color]
    ctlControl = vntArg1 + vntArg2
    [color=green]' XOR[/color]
    ctlControl = vntArg1 & vntArg2

End Sub

Please give exact requirements.
 
That's exactly what I was thinking about since the function isn't always caleld from the form that has the focus. Thanks for the link.
 

Users who are viewing this thread

Back
Top Bottom