Results 1 to 6 of 6

Thread: please help

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    209

    please help

    how to add dash(-) automatically when input 5 characters in textbox.

    example:
    xxxxx- <----automatic insert dash
    xxxxx-xxxxx-xxxxx-xxxxx-xxxxx <---final output

  2. #2
    Lively Member
    Join Date
    Apr 2014
    Posts
    66

    Re: please help

    Hello,
    I just tried it myself. (I don't know the easy way)

    First Set the maxlength of text1 is 29 then

    Code:
    Private Sub Text1_Change()
    
    If Len(Text1) = 5 Then
    Text1.Text = Text1.Text + "-"
    Text1.SelStart = 6
    End If
    
    If Len(Text1) = 11 Then
    Text1.Text = Text1.Text + "-"
    Text1.SelStart = 12
    End If
    
    If Len(Text1) = 17 Then
    Text1.Text = Text1.Text + "-"
    Text1.SelStart = 18
    End If
    
    If Len(Text1) = 23 Then
    Text1.Text = Text1.Text + "-"
    Text1.SelStart = 24
    End If
    
    If Len(Text1) = 28 Then
    
    'setfocus any object you want
    End If
    
    End sub
    Thank you

    Regards
    KAMAL
    Last edited by kamalkumar2023; May 5th, 2014 at 09:19 AM.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    209

    Re: please help

    Quote Originally Posted by kamalkumar2023 View Post
    Hello,
    I just tried it myself. (I don't know the easy way)

    First Set the maxlength of text1 is 29 then

    Code:
    Private Sub Text1_Change()
    
    If Len(Text1) = 5 Then
    Text1.Text = Text1.Text + "-"
    Text1.SelStart = 6
    End If
    
    If Len(Text1) = 11 Then
    Text1.Text = Text1.Text + "-"
    Text1.SelStart = 12
    End If
    
    If Len(Text1) = 17 Then
    Text1.Text = Text1.Text + "-"
    Text1.SelStart = 18
    End If
    
    If Len(Text1) = 23 Then
    Text1.Text = Text1.Text + "-"
    Text1.SelStart = 24
    End If
    
    If Len(Text1) = 28 Then
    
    'setfocus any object you want
    End If
    
    End sub
    Thank you

    Regards
    KAMAL
    thank you sir, it works!

  4. #4
    Lively Member
    Join Date
    Apr 2014
    Posts
    66

    Re: please help

    No problem....glad to assist. Please 'rate this post' (by the 6 pointed star in one of my posts) and then please mark this thread as RESOLVED (Under Thread Tools menu).

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: please help

    You could also use the Masked Edit box instead which can enforce the entry of legal characters only and insure that the dashes are only entered where they need to go

  6. #6
    Hyperactive Member
    Join Date
    Mar 2014
    Posts
    321

    Re: please help

    add 2 textbox

    text1.text enter numbers
    text2.text is your results

    past all this in form1

    Code:
    Dim userinput As String, result As String, splitchar As String
    Dim splitnum As Integer, splitcharamt As Integer, i As Integer, _
    maxtimes As Integer, o As Integer, maxint As Integer
    
    Private Function SplitByFour()
    For i = 1 To maxint
    o = o + Val(1)
    result = result + Mid(userinput, i, 5)
    result = result + splitchar
    splitcharamt = splitcharamt + Val(1)
    i = i + Val(3)
    Next i
    result = (Mid(result, 1, Len(result) - Val(1)))
    Text2.Text = (result)
    i = 1
    o = 0
    splitcharamt = 0
    End Function
    
    Private Sub Command1_Click()
    splitchar = "-"
    userinput = Text1.Text
    i = 1 'function number
    maxtimes = (Len(result) / i) 'times it can go in
    o = 0 'keeping count of x compard to i
    maxint = Len(userinput)
    splitcharamt = 0
    result = SplitByFour()
    
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width