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 dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

textbox question - cursor position and input mask 2

Status
Not open for further replies.

4281967

MIS
Oct 14, 2005
74
US
I have a textbox where a user will enter a name (one for first name, another textbox for last name.) My SQL will concatenate them (LName & ", " & FName)

anyway - I thought it would be nice to capitalize the name in case the user didn't. I did this using an input mask of ">L<??????????????"

I also wanted to "validate" the text to be sure the user did not enter a number.

I used this to do that:
Code:
Private Sub txtFirstName_Exit(Cancel As Integer)

    If IsNumeric(txtFirstName.Value) Then
        MsgBox "Please enter a Name only.", vbInformation, "Error"
        txtRoutingNo.Value = ""
        Cancel = True
        Exit Sub
    End If

End Sub

Here is my question:
1. because of the input mask, if the user "clicks" in the textbox, the cursor is located where ever the user clicked. Any text that is entered and it doesn't start at the first position, it errors out.
2. I assume that the input mask is also preventing the "validation" from working properly, as if you do enter a number, and tab to the next control, the textbox is "empty".

Is there any way to force the cursor to be at the first position when a user selects a text box? This would solve issue number one anyway.

Is there a better way to capitalize the first letter?

 
I figured it out...


Code:
Private Sub txtFirstName_Click()

    txtFirstName.SelStart = 0

End Sub

hopefully this will help someone...
 
Hi,

To be honest I would get rid of the input mask myself.

On the exit event of the textbox you could place:
Code:
txtFirstName.Value = StrConv(txtFirstName.Value,vbProperCase)
and that would convert the text to proper case for you.

As for setting the cursor position to the start of the textbox you can use this code:
Code:
Private Sub txtFirstName_GotFocus()
txtFirstName.SelStart = 0
End Sub
That should achieve what you want.

Hope this helps

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Looks like you beat me to that one...[blush]

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
If you are using the input mask shown, you do not need to check for numbers, the mask will not accept them. However, I agree with HarleyQuinn, input masks can be a nuisance. Also, I think I would modify the code for on click a little (if you are keeping the inputmask), because it would be very annoying if every time the user deliberately clicked in the middle of the name, the cursor jumped to the beginning. Maybe:
Code:
Private Sub txtFirstName_Click()
If Trim(Me.txtFirstName) & "" = "" Then
   Me.txtFirstName.SelText = ""
End If
End Sub
 
I've got to agree with Remou, that code is a lot more user-friendly.

Cheers

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Thanks to both of you.

Remou, what does
Code:
If Trim(Me.txtFirstName) & "" = "" Then
this do?
 
It is a test for whether the control is either Null or contains "" - i e an analogue to, and much faster than something like this

[tt]if IsNull(me!txtFirstname) or me!txtFirstname = "" then[/tt]

Even faster, probably

[tt]If Trim$(Me.txtFirstName & "") = "" Then[/tt]

- see the help topic "Return strings from functions" (which I seem to be totally unable to find on my 2003 setup;-))

Roy-Vidar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top