Results 1 to 23 of 23

Thread: This Should Be Easy (Intrinsic Format)

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2001
    Location
    Hawaii
    Posts
    43

    This Should Be Easy (Intrinsic Format)

    Does anyone know VB's function for formatting 10 digit phone numbers as follows:

    (808) 531-0000

    That's if there is one, of course. I have my date format figured out: FormatDate(Now, vbLongDate)

    But for the 10-digit phone number it would be REALLY nice if as soon as you typed a number in the beginning parentheses would pop out and close after the third digit is entered. If THAT can't be done intrinsically then I need the easiest way to get a user to type a 10-digit code.

    Cynde

  2. #2
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Catalonia
    Posts
    397
    Try this code. There is a slight difference about what you are asking for, bracket is closed when you type the fourth digit not the third.
    [code]
    Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case Len(Text1.Text)
    Case 0
    Text1.Text = "("
    Text1.SelStart = 1
    Case 4
    Text1.Text = Text1.Text & ")"
    Text1.SelStart = Len(Text1.Text)
    Case 8
    Text1.Text = Text1.Text & "-"
    Text1.SelStart = Len(Text1.Text)
    End Select
    End Sub
    [code]


    Josep Mª

  3. #3
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    I dont know if his code works.. but here it is legable.

    Code:
    Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) 
        Select Case Len(Text1.Text) 
        Case 0 
            Text1.Text = "(" 
            Text1.SelStart = 1 
        Case 4 
            Text1.Text = Text1.Text & ")" 
            Text1.SelStart = Len(Text1.Text) 
        Case 8 
            Text1.Text = Text1.Text & "-" 
            Text1.SelStart = Len(Text1.Text) 
        End Select 
    End Sub

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2001
    Location
    Hawaii
    Posts
    43
    AHA! I knew there wasn't an intrinsic code for 10-digit phone numbers like there is in Access. Okay, I'll try both codes. Thanks!! I'll report back and let you know how it worked.
    Cynde

  5. #5
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    =) It should work. If it doesnt I will come up with something....

  6. #6
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Catalonia
    Posts
    397
    Evan

    How can I inser code in a readable way as you posted?

    I have tried writing [code] before amd after it as I have read in the forum help but it does not work.


    Josep Mª

  7. #7
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    =)

    You used the wrong works...

    before the code its [ code ]
    After the code its [ /code ]

    Without the spaces though.

  8. #8
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    You ought to fidaddle with the format function.

    Heres an Example Usage:

    VB Code:
    1. Private Sub Command1_Click()
    2. '(808) 531-0000
    3. Dim MyNum As String
    4. Dim MyTel As String
    5.     MyNum = "8085310000"
    6.     MyTel = Format(MyNum, "(000) 000-0000")
    7. MsgBox "Before: " & MyNum & Chr$(13) & "After: " & MyTel
    8. End Sub

    -Lou

  9. #9
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Originally posted by Evan
    =)

    You used the wrong works...

    before the code its [ code ]
    After the code its [ /code ]

    Without the spaces though.
    Like This :

    [vbcode]
    ---------Type Your Code Here-----
    [/vbcode]

  10. #10
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    Originally posted by NotLKH
    You ought to fidaddle with the format function.

    Heres an Example Usage:

    VB Code:
    1. Private Sub Command1_Click()
    2. '(808) 531-0000
    3. Dim MyNum As String
    4. Dim MyTel As String
    5.     MyNum = "8085310000"
    6.     MyTel = Format(MyNum, "(000) 000-0000")
    7. MsgBox "Before: " & MyNum & Chr$(13) & "After: " & MyTel
    8. End Sub

    -Lou
    oo thats fantastic code. Im impressed!

  11. #11
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    Originally posted by NotLKH


    Like This :

    [vbcode]
    ---------Type Your Code Here-----
    [/vbcode]

    oh thanks... thats what colors it??? thats very cooooool!

  12. #12
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    You might as well use the Validate event:
    VB Code:
    1. Private Sub Text1_Validate(Cancel As Boolean)
    2.     Text1 = Format(Val(Text1), "(###) ###-####")
    3. End Sub
    You just proved that sig advertisements work.

  13. #13
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    But what if they type in the parathesis cause that code doesn't work if Text1.text="(909) 777-7777"

  14. #14
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Originally posted by Edneeis
    But what if they type in the parathesis cause that code doesn't work if Text1.text="(909) 777-7777"
    Sorry, i mistook what the Val function did. Here
    VB Code:
    1. Private Sub Text1_Validate(Cancel As Boolean)
    2.     Text1 = Format(GetNums(Text1), "(###) ###-####")
    3. End Sub
    4.  
    5. Function GetNums(strText As String) As String
    6.     Dim i As Long
    7.     For i = 1 To Len(strText)
    8.         If IsNumeric(Mid(strText, i, 1)) Then
    9.             GetNums = GetNums & Mid(strText, i, 1)
    10.         End If
    11.     Next
    12. End Function
    You just proved that sig advertisements work.

  15. #15
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    (This code is from Programming Microsoft Visual Basic 6 by Francesco Balena)

    Function FormatPhoneNumber(Text As String) As String
    Dim tmp As String
    If Text <> "" Then
    ' First get rid of all embedded dashes, if any.
    tmp = FilterString(Text, "0123456789")
    ' Then reinsert them in the correct position.
    If Len(tmp) <= 7 Then
    FormatPhoneNumber = Format$(tmp, "!@@@-@@@@")
    Else
    FormatPhoneNumber = Format$(tmp, "!@@@-@@@-@@@@")
    End If
    End If
    End Function



    Function FilterString(Text As String, validChars As String) As String
    Dim i As Long, result As String
    For i = 1 To Len(Text)
    If InStr(validChars, Mid$(Text, i, 1)) Then
    result = result & Mid$(Text, i, 1)
    End If
    Next
    FilterString = result
    End Function

  16. #16
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    by the way guys, look at my code ^, see how when I submited this code all the spacings and indentations are gone; well, how can I indent the code without using space? because I can see the indentations when I type them here, but when I submit they're all gone

  17. #17

  18. #18

    Thread Starter
    Member
    Join Date
    Feb 2001
    Location
    Hawaii
    Posts
    43
    hmmm, the code that joijo wrote (and made 'legible' by Evan) didn't seem to come out. Am I supposed to put code in other places also?

    What I did was this:

    Private Sub Text1_KeyDown(KeyCode as Integer, Shift as Integer)
    Select Case Len(txtPhone.Text)
    Case 0
    txtPhone.Text = "("
    txtPhone.SelStart = 1
    Case 4
    txtPhone.Text = txtPhone. Text & ")"
    txtPhone.SelStart = Len(txtPhone.Text)
    Case 8
    txtPhone.Text = txtPhone.Text & "-"
    txtPhone.SelStart = Len(txtPhone.Text)
    End Select
    End Sub

    So I type the ten-digit phone number in the textbox and press tab to the next box. Are the parentheses supposed pop out when I tab to the next field? Did I do the above wrong? Am I supposed to place it in a particular place? Sorry for being so lame but you know newbies......

    I'll try the other codes in this thread and see what the results are.

    Be back soon!!
    Cynde

  19. #19

    Thread Starter
    Member
    Join Date
    Feb 2001
    Location
    Hawaii
    Posts
    43
    Whoops!! Can you say 'stupid Cynde'???

    I forgot to change

    Private Sub Text1_KeyDown(KeyCode as Integer, Shift as Integer)

    to

    Private Sub txtPhone_KeyDown(KeyCode as Integer, Shift as Integer)

    Well, it did work.......THANKS!!!


    Two problems though (sorry):

    After I type the last four numbers I can still type more in. How can I stop after the very last number is typed in?? Can I limit the max length in the textbox property? I'll try that.

    And if I mistakenly type the first three or four numbers (looked at the wrong area code, for instance, and want to retype it) I can't press my back key to erase and start over.

    I must say, though, that you guys really impress me. I hope one day to whip out a code as fast as you all do.
    Cynde

  20. #20
    Tygur
    Guest
    It looks like you're looking for code that formats the phone number in the textbox as you type. If so, here's some code I just put together. I didn't put any comments in it, so it probably looks complex. I usually try to keep as much of my code in the change event as I can. In this case, all the code is in the change event:
    VB Code:
    1. Private Sub Text1_Change()
    2. Dim bRunning As Boolean
    3. Dim lSS As Long
    4. Dim I As Long
    5. Dim sNumbers As String
    6. Dim lAdd As Long
    7. If bRunning Then
    8.     Exit Sub
    9. Else
    10.     bRunning = True
    11. End If
    12. lSS = Text1.SelStart
    13. For I = 1 To Len(Text1)
    14.     If IsNumeric(Mid(Text1.Text, I, 1)) Then
    15.         sNumbers = sNumbers & Mid(Text1.Text, I, 1)
    16.     Else
    17.         If I <= Text1.SelStart Then
    18.             lSS = lSS - 1
    19.         End If
    20.     End If
    21. Next I
    22. sNumbers = Left(sNumbers, 10)
    23. sNumbers = sNumbers & String(10 - Len(sNumbers), "0")
    24. Text1.Text = Format(sNumbers, "(000) 000-0000")
    25. lAdd = 1
    26. If lSS >= 4 Then
    27.     lAdd = lAdd + 2
    28. End If
    29. If lSS >= 7 Then
    30.     lAdd = lAdd + 1
    31. End If
    32. Text1.SelStart = lSS + lAdd
    33. bRunning = False
    34. End Sub

  21. #21

    Thread Starter
    Member
    Join Date
    Feb 2001
    Location
    Hawaii
    Posts
    43
    Hi Tygur,

    Well, I inserted your code and replaced text1 with txtPhone. I ran my program and the phone number came out all zeroes SO it must mean that I'm not following directions. (Blame the newbie, I say, they have no idea what's going on.) I like working with the change event so I can see the parenthesis pop up around the first three numbers as I'm typing it. I also see that happening in joijo's code. It makes want to work with string functions a little more.

    Anyway, everyone's codes have given me alot of ideas to work with. I'll probably come back tomorrow with something I created, for now I'm tired and need to go to bed and get my 6 hours of sleep.

    zzzzzzz
    Cynde

  22. #22
    Tygur
    Guest
    Actually, the zero's just mark empty slots. Just type. They'll get replaced with the new numbers you type. Maybe I should just change the code to get rid of them.

  23. #23
    Tygur
    Guest
    I took the code I posted before and changed it so it doesn't show the zero's. Also, the variable, bRunning, should've been declared using Static instead of Dim. It wasn't needed anyway so I took it out in this code:
    VB Code:
    1. Private Sub Text1_Change()
    2. Dim lSS As Long
    3. Dim I As Long
    4. Dim sNumbers As String
    5. Dim lAdd As Long
    6. Dim BaseMask As String
    7. Dim lNumCount As Long
    8. BaseMask = "(###) ###-####"
    9.  
    10. lSS = Text1.SelStart
    11. For I = 1 To Len(Text1)
    12.     If IsNumeric(Mid(Text1.Text, I, 1)) Then
    13.         sNumbers = sNumbers & Mid(Text1.Text, I, 1)
    14.     Else
    15.         If I <= Text1.SelStart Then
    16.             lSS = lSS - 1
    17.         End If
    18.     End If
    19. Next I
    20.  
    21. lAdd = 1
    22. If lSS >= 4 Then
    23.     lAdd = lAdd + 2
    24. End If
    25. If lSS >= 7 Then
    26.     lAdd = lAdd + 1
    27. End If
    28.  
    29. sNumbers = Left(sNumbers, 10)
    30. lNumCount = Len(sNumbers)
    31. If lNumCount <= 3 Then
    32.     BaseMask = Left(BaseMask, 1 + lNumCount)
    33. ElseIf lNumCount <= 6 Then
    34.     BaseMask = Left(BaseMask, 3 + lNumCount)
    35. Else
    36.     BaseMask = Left(BaseMask, 4 + lNumCount)
    37. End If
    38.  
    39. Text1.Text = Format(sNumbers, BaseMask)
    40. Text1.SelStart = lSS + lAdd
    41. 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