troubleshooting Question

Problem formatting Phone field

Avatar of cekendricks
cekendricks asked on
Microsoft Access
2 Comments1 Solution416 ViewsLast Modified:
I am trying to come up with a function that will properly format the phone number field of a table, if an only if, the record has data (a phone number) in the field.  I have found a couple of example functions online, but the problem is that, while they work for the records where the Phone field is populated, for all those that are blank (presumably Null), the field displays #Error.  As this table, or the query derived from it, serves as the data source for a form that is rather important to our operations, I want the field to remail blank if there is no data to display.  Here are a couple of the functions I have tried:

Public Function FormatPhoneNumber(ByVal sNumToBeFormatted As _
   String) As String

'Takes any entered phone number and returns it in ###-#### format
'or (###) ###-####

Dim iNumberLength As Integer 'Used for the Phone Number length
   
'Trim any leading and trailing spaces

sNumToBeFormatted = Trim$(sNumToBeFormatted)
   
'Length of the phone number.

iNumberLength = Len(sNumToBeFormatted)
   
Select Case iNumberLength
  Case Is < 7
    FormatPhoneNumber = "N / A"
    Exit Function
   
  Case Is > 13
    FormatPhoneNumber = "N / A"
    Exit Function
   
  Case 7  'Format : #######

    FormatPhoneNumber = Left$(sNumToBeFormatted, 3) & _
        "-" & Right$(sNumToBeFormatted, 4)
    Exit Function

  Case 8  'Format : ###-#### or ### ####

    If Mid$(sNumToBeFormatted, 4, 1) = "-" Then
       FormatPhoneNumber = sNumToBeFormatted
       Exit Function
    Else
       FormatPhoneNumber = Left$(sNumToBeFormatted, 3) & "-" & _
          Right$(sNumToBeFormatted, 4)
       Exit Function
    End If

  Case 10 'Format : ##########

 FormatPhoneNumber = "(" & Left$(sNumToBeFormatted, 3) & ") " _
   & Mid$(sNumToBeFormatted, 4, 3) & "-" & _
     Right$(sNumToBeFormatted, 4)
 
   Exit Function

  Case 11 'Format ######-####

 FormatPhoneNumber = "(" & Left$(sNumToBeFormatted, 3) & ") " & _
       Right$(sNumToBeFormatted, 8)
    Exit Function

  Case 12 'Format : ### ###-####

 FormatPhoneNumber = "(" & Left$(sNumToBeFormatted, 3) & ") " & _
      Mid$(sNumToBeFormatted, 5, 3) & "-" & _
      Right$(sNumToBeFormatted, 4)
    Exit Function

  Case 13 'Format : (###)###-####
     FormatPhoneNumber = Left(sNumToBeFormatted, 5) & " " & _
        Right(sNumToBeFormatted, 8)
     Exit Function


  Case Else
        'Return Value Passed
     FormatPhoneNumber = sNumToBeFormatted
                 
End Select  
End Function

Public Function MyPhoneFormat(strPhoneNo As String) As String
    If strPhoneNo Like "##########" Then
        MyPhoneFormat = "(" & Left$(strPhoneNo, 3) & ") " _
        & Mid$(strPhoneNo, 4, 3) & "-" & Right$(strPhoneNo, 4)
    Else
        MyPhoneFormat = strPhoneNo
    End If
End Function


The second function (MyPhoneFormat) was an attempt to update only the newest records in the table which are now being appended via a CSV file import and the Phone field will always be either Null or all numeric digits.  Prior to that the phone numbers can be any format imaginable for an American phone number.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros