Passing Form Name to a Function

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a function in a form that I now will be used in multiple forms, so
I want to put the function in my module and call it from different forms.
When I run the function from the module, I get an error statement
(understandably) from this line:
Me.OptionPrintToExcel.Value = 0
How can I pass the form name to the function and replace "Me" with the form
name?

Thanks,
Jonathan Mulder
Red Bluff, CA
 
Hi, Jon.
How can I pass the form name to the function and replace "Me" with the form
name?

In the form's module, call the public function with the parameter string
Me.Name. For example, if the public function is named setValueInForm( ),
which returns a Boolean value, then the following syntax would call the
function and pass the current form's name:

fSuccess = setValueInForm(Me.Name)

.... where fSuccess is a Boolean variable.

In the function, the following code would set the value in the control named
"OptionPrintToExcel":

' * * * * Start Code * * * *

Public Function setValueInForm(sFormName As String) As Boolean

On Error GoTo ErrHandler

Forms(sFormName).OptionPrintToExcel.Value = 0
setValueInForm = True

Exit Function

ErrHandler:

MsgBox "Error in setValueInForm( )." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear

End Function

' * * * * End Code * * * *


HTH.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)
Beware to those who use munged addresses: known newsgroup E-mail harvesters
for spammers are (e-mail address removed) and (e-mail address removed)

- - -
When you see correct answers to your question posted in Microsoft's Online
Community, please sign in to the Community and mark these posts as "Answers,"
so that all may benefit by filtering on "Answered questions" and quickly
finding the right answers to similar questions. Remember that the first and
best answers are often given to those who have a history of rewarding the
contributors who have taken the time to answer questions correctly.
 
When I run the function from the module, I get an error statement
(understandably) from this line:
Me.OptionPrintToExcel.Value = 0
How can I pass the form name to the function and replace "Me" with the
form name?

ActiveForm.OptionPrintToExcel.Value = 0

or

Forms("CreateExcelForm").OptionPr etc etc

or

Public Function SetXLOptionOff (CurrentForm As Form) As Something
'....
CurrentForm.OptionPr...

or

Public Function SetXLOptionoff(FormName as String) As Something
'...
Forms(FormName).OptionPrin.....


etc. Hope that helps


Tim F
 
Thank you very much! I put that code in my program and it works great!

I've never used the "forms collection" syntax. I appreciate your help!
 
Hi, Jon.

You're very welcome! Thanks for marking my reply as an answer. It's very
much appreciated!

Tim Ferguson has some more examples of syntax that could be used in the same
situation. If you haven't already, please take a look at his post and
consider marking it as an answer as well, so that people searching for
solutions to the same question will take a look at both replies for examples
that work.

Thanks.

Gunny
 
Thanks for the help! I didn't think of using the "Activeform" syntax. I
kind of like using the Forms collection syntax, as it hones my skills on
another way of doing it. I appreciate your response!
 
Back
Top