Select position

I

Ig

Hi
I have field with mask \(000") "000\-0000;0;" " - telephone number.
I want when user first time clicks this field it will get default area code
and cursor would stay on the first next empty place:
(205)___-____
^
The following code positions cursor well:
Private Sub txtHomePhone_Click()
If Nz(Me!txtHomePhone, "") = "" Then
Me!txtHomePhone.SelStart = 4
Me!txtHomePhone.SelLength = 1
End If
End Sub

But when I add area code, then it selects all field and not stays as it
needed.
Private Sub txtHomePhone_Click()
If Nz(Me!txtHomePhone, "") = "" Then
Me!txtHomePhone = "205"
Me!txtHomePhone.SelStart = 4
Me!txtHomePhone.SelLength = 1
End If
End Sub

Thanks
 
W

Wayne Morgan

I just tried what you have and it appears to work. The only anomaly that I
noticed was that position 4 gave me the closing parenthesis after the area
code, but the cursor moved to the next number spot as soon as I pressed a
number key.

Another option would be to use 9s for the area code in the input mask and
place a ! character at the front of the mask. Next, use your first code
example and have the user type in the last 7 digits. In the AfterUpdate
event of the textbox, check the length of the value and append the 205 to
the front if the user only entered the last 7 digits. Be aware that with the
0 after the first ; in the mask, you are storing the formatting characters
in the field, not just the numbers, so you'll have to allow for them when
checking the length of the data. Also, rather than simply concatenating 205
to the front of the string, you may need to use Replace.

Replace(Me.txtHomePhone, "( )", "(205)")
or
Replace(Me.txtHomePhone, "()", "(205)")

If you do adjust the Input Mask, remember to adjust it everywhere it exists
(in controls and the table).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top