Search code examples
vbams-access

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.


Solution

  • 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