By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,045 Members | 1,760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,045 IT Pros & Developers. It's quick & easy.

Enter a number from right to left with an input mask

mshmyob
Expert 100+
P: 903
I have an unbound text box on my form, that has an input mask of 999,999 (I want to limit the entry to 6 digits, no decimals, and only numbers).

Once I put the input mask in, the numbers enter from left to right. I want them to enter from right to left like it does when there is no input mask. (Note: I am NOT talking about right align)

Can it be done and if so how?
Jan 10 '08 #1
Share this Question
Share on Google+
6 Replies


missinglinq
Expert 2.5K+
P: 3,532
Your object can be accomplished but not, I think, using an Input Mask. Instead you'll need to use the KeyDown event of the textbox to limit input to digits plus keys needed to move back and forth within the textbox, and the Change event to keep track of how many digits have been entered. When the limit of 6 digits have been entered, focus will automatically move to another control, called AnyOtherControl in the example.

You'll need to substitute your actual names for YourTextBox and AnyOtherControl.

For your textbox, set the Format Property to General Number (this will give you the right-to-left fill)
Expand|Select|Wrap|Line Numbers
  1. Private Sub YourTextBox_Change()
  2.   If Len(Me.YourTextBox.Text) = 6 Then
  3.     Me.AnyOtherControl.SetFocus
  4. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub YourTextBox_KeyDown(KeyCode As Integer, Shift As Integer)
  2.   Select Case KeyCode
  3.     Case 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, vbKeyDelete, vbKeyBack, vbKeyReturn, vbKeyRight, vbKeyLeft, vbKeySpace
  4.       'Does nothing except allow appropriate keys to be used
  5.     Case Else
  6.       KeyCode = 0  'Inappropriate keys do nothing
  7.   End Select
  8. End Sub
Welcome to TheScripts!

Linq ;0)>
Jan 10 '08 #2

mshmyob
Expert 100+
P: 903
Thanks Linq. It worked great. I made a slight change to just stop numbers from going in when it reaches 6 digits but still allow the user to use the vbKeys you have listed (excpetion the space - I removed it).

The user must click a button when they are finished or hit the Enter key.

I am making a game for my daughter. I already have the hangman type game (for learning spelling) finished and am now working on a math type game.

Thanks for your help. You will probably see more questions from me.


Your object can be accomplished but not, I think, using an Input Mask. Instead you'll need to to use the KeyDown event of the textbox to limit input to digits plus keys needed to move back and forth within the textbox, and the Change event to keep track of how many digits have been entered. When the limit of 6 digits have been entered, focus will automatically move to another control, called AnyOtherControl in the example.

You'll need to substitute your actual names for YourTextBox and AnyOtherControl.

For your textbox, set the Format Property to General Number (this will give you the right-to-left fill)
Expand|Select|Wrap|Line Numbers
  1. Private Sub YourTextBox_Change()
  2.   If Len(Me.YourTextBox.Text) = 6 Then
  3.     Me.AnyOtherControl.SetFocus
  4. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub YourTextBox_KeyDown(KeyCode As Integer, Shift As Integer)
  2.  Select Case KeyCode
  3.  Case 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, vbKeyDelete, vbKeyBack, vbKeyReturn, vbKeyRight, vbKeyLeft, vbKeySpace
  4. 'Does nothing except allow appropriate keys to be used
  5.  Case Else
  6.     KeyCode = 0  'Inappropriate keys do nothing
  7. End Select
  8. End Sub
Welcome to TheScripts!

Linq ;0)>
Jan 10 '08 #3

missinglinq
Expert 2.5K+
P: 3,532
Two generation learning project! Very cool!

Glad we could help! I always put the vbKeySpace in because some users hilite characters and hit space to delete them, but as usual, it's Dealer's Call! If it's left in the code and they should inadvertently enter a space, like

123 456

Access would pop an error message because the textbox is formatted as a General Number and 123 456 isn't a number!

As for future questions, that's why we're here!

Linq ;0)>
Jan 10 '08 #4

mshmyob
Expert 100+
P: 903
As a follow up do you know the number codes for the number pad. The number pad doesn't get picked up with the key codes you gave.

Two generation learning project! Very cool!

Glad we could help! I always put the vbKeySpace in because some users hilite characters and hit space to delete them, but as usual, it's Dealer's Call! If it's left in the code and they should inadvertently enter a space, like

123 456

Access would pop an error message because the textbox is formatted as a General Number and 123 456 isn't a number!

As for future questions, that's why we're here!

Linq ;0)>
Jan 14 '08 #5

missinglinq
Expert 2.5K+
P: 3,532
Sorry! I never use the numbers pad!

vbKeyNumpad0
vbKeyNumpad1
vbKeyNumpad2
vbKeyNumpad3
vbKeyNumpad4
vbKeyNumpad5
vbKeyNumpad6
vbKeyNumpad7
vbKeyNumpad8
vbKeyNumpad9

For the complete list of all keys and their ASCII values go into Access Help and enter Keycode Constants.

Linq ;0)>
Jan 14 '08 #6

mshmyob
Expert 100+
P: 903
Thanks. I don't either but for the game it is easier and my 7 year old was trying to use the num pad so it got me thinking.

Sorry! I never use the numbers pad!

vbKeyNumpad0
vbKeyNumpad1
vbKeyNumpad2
vbKeyNumpad3
vbKeyNumpad4
vbKeyNumpad5
vbKeyNumpad6
vbKeyNumpad7
vbKeyNumpad8
vbKeyNumpad9

For the complete list of all keys and their ASCII values go into Access Help and enter Keycode Constants.

Linq ;0)>
Jan 15 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.