ACC2000: Sample Function to Remove Alpha Characters from a Numeric Field

ID: Q210537


The information in this article applies to:
  • Microsoft Access 2000

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


SUMMARY

This article shows you how to create a sample user-defined function named RemoveAlphas() that removes 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 a social security number field. For example, the following strings contain parentheses, dashes, and spaces:

"(206) 635-7050"
"206-635-7050"
"535-87-4529"
After you run the RemoveAlphas() function, these strings will look as follows:
"2066357050"
"2066357050"
"535874529"


MORE INFORMATION

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

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

How to Create the RemoveAlphas() Function

  1. Create a new module.


  2. Type the following function in the Module window:


  3. 
    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$ >= "0" And A_Char$ <= "9" Then
             Clean$ = Clean$ + A_Char$
          End If
       Next Pos
    
       RemoveAlphas = Clean$
    
    End Function 
  4. Save the module with any unique name.


How to Use the RemoveAlphas() Function in Data Entry

You can use the RemoveAlphas() function to have dashes or parentheses removed while someone is typing phone numbers 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

  1. Create a new query based on the table with the phone number field.


  2. Place the phone number field in the first column of the query grid.


  3. On the Query menu, click Update.


  4. In the Update To row, enter the following for a field named Phone:


  5. RemoveAlphas([Phone])
  6. Run the query.


Additional query words: inf ssn hyphens

Keywords : kbprg kbdta AccCon KbVBA
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: September 10, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.