How to Select All Text in TextBox After textBox.Se

2019-07-20 05:23发布

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.

4条回答
聊天终结者
2楼-- · 2019-07-20 06:00

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
查看更多
Emotional °昔
3楼-- · 2019-07-20 06:05

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)
查看更多
Root(大扎)
4楼-- · 2019-07-20 06:06

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
查看更多
再贱就再见
5楼-- · 2019-07-20 06:27

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
查看更多
登录 后发表回答