How to pass an argument to the active control (vba)

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
I want a way that I could pass an argument to the active or current control that I have selected.

For example, when I double click on a textbook, there is this code that I would run.

Instead of placing the code under each textbox, I want a way where I would create a separate Sub for the code then call it each time I double click a textbox.

The issue now is how to be able to know which textbox to point to from the sub.

All the codes that will run before pointing to the textbox does not depend on the various textboxes.

But they are a bit long - the reason why I want to avoid writing them one by one.

So say I double click textbox1 then after running all my verification code, what I do is unlock the textbox1 because it has been locked before I do the double click.

Is there a way around it?
 
What did it do and how did you trigger it. If there was a Command Button clicked, what is that button's .TakeFocusOnClick property?

Also, the Container argument is optional. Omit it if you want the reallyactivecontrol of the whole userform.
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I triggered the code with the double click event of the textbox. I am using that message box to test it.

Code:
.......
ReallyActiveControl
.........

Code:
Public Function ReallyActiveControl(Optional Container As Object) As MSForms.Control
    If Container Is Nothing Then
        Set Container = Me
    End If
   
    On Error Resume Next
    Set ReallyActiveControl = Container.ActiveControl
    Set ReallyActiveControl = Container
    On Error GoTo 0
   
    On Error Resume Next
    Do
        If TypeName(ReallyActiveControl) = "MultiPage" Then
            With ReallyActiveControl
                Set ReallyActiveControl = .Pages(.Value)
            End With
        End If
        Set ReallyActiveControl = ReallyActiveControl.ActiveControl
    Loop Until Err
   
  MsgBox ReallyActiveControl.Name 
    
    On Error Goto 0
End Function
 
Upvote 0
Is this textBox in a userform?
What is it returning?


It's on the multi page form.

This is the level.
Userform - multi page form - frame - textbox

Currently when I double click the textbox, the message I get is the name of the multi page form

"MultiPage2"
 
Upvote 0
I can't replicate that problem. Have you tried setting a break point and stepping through the function?
The MultiPage should be caught by the If Typename line.
 
Upvote 0
@mikerickson
I have now placed the textboxes directly on the userform - no frames or whatever.
I would be glad if you can get the script for it for me.

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,839
Messages
6,121,892
Members
449,058
Latest member
Guy Boot

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