Passing Reference of Control to Module. (1 Viewer)

Emohawk

What a wicked mullet...
Local time
Today, 19:02
Joined
Mar 14, 2002
Messages
79
Is there a generic way to pass the reference of a control to a module(Like you use "Me!" for the current form)?

What I am doing is calling a sub that executes everytime a user enters or exits a field passing a refence of the current control to the module. I.e

private sub my_Control_GotFocus
RunModule("Reference of my_Control object goes here")
end sub

private sub my_Control_LostFocus
RunModule("Reference of my_Control object goes here")
end sub

I am trying to get around placing the name of each control in the call as the module will be used by up to 70 different controls.

Cheers
 

Fizzio

Chief Torturer
Local time
Today, 19:02
Joined
Feb 21, 2002
Messages
1,885
Have you tried using arguments for the sub ie

Sub my_Control_GotFocus (ControlName as String)
RunModule(ControlName)
End Sub

to call the sub, use
my_Control_GotFocus (name of the control)
or you might have to preceed it with the Call statement (I'm not sure)

HTH
 

Emohawk

What a wicked mullet...
Local time
Today, 19:02
Joined
Mar 14, 2002
Messages
79
I'm actually trying to avoid typing the name of the control into the code. I'm looking for a key word that will pass the reference of the object actually making the call. I know Java does this with the "This" object, does VBA have an equivelant?
 

Fizzio

Chief Torturer
Local time
Today, 19:02
Joined
Feb 21, 2002
Messages
1,885
I think you could try

Private Sub YourControl_GotFocus
Dim ctl as Control, ctlname as string
ctl = Screen.ActiveControl
ctlname = ctl.name
RunModule(ctlname)
End Sub

I'm not sure if you will lose the current control name before this code runs though.
 

DALeffler

Registered Perpetrator
Local time
Today, 12:02
Joined
Dec 5, 2000
Messages
263
Perhaps:

Private Sub cmbMyComboBox_GotFocus()
fncMyFunctionName Me.ActiveControl

to call the function and:

Function MyFunctionName(ctl)
Debug.Print ctl.Name
Debug.Print ctl.TabIndex
End Function

for the function code. If you want the function to return a value, then you'll need:

Private Sub cmbMyComboBox_GotFocus()
Dim intReturnValue as Integer
intReturnValue = fncMyFunctionName (Me.ActiveControl)
Debug.Print intReturnValue

to call the function and:

Function MyFunctionName(ctl) as Integer
Debug.Print ctl.Name
Debug.Print ctl.TabIndex
MyFunctionName = ctl.BorderStyle
End Function

Hth,

Doug.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 13:02
Joined
Feb 28, 2001
Messages
27,272
There is a way to do this. It has its upside and its downside.

Public Sub Reset_Color(ctlX As Control)

ctlX.ForeColor = vbBlack
ctlX.BackColor = vbWhite
ctlX.Value = ctlX.Name

End Sub

The catch is, if you select a control that doesn't have one of these properties, you will get an error. For example, if your control was a line, it doesn't have a ForeColor or BackColor property. So you have to include error trapping or have to write something that tests for a property before using it. Or tests the .Type property and acts based on known properties for each type.
 

Users who are viewing this thread

Top Bottom