AutoInsert "/" in TextBox after Usert types 4 characters

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

AutoInsert "/" in TextBox after Usert types 4 characters

Post by MSingh »

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

User avatar
HansV
Administrator
Posts: 78481
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: AutoInsert "/" in TextBox after Usert types 4 characters

Post by HansV »

You could use the following as part of the Textbox1_Change event procedure:

Code: Select all

  If Len(Me.TextBox1.Text) = 4 Then
    Me.TextBox1 = Me.TextBox1 & "/"
  End If
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
Best wishes,
Hans

User avatar
Leif
Administrator
Posts: 7209
Joined: 15 Jan 2010, 22:52
Location: Middle of England

Re: AutoInsert "/" in TextBox after Usert types 4 characters

Post by Leif »

HansV wrote:You could use the following as part of the Textbox1_Change event procedure:

Code: Select all

  If Len(Me.TextBox1.Text) = 4 Then
    Me.TextBox1 = Me.TextBox1 & "/"
  End If
It's not perfect, but it'll do for most purposes.
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.

(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?)

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
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.
x.jpg
You do not have the required permissions to view the files attached to this post.
Leif

User avatar
HansV
Administrator
Posts: 78481
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: AutoInsert "/" in TextBox after Usert types 4 characters

Post by HansV »

Yes, correcting would be a problem, that's why I suggested an alternative.

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
It would still be possible to use VBA to insert other characters.
Best wishes,
Hans