Thor My
Thor My

Reputation: 299

How to Select All Text in TextBox After textBox.Setfocus Using Access VBA

I need to select all the text in a textbox of an Access form when I click (or double click) into it. i tried the following code, unsuccessfully:

Me.txt_CompraPreco.SelStart = 0
Me.txt_CompraPreco.SelLength = Len(Me.txt_CompraPreco)

thanks in advance.

Upvotes: 2

Views: 26117

Answers (5)

Flavio Battocchio
Flavio Battocchio

Reputation: 75

This works for me:

Dim bSelect As Boolean

Private Sub fieldX_Click()
    If bSelect Then 
        'Select text only at first mouse click then user can click again
        'and is able to put mouse pointer where he prefers
        Me.fieldX.SelStart = 0
        Me.fieldX.SelLength = Len(Me.fieldX)
        bSelect = False
    End If
End Sub

Private Sub fieldX_GotFocus()
    bSelect = True
    'Select text if field got focus via keyboard, Enter or TAB
    'this is not enough if field got focus via mouse click
    Me.fieldX.SelStart = 0
    Me.fieldX.SelLength = Len(Me.fieldX)
End Sub

Upvotes: 0

Lewis Darnton
Lewis Darnton

Reputation: 1

My trial and error found this. If your textfield is formatted as a Standard Number and you have set the decimal places to a certain length, you will run into trouble when you enter a single digit. For example if your decimal places in the field properties is set to 2 and you enter "1", you will display "1.00". To get the entire field (1.00) selected, you must specify the .Text property when you determine the .SelLength (not the default .Value property)

Me.txtYourFieldname_GotFocus
  Me.txtYourFieldName.SelStart = 0
  Me.txtYourFieldName.SelLength = Len(Me.txtYourFieldName.Text)
End Sub

Upvotes: 0

Chien Pham Huy
Chien Pham Huy

Reputation: 51

This code will resolve your problem (use with userform).

txt_CompraPreco.SetFocus
Me.txt_CompraPreco.SelStart = 0
Me.txt_CompraPreco.SelLength = Len(Me.txt_CompraPreco)

Upvotes: 0

zynine
zynine

Reputation: 31

I was looking for a solution regarding this problem, I have the same issue, however, I have a solution to it, I'm not sure if it's efficient, but here's my code:

'Declare a flag
Public flagDblClick As Boolean

'Double click event
Private Sub txtbox_DblClick (Cancel As Integer)
  flagDblClick = True
End Sub

'Mouse up Event
Private Sub txtbox_MouseUp(Button As Integer, Shift As Integer, X as Single, Y as Single)
  If flagDblClick Then
    flagDblClick = False
    txtBox.SelStart = 0
    txtBox.SelLength = Len(txtBox.Value)
  End If
End Sub

Upvotes: 3

Wayne G. Dunn
Wayne G. Dunn

Reputation: 4312

You can use the code shown below. If it doesn't work, place a breakpoint at the first line of code. If it doesn't stop on your breakpoint, then your event is not recognized.

Option Compare Database
Option Explicit

Private Sub txt_CompraPreco_Click()
    If Len(Me.txt_CompraPreco & "") = 0 Then Exit Sub
    Me.txt_CompraPreco.SelStart = 0
    Me.txt_CompraPreco.SelLength = Len(Me.txt_CompraPreco)
End Sub

Upvotes: 7

Related Questions