|
-
Dec 4th, 2001, 03:58 PM
#1
Thread Starter
Member
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.
-
Dec 4th, 2001, 04:58 PM
#2
Hyperactive Member
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ª
-
Dec 4th, 2001, 05:05 PM
#3
Frenzied Member
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
-
Dec 4th, 2001, 05:07 PM
#4
Thread Starter
Member
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.
-
Dec 4th, 2001, 05:10 PM
#5
Frenzied Member
=) It should work. If it doesnt I will come up with something....
-
Dec 4th, 2001, 05:12 PM
#6
Hyperactive Member
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ª
-
Dec 4th, 2001, 05:14 PM
#7
Frenzied Member
=)
You used the wrong works...
before the code its [ code ]
After the code its [ /code ]
Without the spaces though.
-
Dec 4th, 2001, 05:24 PM
#8
You ought to fidaddle with the format function.
Heres an Example Usage:
VB Code:
Private Sub Command1_Click()
'(808) 531-0000
Dim MyNum As String
Dim MyTel As String
MyNum = "8085310000"
MyTel = Format(MyNum, "(000) 000-0000")
MsgBox "Before: " & MyNum & Chr$(13) & "After: " & MyTel
End Sub
-Lou
-
Dec 4th, 2001, 05:26 PM
#9
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]
-
Dec 4th, 2001, 05:28 PM
#10
Frenzied Member
Originally posted by NotLKH
You ought to fidaddle with the format function.
Heres an Example Usage:
VB Code:
Private Sub Command1_Click()
'(808) 531-0000
Dim MyNum As String
Dim MyTel As String
MyNum = "8085310000"
MyTel = Format(MyNum, "(000) 000-0000")
MsgBox "Before: " & MyNum & Chr$(13) & "After: " & MyTel
End Sub
-Lou
oo thats fantastic code. Im impressed!
-
Dec 4th, 2001, 05:29 PM
#11
Frenzied Member
Originally posted by NotLKH
Like This :
[vbcode]
---------Type Your Code Here-----
[/vbcode]
oh thanks... thats what colors it??? thats very cooooool!
-
Dec 4th, 2001, 05:32 PM
#12
Frenzied Member
You might as well use the Validate event:
VB Code:
Private Sub Text1_Validate(Cancel As Boolean)
Text1 = Format(Val(Text1), "(###) ###-####")
End Sub
You just proved that sig advertisements work.
-
Dec 4th, 2001, 06:09 PM
#13
But what if they type in the parathesis cause that code doesn't work if Text1.text="(909) 777-7777"
-
Dec 4th, 2001, 06:36 PM
#14
Frenzied Member
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:
Private Sub Text1_Validate(Cancel As Boolean)
Text1 = Format(GetNums(Text1), "(###) ###-####")
End Sub
Function GetNums(strText As String) As String
Dim i As Long
For i = 1 To Len(strText)
If IsNumeric(Mid(strText, i, 1)) Then
GetNums = GetNums & Mid(strText, i, 1)
End If
Next
End Function
You just proved that sig advertisements work.
-
Dec 4th, 2001, 06:52 PM
#15
(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
-
Dec 4th, 2001, 06:59 PM
#16
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
-
Dec 4th, 2001, 07:13 PM
#17
Nest the code in:
[vbcode]
place code here
[/vbcode]
-
Dec 5th, 2001, 01:01 AM
#18
Thread Starter
Member
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!!
-
Dec 5th, 2001, 01:19 AM
#19
Thread Starter
Member
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.
-
Dec 5th, 2001, 02:00 AM
#20
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:
Private Sub Text1_Change()
Dim bRunning As Boolean
Dim lSS As Long
Dim I As Long
Dim sNumbers As String
Dim lAdd As Long
If bRunning Then
Exit Sub
Else
bRunning = True
End If
lSS = Text1.SelStart
For I = 1 To Len(Text1)
If IsNumeric(Mid(Text1.Text, I, 1)) Then
sNumbers = sNumbers & Mid(Text1.Text, I, 1)
Else
If I <= Text1.SelStart Then
lSS = lSS - 1
End If
End If
Next I
sNumbers = Left(sNumbers, 10)
sNumbers = sNumbers & String(10 - Len(sNumbers), "0")
Text1.Text = Format(sNumbers, "(000) 000-0000")
lAdd = 1
If lSS >= 4 Then
lAdd = lAdd + 2
End If
If lSS >= 7 Then
lAdd = lAdd + 1
End If
Text1.SelStart = lSS + lAdd
bRunning = False
End Sub
-
Dec 5th, 2001, 02:40 AM
#21
Thread Starter
Member
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
-
Dec 5th, 2001, 03:38 AM
#22
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.
-
Dec 5th, 2001, 04:06 AM
#23
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:
Private Sub Text1_Change()
Dim lSS As Long
Dim I As Long
Dim sNumbers As String
Dim lAdd As Long
Dim BaseMask As String
Dim lNumCount As Long
BaseMask = "(###) ###-####"
lSS = Text1.SelStart
For I = 1 To Len(Text1)
If IsNumeric(Mid(Text1.Text, I, 1)) Then
sNumbers = sNumbers & Mid(Text1.Text, I, 1)
Else
If I <= Text1.SelStart Then
lSS = lSS - 1
End If
End If
Next I
lAdd = 1
If lSS >= 4 Then
lAdd = lAdd + 2
End If
If lSS >= 7 Then
lAdd = lAdd + 1
End If
sNumbers = Left(sNumbers, 10)
lNumCount = Len(sNumbers)
If lNumCount <= 3 Then
BaseMask = Left(BaseMask, 1 + lNumCount)
ElseIf lNumCount <= 6 Then
BaseMask = Left(BaseMask, 3 + lNumCount)
Else
BaseMask = Left(BaseMask, 4 + lNumCount)
End If
Text1.Text = Format(sNumbers, BaseMask)
Text1.SelStart = lSS + lAdd
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|