How to get the active field?

DeepButi

New Member
Joined
Jul 14, 2015
Messages
41
Office Version
  1. 365
Platform
  1. Windows
I have a Form with several Text fields that go to a common code on TextBoxn_Change. How do I get the field that triggers the event so I can call the common routine with it?
 
Once you have the TextBox name, then you can reference it's Properties w/
Me.Controls(tb)
Sorry, but that doesn't work,

Just try the MsgBox on the event and you will see the problem, If it doesn't even work on the event, it will not (it does not) on MyTest()
VBA Code:
Private Sub TextBox1_Change()
    MsgBox "Current ActiveCell is " & ActiveSheet.Name & "!" & ActiveCell.Address & _
        " and, the current TextBox is " & Me.ActiveControl.Name & ".", vbInformation, "Info Show"               ' ERROR Me,ActiveControl is not an object
...

a) ActiveSheet.Name and ActiveSheet.Address refers to the active cell. I don't need this at all. I need the active Form Field
b) Me.ActiveControl doesn't exist.
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
VBA Code:
Private Sub TextBox1_Change()
Dim tObj, tName As String, tVal As String
Set tObj = Me.ActiveControl
tName = tObj.Name
tVal = tObj.Value
Debug.Print tName & " "; tVal
End Sub
 
Upvote 0
VBA Code:
Private Sub TextBox1_Change()
Dim tObj
Set tObj = Me.ActiveControl
Me.DoStuff tObj
End Sub

Private Sub TextBox2_Change()
Set tObj = Me.ActiveControl
Me.DoStuff tObj
End Sub

Sub DoStuff(ByVal tb As Control)
Debug.Print tb.Name & " " & tb
End Sub
 
Upvote 0
VBA Code:
Private Sub TextBox1_Change()
Dim tObj
Set tObj = Me.ActiveControl
Me.DoStuff tObj
End Sub

Private Sub TextBox2_Change()
Set tObj = Me.ActiveControl
Me.DoStuff tObj
End Sub

Sub DoStuff(ByVal tb As Control)
Debug.Print tb.Name & " " & tb
End Sub
OMG ... please read my answers ... Me.ActiveControl doesn't exist

Are you using an AI to answer???????
 
Upvote 0
OMG ... please read my answers ... Me.ActiveControl doesn't exist

Are you using an AI to answer???????
Well, it exists on my machine. What version of Windows are you using?
 
Upvote 0
Is your Form reference checked?
1700942128882.png
 
Upvote 0
Me.ActiveControl doesn't exist
It does if your controls are on a userform. If they they are on a sheet then there is no such thing as a Change event for FormControls.
So what exactly are you using?
 
Upvote 1
Solution
1700942899352.png



1700942931303.png


Curious because according to Microsoft doc it doesn't exist

I will continue to use my initial code

VBA Code:
Private Sub TextBox1_Change()
    MyTest(TextBox1)
End Sub
Private Sub TextBox2_Change()
    MyTest(TextBox2)
End Sub
... and so on

Thanks for all your answers
 
Upvote 0
It does if your controls are on a userform. If they they are on a sheet then there is no such thing as a Change event for FormControls.
So what exactly are you using?

I am using an ActiveX control and yes, ActiveX controls have events

Going to try it on a form!
 
Upvote 0

Forum statistics

Threads
1,215,403
Messages
6,124,714
Members
449,182
Latest member
mrlanc20

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top