Passing Reference of Control to Module.

Emohawk

What a wicked mullet...
Local time
Today, 21:22
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
 
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
 
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?
 
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.
 
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.
 
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

Back
Top Bottom