Avatar of cekendricks
cekendricks
 asked on

Problem formatting Phone field

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.
Microsoft Access

Avatar of undefined
Last Comment
Dale Fye

8/22/2022 - Mon
cekendricks

ASKER
Well I guess I should have just kept on plugging just a little bit longer before I cried out for help.

I was able to get the results I wanted by simply formatting the Phone field in the query:

Phonee: IIf([Phone] Like "##########","(" & Left$([Phone],3) & ") " & Mid$([Phone],4,3) & "-" & Right$([Phone],4),[Phone])

and it worked.
ASKER CERTIFIED SOLUTION
Dale Fye

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy