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

Relocating cursor in input-masked text field

Status
Not open for further replies.

gymbeef

Technical User
Sep 18, 2002
33
0
0
US
I have an unbound control where the user is required to input a number. To make checking for numbers-only easier, I've set an input mask for that control, however that causes another side-effect.

For visual purposes, the control is left-justified and longer than the allowed number of digits, so if you click on that field, the cursor tends to be at the right-end of the mask. Which means that if you just click and start typing, then nothing happens, as you are already at the end of the mask - you have to backspace into the mask and *then* enter your number. Its very annoying and it doesn't matter if the mask fill is set left-to-right or right-left.

Is there a way to position the cursor in the control at the very beginning? I can put that routine in the Click event of the control, so that typing always begins at the left-side, but I'm not sure how to code for that.

I'm not pleased with the way masking works in Access anyways, so I have an alternate solution I think. Can I use the Change event, such that if a non-numeric character is typed, it causes an automatic backspace to occur to delete it? I think I like the aesthetics of that better, but not sure how to code the backspace to occur either.

Thanks in advance
 
You might try this:

' add this function to your form's code
[tt]
Private Function IsValid(KeyCode As Integer) As Boolean
Select Case KeyCode
Case 8, 13, 27
IsValid = True
Case Else
IsValid = KeyCode >= Asc(&quot;0&quot;) And KeyCode <= Asc(&quot;9&quot;)
End Select
End Function
[/tt]

Add this event procedure (the KeyDown event) to your control:
[tt]
Private Sub MyCtrl_KeyDown(KeyCode As Integer, Shift As Integer)
If Not IsValid(KeyCode) Then KeyCode = 0
End Sub
[/tt]
 
The simplest way get the Cursor to start at the beginning of a Field is to put this in the On Click event of your Control:

Me!ControlName.SelStart = 0

But this could be annoying if a user actually wants to edit the Control, so another not so simple way is to set up a Counter, if the Control is clicked more than once, don't go to the start of the Field. To do this:

Paste into the Declarations Section of your Form:

Dim intClickCounter As Integer

Paste into the On Click event of the Control:

If intClickCounter = 0 Then
Me!ControlName.SelStart = 0
End If
intClickCounter = intClickCounter + 1

Paste into the Lost Focus event of the Control:

intClickCounter = 0

Regards

Bill





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top