Can I Enter Sub Name In On Click Property? (1 Viewer)

Soule

Registered User.
Local time
Today, 01:57
Joined
Jan 6, 2012
Messages
28
Hi, Everyone,

I want to click a form button and run code in a STANDARD module (not the "behind the form" class module).

Can I enter the name of that Public Sub in the On Click Property box?

Pointing a button to code doesn't always have to be done with the "Event Procedure" option filled in the On Click Property box, correct?

I have a Public Sub in a standard module that my form button doesn't seem to see, even though the name of the button and the Sub are different.

Any bit of information to clear this up for me will be appreciated. Thanks.

Frank
 

NickHa

CITP
Local time
Today, 09:57
Joined
Jan 29, 2012
Messages
203
You can use a Public Function name in the button click event, but not a Sub. You must include the closing parentheses, even if there are no parameters to the function.
 

Soule

Registered User.
Local time
Today, 01:57
Joined
Jan 6, 2012
Messages
28
Thanks for replying, Nick.

Does this mean actually changing my Sub procedure to a Function procedure?
 

MSAccessRookie

AWF VIP
Local time
Today, 04:57
Joined
May 2, 2008
Messages
3,428
Thanks for replying, Nick.

Does this mean actually changing my Sub procedure to a Function procedure?

Why not just use the Class Module and in the On Click Event call the STANDARD Sub? In that way, you could still keep it as a SUB.
 

ChrisO

Registered User.
Local time
Today, 18:57
Joined
Apr 30, 2003
Messages
3,202
' In a standard module:-
Code:
Public Function MyFunctionNameHere()

    MsgBox "Here in MyFunctionNameHere"

End Function


'Called from Property as:-
=MyFunctionNameHere()

Chris.
 

NickHa

CITP
Local time
Today, 09:57
Joined
Jan 29, 2012
Messages
203
Yes, that's right. If you have a Public Sub named DoSomeThing, then simply change the word 'Sub' to 'Function'. You don't need a return value from the function (unless your logic has need of it).
In the control's Click event, you would enter
Code:
=DoSomeThing()
If you want to generalise the code to handle different controls, you can refer to the form's ActiveControl to retrieve its properties (e.g. name, value). An example of this would be to show a user prompt when a control received the focus, so you might have
Code:
Private Function showLabel(ByVal pShow As Boolean)
Rem the calling control has an associated label with the same name, apart from prefix 'lbl' in palce of the control's 3-character prefix
Rem e.g. ctlDescription has associated label lblDescription
Me.Controls("lbl" & Right(Me.ActiveControl.Name, Len(Me.ActiveControl.Name) - 2)).FontBold = pShow
Me.Controls("lbl" & Right(Me.ActiveControl.Name, Len(Me.ActiveControl.Name) - 2)).Visible = pShow
End Function
then in every control's OnGotFocus event you put
Code:
=showLabel(True)
and in the OnLostFocus event you put
Code:
=showLabel(False)
 

Soule

Registered User.
Local time
Today, 01:57
Joined
Jan 6, 2012
Messages
28
SOLVED....

Hey, guys....thanks for the attention to my post...

I decided to go with what MSAccessRookie suggested and call Subs behind my form class module. I've never worked with a Function procedure before, so, given the time constraints of this project, I went with code On Click events.

Thanks, though.
 

Users who are viewing this thread

Top Bottom