Textbox Input Mask - cursor always defaults to rightmost location (end of mask)

AOB

Registered User.
Local time
Today, 06:04
Joined
Sep 26, 2012
Messages
621
I'm trying to do something that seems very simple but can't quite get it to work as intended.

I have a textbox (unbound) to which I want to apply an input mask. Basically, I want to restrict the user to only enter values of the form "ABC-099999" - in other words, it must be prefixed with the characters "ABC-" and at least 1 and up to 6 numerical characters only.

So basically my input mask looks like this : "ABC-"099999;0;" "

What's annoying me is, when I click into the box, the cursor is automatically placed at the end of the text (i.e. I see "ABC-" followed by 6 spaces and then the cursor) So if I immediately start typing my digits, nothing appears (I have exceeded the "bounds" of the mask) I have to either backspace all the way back to the hyphen, or click the mouse pointer to the same location, before I can actually start to key in the digits. It would make a lot more sense if the cursor was automatically adjacent to the hyphen so I can immediately start keying the digits without manual intervention.

And I'm assuming this is possible because otherwise such input masks would be extremely irritating for users (having to always manually correct the cursor position each time)

What am I doing wrong?
 
Can you post a copy of your db to illustrate the problem.
I can not replicate your problem. On the contrary, I find that it works very well. Cursor goes to the start of the control on a new record. As soon as I press a key, ABC- appears.
.
 
have to say, I don't use input masks - in your case, you only want the number part, so why bother with the ABC? - you can always use the format property (mot function)- as you described it would be "ABC"#

Benefits are:
you store a number rather than a string which is more efficient
when looking something up the user doesn't have to type 'ABC' or use Like in criteria
 
Can you post a copy of your db to illustrate the problem.
I can not replicate your problem. On the contrary, I find that it works very well. Cursor goes to the start of the control on a new record. As soon as I press a key, ABC- appears.
.

I can't post a copy of the DB but there really isn't much more to it than what I've described above? When I click into the textbox, ABC- appears, but also the 6 "placeholder" spaces and the cursor is at the end of them. I have to reposition the cursor or backspace through the spaces to start keying.
 
have to say, I don't use input masks - in your case, you only want the number part, so why bother with the ABC? - you can always use the format property (mot function)- as you described it would be "ABC"#

Benefits are:
you store a number rather than a string which is more efficient
when looking something up the user doesn't have to type 'ABC' or use Like in criteria

Understood but the people I'm building this for need that field to store the full string, i.e. "ABC-123456" not just the numeric part. It just needs to confirm to the pattern.
 
I believe that Chris made some valid points.
If you're just looking for the visual effect you could have a label control next to the text box to display ABC-.
With careful use of the available properties, you can create a frame control to give a single border. Then it looks like its all in the same textbox.
 
That is strange, as I hate masks as when I click into a textbox, the cursor always stays where I happen to enter the textbox. :)
 
Look in the Client Settings section of Access Options

1677687137567.png
 
Not sure that solves the problem unless Access is smart enough to start at the first "number" rather than the "a". The problem is storing the fixed string "ABC-" at all.
 
Yes I agree but the OP is concerned that the cursor placement is at the end of the text.
 
Got too frustrated messing with labels and frames and transparency so ended up just removing the mask altogether (I can see now why CJ doesn't use them, I may follow in those footsteps) and creating a function to clean up the entered value and call it from the After_Update event of the textbox :

Code:
Private Function CleanUpEntry(strInput As String) As String
    Dim strTempValue As String
    Dim i As Integer
    For i = 1 To Len(strInput)
        If IsNumeric(Mid(strInput, i, 1)) Then strTempValue = strTempValue & Mid(strInput, i, 1)
    Next i
    If strTempValue > 0 Then strTempValue = "ABC-" & strTempValue
    CleanUpEntry= strTempValue
End Function

So if the user only keys numbers, the "ABC-" is appended to the front; if they key the prefix themselves, it's retained, if they key it lowercase, it is corrected to uppercase, etc. etc. etc. Works fine for this particular need, far easier on the users than the mask. Over time they will simply just key the numbers knowing that the prefix will be added automatically. (And that's fine)
 
I would echo @CJ_London's comment:
- Use a numeric field for table and form (with validity rule to keep number range).
The number by itself can be efficiently evaluated (incrementing, detecting gaps, etc.) in the sequence.
- For the view, the form text box gets a format property "000000". So the user sees his expected 6 digits.
- The part "ABC-" can be visually prefixed to this field in a label or text field.

So the viewer of the form is happy.

need that field to store the full string
For what?

The total expression can always be composed in a query and the query with this calculated field can be used as a data source for further:
Code:
SELECT "ABC-" & Format(MyNumber, "000000") AS WholeExpression
FROM TableX
Along the way, you move within the framework of normalization, which has its purpose after all ...
You are prepared, if there should be after the next company merger also: "xDEF-".
 
What's annoying me is, when I click into the box, the cursor is automatically placed at the end of the text
you add code to Enter and Click Event of your Unbound Textbox:
Code:
Private Sub UnboundTextboxName_Click()
Call Field1_Enter
End Sub

Private Sub UnboundTextboxName_Enter()
Me.UnboundTextboxName.SelStart = 4
End Sub
 
Last edited:

Users who are viewing this thread

Back
Top Bottom