Microsoft KB Archive/155134

= ACC: How to Format Fax Numbers to Merge with Microsoft Word =

Article ID: 155134

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q155134



Moderate: Requires basic macro, coding, and interoperability skills.



SUMMARY
This article shows you how to format a fax number in Microsoft Access so that you can merge it with a Microsoft Word document that uses Microsoft Exchange to print to a fax modem.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.



MORE INFORMATION
In Microsoft Word, you can create a merge document that uses Microsoft Exchange to print to a fax modem. However, if the fax number is not in a specific format, Microsoft Exchange cannot process it. When that happens, Microsoft Exchange presents a Check Names dialog box during the merge for each unrecognized fax number it encounters.

The fax number data must be in the following format

[FAX: ]

where is the fax number you are dialing. For example:

[FAX:12065551212]

The following example uses the sample database Northwind.mdb. It shows you how to create a procedure to format your fax numbers, and then uses the procedure in a make-table query. The resulting new table contains only records that have 7- or 10-digit fax numbers.

NOTE: During a mail merge, Microsoft Word can use dynamic data exchange (DDE) or Open Database Connectivity (ODBC) to connect with Microsoft Access. DDE, the most common connection type, recognizes a user-defined procedure in a query. If you use DDE, you can make the query in this example a select query, and use it as the source of data for the mail merge. However, the Microsoft Access 7.0 and 97 ODBC drivers produce an error when you use a select query because the drivers do not recognize the user-defined procedure. The following example uses a make-table query instead of a select query in order to be compatible with both connection methods.

 Open the sample database Northwind.mdb.  Create a module and type the following line in the Declarations section: Option Explicit   Type the following procedure:

NOTE: In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this code. Function FaxNumFormat(FaxNum)

' If the fax number field is empty, return "Invalid Fax" and exit. If IsNull(FaxNum) Then FaxNumFormat = "Invalid Fax Number" Exit Function End If

Dim Counter As Integer Dim NewString As String

' Strip out all characters except numbers. For Counter = 1 To Len(FaxNum) If IsNumeric(Mid(FaxNum, Counter, 1)) Then NewString = NewString & Mid(FaxNum, Counter, 1) End If      Next Counter

' If NewString is 10 digits long and the call is local, format the ' digits to the right of the area code. If Len(NewString) = 10 And _ Left(NewString,3) = "" Then NewString = Right(NewString,7) NewString = Format(NewString,"\[\F\A\X\:#\]")

' If NewString is 10 digits long, and the call is long distance, add ' the long distance prefix '1' to the beginning, then format ' accordingly. ElseIf Len(NewString) = 10 Then NewString = "1" & NewString NewString = Format(NewString, "\[\F\A\X\:#\]") ' If NewString is 7 digits long, format accordingly. ElseIf Len(NewString) = 7 Then NewString = Format(NewString, "\[\F\A\X\:#\]") Else ' All other numbers are invalid. NewString = "Invalid Fax Number" End If      FaxNumFormat = NewString

End Function  Save the module as basFaxNumber and close it. Create a new query in Design view and add the Suppliers table.  In the query grid, add the following fields: <pre class="fixed_text">      Field: CompanyName Show: Yes Field: ContactName Show: Yes Field: FaxNbr: FaxNumFormat([Fax]) Show: Yes Criteria: <>"Invalid Fax Number" </li> On the Query menu, click Make Table.</li> In the Make Table dialog box, type the name tblMergeFax and click OK.</li> On the Query menu, click Run.</li> When you see the prompt "Are you sure you want to create a new table with the selected records?" click Yes.</li> Open the table tblMergeFax.</li>  Note that all records in the table contain the correct fax number format in the FaxNbr field. For example: <pre class="fixed_text">      CompanyName                 ContactName     FaxNbr -  -   -       Grandma Kelly's Homestead   Regina Murphy   [FAX:13135553349] </li> Use the table tblMergeFax to merge with Microsoft Word.</li></ol>

<div class="references_section">