Others

How to Replace Spaces with Underscores in an Access Text Field

Camila MilosovichCamila Milosovich Nov 3, 2026 869 views

Question details

The user needs to replace spaces with underscores in a Microsoft Access text field without triggering errors when the field contains Null or empty values.

How to Replace Spaces with Underscores in an Access Text Field
Product
Microsoft Access
Device & OS
not provided
Scenario
Formatting text output for a query or report where spaces are not permitted or underscores are preferred for readability.
Observed behavior
The goal is to safely output text with underscores instead of spaces, as applying the Replace function directly on a Null value returns an invalid use of null error.
Before you start

Before modifying your database, verify whether you need to permanently alter the raw data in your tables or just change how it displays. It is highly recommended to apply text transformations in a query or report to preserve the original entries.

Solution 1Recommended

Use the Replace and Nz Functions in a Select Query

Combine the Replace and Nz functions to safely convert spaces to underscores while transforming any Null values to empty strings to prevent calculation errors.

Applying formatting at the query level is the most effective way to manipulate text in Access. The Nz() function handles the empty records, ensuring the Replace() function only attempts to process valid text strings.

1
Open Query Design

Open your Microsoft Access database, navigate to the Create tab on the top ribbon, and click on Query Design. Add the table containing your target text field.

2
Enter the formatting expression

Click into an empty column in the Field row of the query design grid. Type the following expression, replacing 'Note' with your actual field name: FormattedNote: Replace(Nz([Note],"")," ","_")

3
Run the Query

Click the Run button (red exclamation mark) in the Design ribbon. The results will display a new column where all spaces are replaced with underscores, and empty fields remain safely blank.

Use the Replace and Nz Functions in a Select Query
Best Practice: By doing this in a Select query, you preserve your original unformatted data in the underlying table, allowing you to use the original text for other purposes.
Free Microsoft Office alternative

Looking for a lightweight alternative to Microsoft Office?

While Microsoft Access handles complex relational databases, many text manipulation and data management tasks can be easily accomplished using spreadsheets. WPS Office is a free, lightweight suite that includes Writer, Spreadsheet, and Presentation. It offers full compatibility with Microsoft Office formats, allowing you to manage large datasets and replace text effortlessly.

  1. 1. Open your data in WPS Spreadsheet: Export your Access table to an Excel format and open it in WPS Spreadsheet.
  2. 2. Use the SUBSTITUTE function: In a blank cell next to your text, enter the formula =SUBSTITUTE(A2, " ", "_") to replace spaces with underscores.
  3. 3. Apply to all rows: Drag the fill handle down to apply the formula to the rest of your dataset.
100% compatible with Microsoft Excel (.xlsx), Word (.docx), and PowerPoint (.pptx) formats.Easily clean and format text data using powerful Spreadsheet functions like SUBSTITUTE without complex database queries.Free, lightweight, and features a familiar user interface for a seamless transition from Microsoft Office.
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get an 'Invalid Use of Null' error when using Replace in Access?

The Replace function in Microsoft Access cannot process Null values. If a field in your table is completely empty (Null), passing it directly to Replace throws an error. Wrapping the field in the Nz() function, like Nz([Field], ""), converts the Null to an empty string, allowing the Replace function to execute successfully.

Can I permanently replace spaces with underscores in the Access table?

Yes, you can use an Update Query to change the raw data. Set the 'Update To' row of your target field to Replace([Field]," ","_"). However, you must add 'Is Not Null' in the Criteria row to prevent errors on empty records. Keep in mind that this permanently alters your database entries.

Is there a similar function to Replace for spreadsheets like Excel or WPS Office?

Yes. In Excel and WPS Spreadsheet, the equivalent text manipulation function is SUBSTITUTE. You can use a formula like =SUBSTITUTE(A1, " ", "_") to swap all spaces in cell A1 for underscores. Unlike Access, empty cells in spreadsheets do not trigger a Null error when using this function.