Posted By

corydeppen on 06/25/07


Tagged

access vba 2003 vb


Versions (?)

Format Phone Number


 / Published in: Visual Basic
 

  1. Function formatPhoneNbr(PhoneNbrStr As String) As String
  2.  
  3. Dim tmpNbr As String
  4. Dim tmpPos As String
  5. Dim tmpLen As Byte
  6. Dim i As Byte
  7.  
  8. ' Get all numbers
  9. For i = 1 To Len(PhoneNbrStr)
  10. tmpPos = Mid(PhoneNbrStr, i, 1)
  11. If IsNumeric(tmpPos) Then
  12. tmpNbr = tmpNbr & tmpPos
  13. End If
  14. Next i
  15.  
  16. ' Get phone number length
  17. tmpLen = Len(tmpNbr)
  18.  
  19. ' Output formatted phone number
  20. Select Case tmpLen
  21. Case 7 ' No area code
  22. formatPhoneNbr = Left(tmpNbr, 3) & "-" & Right(tmpNbr, 4)
  23.  
  24. Case 10 ' Include area code
  25. formatPhoneNbr = "(" & Left(tmpNbr, 3) & ") " & _
  26. Mid(tmpNbr, 4, 3) & "-" & Right(tmpNbr, 4)
  27.  
  28. Case Else
  29. formatPhoneNbr = "Invalid Phone Number"
  30.  
  31. End Select
  32.  
  33. End Function

Report this snippet  

You need to login to post a comment.