How to Extract Phone Numbers from Text in Microsoft Access Using VBA
Question details
The user needs to extract ten-digit phone numbers from mixed text strings in Microsoft Access while ignoring unrelated alphanumeric codes.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- Cleaning and extracting specific data from a database field that contains mixed text, such as alphanumeric codes and standard phone numbers.
- Observed behavior
- Native string functions like Split are insufficient or return unrecognized function errors in queries, requiring a regular expression approach to isolate specific patterns.
Before running regular expressions in Microsoft Access, ensure you have enabled the 'Microsoft VBScript Regular Expressions 5.5' library in your VBA Editor References.
Use a Custom VBA Regular Expression Function
Create a custom VBA function utilizing regular expressions to accurately identify and extract phone numbers from mixed text.
Regular expressions (Regex) provide a powerful way to match specific text patterns. By using the pattern `([0-9]{3}-?[0-9]{3}-?[0-9]{4})`, you can capture standard 10-digit phone numbers regardless of where they appear in the string, while ignoring unrelated alphanumeric codes.
In Microsoft Access, press ALT + F11 to open the Visual Basic for Applications (VBA) editor.
Go to Tools > References in the top menu, scroll down, and check the box for 'Microsoft VBScript Regular Expressions 5.5'. Click OK.
Click Insert > Module, and paste a custom function script that defines a New RegExp object. Set its `.Pattern` to `([0-9]{3}-?[0-9]{3}-?[0-9]{4})` and `.Global = True` to find all matches.
Save the module. Open your query in Design View, and create a calculated field by typing an alias and your function name, for example: `Phones: ExtractPhoneNumbers([YourTextField])`.

Use the Split Function for Delimited Text
If your text follows a strict delimited format (like commas), you can use the VBA Split function combined with pattern matching.
Streamline Your Data Processing with WPS Office
If dealing with Microsoft Access databases and complex VBA functions feels overwhelming, consider migrating your data tasks to a highly capable spreadsheet environment. WPS Spreadsheet handles large datasets effortlessly, supports advanced Macros/VBA, and provides an intuitive, lightweight alternative for your daily data extraction needs.
- 1. Export Access Data: Export your Microsoft Access tables to a standard .xlsx or .csv format.
- 2. Open in WPS Spreadsheet: Launch WPS Office and open your exported data file to view your records in a clear grid interface.
- 3. Extract Data Easily: Use WPS Spreadsheet's built-in text manipulation functions or its fully supported VBA environment to extract phone numbers effortlessly.

Frequently Asked Questions
Why does the Split function return an 'unrecognized function' error in an Access query?
The Microsoft Access SQL engine cannot directly evaluate the VBA Split function because it returns an array rather than a single value. To use it in a query, you must wrap the Split function inside a custom Public Function in a VBA module that processes the array and returns a string.
How do I return multiple phone numbers from a single row into an Access query?
Your custom VBA Regex function should loop through the `MatchCollection` object. As it finds matches, concatenate them into a single string separated by commas (e.g., `Result = Result & Match.Value & ", "`), and have the function return this combined string back to the query.
Can I extract international phone numbers using this VBA regex?
Yes, but you will need to modify the regular expression pattern. To include optional country codes and plus signs, you can adjust your pattern to something like `(\+?[0-9]{1,3}[- ]?[0-9]{3}[- ]?[0-9]{3}[- ]?[0-9]{4})` to accommodate different global formats.




