Hi,
In Textbox1 the user is required to type-in a new account code eg. "4150/050"
How can the "/" be automatically inserted after user types "4150 "
TextBox1 only accepts numbers.
Presently, Private Sub TextBox1_Change() calls a macro to check if the account code already exists.
Thanks again
Mohamed
AutoInsert "/" in TextBox after Usert types 4 characters
-
- 3StarLounger
- Posts: 366
- Joined: 12 May 2010, 06:49
-
- Administrator
- Posts: 78558
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: AutoInsert "/" in TextBox after Usert types 4 characters
You could use the following as part of the Textbox1_Change event procedure:
It's not perfect, but it'll do for most purposes.
Another option would be to use two text boxes, one for the part before the "/" and one for the part after the "/" and to assemble the account code in your VBA.
Dim strAccountCode
strAccountCode = Me.TextBox1 & "/" & Me.TextBox2
Code: Select all
If Len(Me.TextBox1.Text) = 4 Then
Me.TextBox1 = Me.TextBox1 & "/"
End If
Another option would be to use two text boxes, one for the part before the "/" and one for the part after the "/" and to assemble the account code in your VBA.
Dim strAccountCode
strAccountCode = Me.TextBox1 & "/" & Me.TextBox2
Best wishes,
Hans
Hans
-
- Administrator
- Posts: 7213
- Joined: 15 Jan 2010, 22:52
- Location: Middle of England
Re: AutoInsert "/" in TextBox after Usert types 4 characters
The problem I have found with this is that is frustrating trying to correct an error in, say, the fourth character, or when four characters have been entered - as as soon as you delete a number, another "/" is inserted.HansV wrote:You could use the following as part of the Textbox1_Change event procedure:
It's not perfect, but it'll do for most purposes.Code: Select all
If Len(Me.TextBox1.Text) = 4 Then Me.TextBox1 = Me.TextBox1 & "/" End If
(Would you not get an error if you tried to insert an non-numeric character if the text box is set to accept numbers only?)
I would recommend Hans' second suggestion of using two text boxes, and using code to automatically move the cursor focus to the second text box as soon as TextBox1 has four digits.HansV wrote: Another option would be to use two text boxes, one for the part before the "/" and one for the part after the "/" and to assemble the account code in your VBA.
Dim strAccountCode
strAccountCode = Me.TextBox1 & "/" & Me.TextBox2
You do not have the required permissions to view the files attached to this post.
Leif
-
- Administrator
- Posts: 78558
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: AutoInsert "/" in TextBox after Usert types 4 characters
Yes, correcting would be a problem, that's why I suggested an alternative.
One could prevent non-numeric characters from being entered as follows:
It would still be possible to use VBA to insert other characters.
One could prevent non-numeric characters from being entered as follows:
Code: Select all
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < vbKey0 Or KeyAscii > vbKey9 Then
KeyAscii = 0
End If
End Sub
Best wishes,
Hans
Hans