ACC: How to Format Phone Number Fields Using an Update QueryID: Q180816
|
Moderate: Requires basic macro, coding, and interoperability skills.
If you use an input mask to format a field that contains phone numbers, but
you choose not to store the formatting characters, the phone numbers in
this field do not appear formatted when you use the table as the data
source of a mail merge in Microsoft Word. In addition, the phone numbers do
not appear formatted if you export the table to a text file. This article
demonstrates how to use sample Visual Basic for Applications code with
either an update query or a select query to create a new field that
contains the phone numbers with correct formatting characters.
Microsoft provides programming examples for illustration only, without warranty
either expressed or implied, including, but not limited to, the implied warranties of
merchantability and/or fitness for a particular purpose. This article assumes that you
are familiar with the programming language being demonstrated and the tools used to
create and debug procedures. Microsoft support professionals can help explain the functionality
of a particular procedure, but they will not modify these examples to provide added
functionality or construct procedures to meet your specific needs. If you have limited
programming experience, you may want to contact a Microsoft Certified Solution Provider
or the Microsoft fee-based consulting line at (800) 936-5200. For more information about
Microsoft Certified Solution Providers, please see the following page on the World Wide Web:
http://www.microsoft.com/mcsp/For more information about the support options available from Microsoft, please see the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/overview/overview.asp
There are two methods for formatting phone numbers so that both the data and formatting characters appear when you perform a mail merge in Microsoft Word or when you export the data to a text file. You can use an update query to change the data in the original table so that it includes the formatting characters. Or you can create a select query that contains an expression that returns the phone number data with the formatting characters. You can then export the select query to text or use it as the source of a mail merge in Microsoft Word. Both of the following examples use a sample Visual Basic for Applications procedure to format the phone number.
tblPhoneNumbers
--------------------------
Field Name: OriginalNumber
Data Type: Text
Field Name: NewNumber
Data Type: Text
OriginalNumber NewNumber
-------------- ---------
(111) 123-4567
( ) 345-6789
NOTE: No values are entered in the NewNumber field.
Function ConvertPhone(phone As String)
Dim PhoneLen As Integer
On error Goto ConvertPhone_err
' Determine if a valid number exists. If not, exit function.
If Len(phone) = 7 Or Len(phone) = 10 Then
PhoneLen = Len(phone)
Else
Exit Function
End If
' Format number and return value to function name.
Select Case PhoneLen
Case 7
ConvertPhone = "( ) " & Left(phone, 3) & _
"-" & Right(phone, 4)
Case 10
ConvertPhone = "(" & Left(phone, 3) & ") " _
& Mid(phone, 4, 3) & _
"-" & Right(phone, 4)
End Select
ConvertPhone_exit:
Exit Function
ConvertPhone_err:
' Use the following line if you are
' running Microsoft Access 7.0 or 97.
MsgBox Cstr(Err) & " " & Err.Description
' Use the following line if you are
' running Microsoft Access 2.0.
' MsgBox CStr(Error) & " " & Error(Err)
Resume ConvertPhone_exit
End Function
ConvertPhone([OriginalNumber])
NewPhone: ConvertPhone([OriginalNumber])
For more information about input masks, search the Help Index for "Create an input mask to control how data is entered in a field or control," or ask the Microsoft Access 97 Office Assistant.
Additional query words: inf phone number mail merge Word
Keywords : IntpPrtm QryUpdat
Version : WINDOWS:2.0,7.0,97
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: July 6, 1999