easy one - pass form and field to function

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!
terpsichoreAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

John TsioumprisSoftware & Systems EngineerCommented:
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

terpsichoreAuthor Commented:
Thank you - how do I think reference that form+field to update it with a certain value, in the function?
John TsioumprisSoftware & Systems EngineerCommented:
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
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

terpsichoreAuthor 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 EngineerCommented:
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

terpsichoreAuthor 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 EngineerCommented:
Also to reference to a control from a frm object
frm.controls(thiscontrol.name)

Open in new window

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

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dale FyeOwner, Developing Solutions LLCCommented:
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)
terpsichoreAuthor Commented:
Definitely covered the waterfront - great responses from real experts. I am grateful.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.