Referencing a control from a form in a module (1 Viewer)

chrisjames25

Registered User.
Local time
Today, 03:24
Joined
Dec 1, 2014
Messages
401
HI.

I am trying to reference the active control in a form in a module but keep getting an error stating control needs to be in active window.

Code:
Dim ctlCurrentControl As Control
Dim strControlName As String

Set ctlCurrentControl = Screen.ActiveControl
strControlName = ctlCurrentControl.Name
ctlCurrentControl.ForeColor = vbRed

Instead of screen.activecontrol how could i refernece the current active form?
 

Ranman256

Well-known member
Local time
Yesterday, 22:24
Joined
Apr 9, 2015
Messages
4,339
instead of a module cant you put the code into the form? ,and it will KNOW its active.

if you cant put the code in the form, can you send the module code the form ID,:
Code:
'in the form:
Call MyFct(Me)          'send the form id with it

'in the module:
Function MyFct(pfrm as form)
 pfrm.cbobox.backcolor = vbRed
end Function
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 10:24
Joined
May 7, 2009
Messages
19,231
Screen. ActiveForm
 

JHB

Have been here a while
Local time
Today, 04:24
Joined
Jun 17, 2012
Messages
7,732
Another way is to pass the control to the module.

Code:
'Put that in the form
'Call the procedure/sub in the module from the form
Call ActiveControlInForm(Me.ActiveControl)

'Put the following in a module.
Public Sub ActiveControlInForm(ctrl As Control)
   ctrl.ForeColor = vbRed
End Sub
 

Users who are viewing this thread

Top Bottom