Microsoft KB Archive/99938

= ACC: Sample Function to Remove Alphas from a Numeric Field =

Article ID: 99938

Article Last Modified on 1/18/2007

-

APPLIES TO


 * Microsoft Access 1.0 Standard Edition
 * Microsoft Access 1.1 Standard Edition
 * Microsoft Access 2.0 Standard Edition
 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q99938



SUMMARY
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article shows you how to create a sample Microsoft Access function called RemoveAlphas to remove alpha, or nonnumeric, characters from an alphanumeric string. All nonnumeric characters will be removed.

A common use for the RemoveAlphas function is to remove the parentheses, dashes, and spaces from a telephone number or social security number field. The following strings contain parentheses, dashes, and spaces:

&quot;(206) 635-7050&quot;

&quot;206-635-7050&quot;

&quot;535-87-4529&quot;

After the RemoveAlphas function is run, these strings will be:

&quot;2066357050&quot;

&quot;2066357050&quot;

&quot;535874529&quot;

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 &quot;Building Applications with Microsoft Access&quot; manual.

NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access versions 1.x and 2.0. For more information about Access Basic, please refer to the &quot;Introduction to Programming&quot; manual in Microsoft Access version 1.x or the &quot;Building Applications&quot; manual in Microsoft Access version 2.0



MORE INFORMATION
The following example demonstrates how to create the RemoveAlphas function, and how to use it either while data is being entered, or in an update query to remove nonnumeric characters from phone numbers in a table.

How to Create the RemoveAlphas Function
 Create a new module. On the Insert menu, click Procedure.

NOTE: In Microsoft Access 2.0 or earlier, on the Edit menu, click New Procedure. Type RemoveAlphas in the Name box, and select Function in the Type box. Click OK.  Type the following function in the Module window: Function RemoveAlphas (ByVal AlphaNum as Variant) Dim Clean As String Dim Pos, A_Char$

Pos = 1 If IsNull(AlphaNum) Then Exit Function

For Pos = 1 To Len(AlphaNum) A_Char$ = Mid(AlphaNum, Pos, 1) If A_Char$ >= &quot;0&quot; And A_Char$ <= &quot;9&quot; Then Clean$ = Clean$ + A_Char$ End If        Next Pos

RemoveAlphas = Clean$

End Function  Save the module with any unique name.

How to Use the RemoveAlphas Function in Data Entry
You can use the RemoveAlphas function to remove dashes or parentheses while phone numbers are being typed into a field.

For a field called Phone, add the following code to the AfterUpdate property to the Phone text box on a form: Private Sub Phone_AfterUpdate Me![Phone] = RemoveAlphas(Me![Phone]) End Sub

How to Use the RemoveAlphas Function in an Update Query
 Create a new query based on the table with the phone number field.</li> Place the phone number field in the first column of the query grid.</li> On the Query menu, click Update.</li> In the Update To row, enter the following for a field named Phone:

RemoveAlphas([Phone])

</li> Run the query.</li></ol>

Additional query words: ssn hyphens

Keywords: kbinfo kbprogramming KB99938

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.