VBA & Macro Problems

How to Extract Phone Numbers from Text in Microsoft Access Using VBA

Anum MushtaqAnum Mushtaq Nov 3, 2026 869 views

Question details

The user needs to extract ten-digit phone numbers from mixed text strings in Microsoft Access while ignoring unrelated alphanumeric codes.

How to Extract Phone Numbers from Text in Microsoft Access Using VBA
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 you start

Before running regular expressions in Microsoft Access, ensure you have enabled the 'Microsoft VBScript Regular Expressions 5.5' library in your VBA Editor References.

Solution 1Recommended

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.

1
Open the VBA Editor

In Microsoft Access, press ALT + F11 to open the Visual Basic for Applications (VBA) editor.

2
Enable Regex Reference

Go to Tools > References in the top menu, scroll down, and check the box for 'Microsoft VBScript Regular Expressions 5.5'. Click OK.

3
Create a New Module

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.

4
Call the Function in a Query

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 a Custom VBA Regular Expression Function
Global Matching: Setting the `.Global` property to True ensures that if a single text field contains multiple phone numbers, the function will evaluate and capture all of them.
Free Microsoft Office alternative

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. 1. Export Access Data: Export your Microsoft Access tables to a standard .xlsx or .csv format.
  2. 2. Open in WPS Spreadsheet: Launch WPS Office and open your exported data file to view your records in a clear grid interface.
  3. 3. Extract Data Easily: Use WPS Spreadsheet's built-in text manipulation functions or its fully supported VBA environment to extract phone numbers effortlessly.
Fully compatible with Microsoft Excel (.xlsx, .xlsm, .csv) formats.Supports advanced VBA and Macros for automated data extraction and regex processing.Lightweight installation with a highly familiar, easy-to-use interface.Free all-in-one suite for Word, Excel, PowerPoint, and PDF editing.
microsoft office alternative - wps office

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.