How to Extract UK Postcodes in Microsoft Access Queries
Question details
The user needs a method to extract the first part or both components of a United Kingdom postcode from a full address field within a Microsoft Access query.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- Extracting specific UK postcode components from varying address strings in a database query, noting that standard functions like Split do not work directly.
- Observed behavior
- The native Split expression causes an error or is not accepted directly within the Access query design grid without a VBA wrapper.
Identify the exact table and field names containing your address data, and ensure your Access database file is placed in a Trusted Location so that custom VBA functions can execute without security blocks.
Use Built-in String Functions for the First Component
Use native Access string functions to quickly extract the first part of a postcode separated by a space, avoiding the need for VBA.
While the Split function works in the Immediate window, it requires a VBA wrapper in queries. For a straightforward extraction of the first postcode block, you can use a combination of Left and InStr functions directly in the query design.
Open your Access database, navigate to the 'Create' tab on the ribbon, and click 'Query Design'.
Select and add the table containing your address or postcode field to the query grid.
In an empty column within the Field row of the query grid, type the expression: PostCodePrefix: Left([YourFieldName], InStr([YourFieldName], " ") - 1), replacing 'YourFieldName' with your actual field name.
Click 'Run' in the Design ribbon to view the datasheet and verify that the first component of the postcode has been successfully extracted.

Create a Custom VBA Function for Full Postcodes
Implement a VBA wrapper function to handle complex address strings, remove trailing country codes, and extract both postcode components.
Manage and Extract Data Easily with WPS Office
While Microsoft Access is highly specialized for relational databases, many everyday data extraction, string manipulation, and cleaning tasks (like extracting postcodes) can be handled more quickly in a spreadsheet. WPS Office is a lightweight, fully compatible alternative to Microsoft Office that offers powerful data tools completely free of charge.

Frequently Asked Questions
Why doesn't the Split expression work directly in an Access query?
Microsoft Access queries utilize the Expression Service, which does not directly support the native VBA Split function. To use Split, you must wrap it inside a custom VBA function and then call that user-defined function from your query grid.
How do I handle trailing country codes like 'UK' or 'GB' in my addresses?
You can handle trailing text by creating a VBA function that evaluates the end of the string using the Right function. If it detects ', UK' or ' GB', it uses the Left function to truncate those characters before executing the main postcode extraction logic.
Can I extract the final two components of an address without using VBA?
It is technically possible by nesting multiple InStrRev, Mid, and Right functions directly in the query expression builder. However, the resulting formula becomes extremely complex and difficult to maintain, which is why a custom VBA wrapper is generally recommended for parsing complex address strings.




