Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select all text in textbox via GotFocus event 1

Status
Not open for further replies.

mych

Programmer
May 20, 2004
248
GB
Hi

I'm trying to slim down my code and save my fingers....

I have various forms with multiple text boxes. I want some code on the get focus event that will select whatever text happens to be in the box.

In the past I have used...
Code:
Private Sub [i]txtBoxName[/i]_GotFocus()

    [i]txtBoxName[/i].SelStart = 0
    [i]txtBoxName[/i].SelLength = Len([i]txtBoxName[/i].Text)

End Sub

But with the number of boxes I have, I need to make a call to a simple module... I have this so far....

In the get focus event of each text box I have...
Code:
Private Sub txtBoxName_GotFocus()
    
    SelectAllTxt (Me)

End Sub
In a module I have....
Code:
Public Sub SelectAllTxt(txtBox As Control)
    
    txtBox.SelStart = 0
    txtBox.SelLength = Len(txtBox.Text)
    
End Sub

This gives me a type 13 Mismatch error. Where have I gone wrong???

Any Help appreciated
 
Given that the use of Me is probably an attempt to generalise the code so that a different call (different parameter each time) does not have to be used in each GotFocus event, it would probably be better to use:

SelectAllTxt(ActiveControl)
 
Tried that and still get Type Mismatch.

I changed SelectAllTxt (Me) to SelectAllTxt (txtBoxName)

This gave Type Mismatch

I then also changed

Public Sub SelectAllTxt(txtBox As Control) to

Public Sub SelectAllTxt(txtBox As TextBox)

and still got Type Mismatch error.

:(
 
strongm, is the active control set before the got_focus event is fired?
 
strongm, sorry i doubted you. I just checked it out and the active control is set before the got focus event.

Your solution is cleaner than mine.
 
Firstly
>Type Mismatch

An 'oops' from me:

SelectAllTxt(ActiveControl)

should have been either

Call SelectAllTxt(ActiveControl)

or

SelectAllTxt ActiveControl

(this will eliminate the type mismatch problem, and a key word search on the use of brackets or not in making function/procedure calls should reveal why)

Secondly
>strongm, is the active control set before the got_focus event is fired?

Why don't you try it and see?
 
StrongM...

Tried your solution and get Object required error.

Hovering over ActiveControl I get "the actual text in the text box" instead of the name of the control...

I tried SelectTxtAll(ActiveControl.Name)

That didn't work either
 
Call SelectTxtAll(ActiveControl) did it....

Many Thanks... Star for you StrongM
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top