PDA

View Full Version : Insert symbol into text box - in the active textbox where the mouse cursor is located



BogdanBogdan
11-07-2018, 01:44 PM
Insert symbol into text box - in the active textbox where the mouse cursor is located:

I have code :

=====================================================

Private Sub CommandButton1_Click()
Dim doClip As DataObject
Dim bulletStr As String
Set doClip = New DataObject
bulletStr = "• "
doClip.SetText bulletStr
doClip.PutInClipboard
With Me.TextBox1
.Paste
.SetFocus
End With
End Sub

=====================================================

An I have 5 textbox . VBA Code from above is work only for "TextBox1"....but if I select TextBox 2 ( or other ) and give click on button....also in my textbox1 inserts the symbol. I do not know how to do this process to see the textbox I've selected with the mouse, and to insert my symbol when I press the insert button.

Can you help me with that ?

Please, see my file in attach.

Thank you in advance,

Bogdan23169

Paul_Hossler
11-07-2018, 03:29 PM
Welcome to the forum

Take a minute to read the FAQs in my signature

Try this




Option Explicit
Dim oTextbox As MSForms.TextBox

Private Sub CommandButton1_Click()
Dim doClip As DataObject
Dim bulletStr As String
Set doClip = New DataObject
bulletStr = "? "
doClip.SetText bulletStr
doClip.PutInClipboard

' With Me.TextBox1 ' <<<<<<<<<<<<<<<<<<<<<< what did you expect
With oTextbox
.Paste
.SetFocus
End With

End Sub




Private Sub TextBox1_Enter()
Set oTextbox = TextBox1
End Sub
Private Sub TextBox2_Enter()
Set oTextbox = TextBox2
End Sub
Private Sub TextBox3_Enter()
Set oTextbox = TextBox3
End Sub
Private Sub TextBox4_Enter()
Set oTextbox = TextBox4
End Sub
Private Sub TextBox5_Enter()
Set oTextbox = TextBox5
End Sub
Private Sub TextBox6_Enter()
Set oTextbox = TextBox6
End Sub

mikerickson
11-09-2018, 06:33 AM
If this is in a userform and if you set the .TakeFocusOnClick property of the CommandButton to False, you could use the userform's ActiveControl property to determine which Textbox has the focus.

BogdanBogdan
11-09-2018, 08:18 AM
Thank you for your sugestion. You are most welcome!

Bogdan