easy one - pass form and field to function

terpsichore
terpsichore used Ask the Experts™
on
Dear experts -
I want to pass the current form name and the name of a text box (field) on that form to a function.
I then want the function to do certain things and in certain cases update that field.

How do I properly pass the form and field name, and then how do I properly reference them in the function?
Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
John TsioumprisSoftware & Systems Engineer

Commented:
Public Function YourFunction (frm as form,TextBoxValue as string)

Open in new window


You call it like this
YourFunction Me, Me.textbox.name

Open in new window

Author

Commented:
Thank you - how do I think reference that form+field to update it with a certain value, in the function?
John TsioumprisSoftware & Systems Engineer

Commented:
I am not 100% percent sure what you need,,,
I assume that you want to perform some operations in your function that will affect your form....
if this is correct then you need to modify the function to  like this
Public Function YourFunction (ByRef frm as form,TextBoxValue as string)

Open in new window

but better to give some more info about your function
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
I'm trying to do something like this in the function, but it is giving me an error:

Function ShowLink(thislink As String, thisform As Form, thiscontrol As String)

Dim sFilename as string
sFileName = Mid(thislink, iBackslash + 1, (Len(thislink) - iBackslash))
thisform.thiscontrol = sFileName

On the last line above, I'm getting error 2465, Application-defined or object-defined error...
John TsioumprisSoftware & Systems Engineer

Commented:
Function ShowLink(thisLink as string) as string
ShowLink=  = Mid(thisLink , iBackslash + 1, (Len(thislink) - iBackslash))
End function

Open in new window

Then on the control you want to use it
me.thiscontrol = showlink(thisLink )

Open in new window

Author

Commented:
Also - when I'm passing the formname ('me'), I guess it is not fully referenced. Maybe this is part of the problem?
John TsioumprisSoftware & Systems Engineer

Commented:
Also to reference to a control from a frm object
frm.controls(thiscontrol.name)

Open in new window

Author

Commented:
sorry, I'm still not getting this. can you pls be more specific on how I should correct the error?
Thanks!
Software & Systems Engineer
Commented:
thisform.controls("thiscontrol") = sFileName

Open in new window

Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010
Commented:
There are a number of ways to do this.

1.  You could pass the form and control names to your function/subroutine and then create objects within the procedure, something like:
Public function fnSomething(FormName as string, CtrlName as string)

    Dim frm as Form
    Dim ctrl as Control

    set frm = forms(FormName)
    set ctrl = frm.controls(CtrlName)

Open in new window

2.  Or you could simply pass the form and control objects directly to your function:
Public Function fnSomething(frm as Form, ctrl as Control)

Open in new window

In this case, to call the function you would simply use something like:

Call fnSomething(me, me.txt_TextboxName)

Author

Commented:
Definitely covered the waterfront - great responses from real experts. I am grateful.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial