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

AOB

Registered User.
Local time
Today, 13:54
Joined
Sep 26, 2012
Messages
615
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?
 

bob fitz

AWF VIP
Local time
Today, 13:54
Joined
May 23, 2011
Messages
4,727
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.
.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 13:54
Joined
Feb 19, 2013
Messages
16,614
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
 

AOB

Registered User.
Local time
Today, 13:54
Joined
Sep 26, 2012
Messages
615
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.
 

AOB

Registered User.
Local time
Today, 13:54
Joined
Sep 26, 2012
Messages
615
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.
 

bob fitz

AWF VIP
Local time
Today, 13:54
Joined
May 23, 2011
Messages
4,727
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.
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:54
Joined
Sep 21, 2011
Messages
14,306
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. :)
 

isladogs

MVP / VIP
Local time
Today, 13:54
Joined
Jan 14, 2017
Messages
18,225
Look in the Client Settings section of Access Options

1677687137567.png
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:54
Joined
Feb 19, 2002
Messages
43,275
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.
 

isladogs

MVP / VIP
Local time
Today, 13:54
Joined
Jan 14, 2017
Messages
18,225
Yes I agree but the OP is concerned that the cursor placement is at the end of the text.
 

AOB

Registered User.
Local time
Today, 13:54
Joined
Sep 26, 2012
Messages
615
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)
 

ebs17

Well-known member
Local time
Today, 14:54
Joined
Feb 7, 2020
Messages
1,946
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-".
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:54
Joined
May 7, 2009
Messages
19,245
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

Top Bottom