Access VBA String Functions: Step-by-Step Guide
In this article
Microsoft Access VBA string functions let you clean, slice, search, and rebuild text inside forms, reports, queries, and macros. The core set you will use most is Len, Left, Right, Mid, InStr, InStrRev, Replace, Trim, LTrim, RTrim, UCase, LCase, StrComp, StrReverse, and Format. They are built into VBA, so there is nothing to install or enable. The shortest working path is: open the VBA editor, add a standard module, write a small Sub that calls the function, and run it with F5 or step through it with F8.
What You'll Need
- Microsoft Access desktop application (Microsoft 365 or a recent perpetual version). The steps below use the desktop VBA editor; Access for the web does not include the VBA editor.
- A database where macros are enabled, or a blank database you can use for testing.
- Basic familiarity with opening a module and reading the Immediate window.
Menu labels and dialog wording can differ slightly by Access version, device, and region, so treat the exact names below as a guide rather than a guarantee.
Step-by-Step
- Open the VBA editor. In Access, press Alt+F11. If nothing appears, make sure the database window is active and try again. On a Mac, the shortcut may differ or the editor may be unavailable depending on the Access build; the reliable path is the desktop Windows version.
- Insert a standard module. In the VBA editor menu, choose Insert > Module. A blank code window opens. Standard modules are where general-purpose string helpers belong; form and report modules are for code tied to a specific object.
- Turn on
Option Explicit. At the very top of the module, typeOption Explicit. This forces every variable to be declared, which prevents the most common cause of silent string bugs: a misspelled variable name that VBA treats as an empty string. - Write a test procedure. Paste a small
Subthat exercises the functions you care about. For example:Sub TestStrings() Dim s As String s = " Access VBA " Debug.Print Len(s) ' 14 Debug.Print Trim(s) ' "Access VBA" Debug.Print UCase(Trim(s)) ' "ACCESS VBA" Debug.Print Left(Trim(s), 6) ' "Access" Debug.Print Right(Trim(s), 3) ' "VBA" Debug.Print Mid(Trim(s), 8, 3) ' "VBA" Debug.Print InStr(1, s, "VBA") ' 10 Debug.Print Replace(s, "VBA", "Code") ' " Access Code " End Sub - Open the Immediate window. Press Ctrl+G. This is where
Debug.Printoutput appears. If the window is already visible, the shortcut just moves focus to it. - Run the procedure. Click inside the
Suband press F5. Each result prints on its own line in the Immediate window. If you get a compile error, check that the module starts withOption Explicitand that every variable is declared withDim. - Step through line by line. Press F8 instead of F5 to execute one line at a time. This is the fastest way to see which function returns an unexpected value. Press F5 again to run to the end.
- Use the functions in a query or form. You can call these functions from an expression in a query, a control source, or a validation rule. For example, a control source could be
=Trim([FirstName]) & " " & Trim([LastName]). In a query, create a calculated field with an expression such asLeft([Phone], 3). Keep in mind that Access query expressions use the same function names but are evaluated by the Access expression service, not VBA, so a few edge cases differ. - Handle Nulls explicitly. VBA string functions return
Nullwhen givenNull. Wrap fields inNz()before passing them in, for exampleTrim(Nz([MiddleName], "")). Without this, concatenation and length calculations silently produceNull. - Test edge cases. Try empty strings, strings with leading and trailing spaces, strings shorter than the number of characters you request, and text containing the search term more than once.
Midwith a start position past the end returns an empty string;InStrreturns 0 when the term is not found. Confirm these behaviours in the Immediate window before relying on them in production code. - Save and reuse. Save the module with Ctrl+S and give it a descriptive name. Put reusable helpers in their own module so you can call them from forms, reports, and other modules without duplicating code.
Common Mistakes
- Forgetting that string positions are 1-based.
Mid("Access", 1, 3)returns "Acc". Using 0 as the start position causes an error or unexpected results. - Ignoring
Null. Any string function applied to aNullfield returnsNull. UseNz()first. - Confusing
Lenwith the visible length.Lencounts every character, including spaces. Trim before measuring if you want the meaningful length. - Assuming
Replaceis case-sensitive. By default, VBA'sReplaceis case-sensitive. If you need case-insensitive replacement, convert both sides withLCaseorUCasefirst, or write a small loop. - Using the wrong comparison mode.
StrComptakes a compare argument. Omitting it uses the module's default, which may be binary or text depending onOption Compare. State it explicitly to avoid surprises. - Putting general helpers in a form module. Code in a form module is only reachable while that form is loaded. General string helpers belong in a standard module.
Tips
- Use the Immediate window as a scratchpad: type
? Left("Hello", 2)and press Enter to see the result without writing a procedure. - Chain functions for readable transformations:
UCase(Trim(Replace(s, "-", " "))). - Use
InStrRevwhen you need the last occurrence of a character, such as extracting a file extension or the part after the final backslash. - Use
StrCompwithvbTextComparefor case-insensitive equality checks instead of converting both strings. - Keep test procedures in a separate module so you can delete them before distributing the database.
- Comment each helper with the expected input and output; string functions are easy to misuse six months later.
Troubleshooting
If a procedure will not run, first check that the code is in a standard module and not behind a form that is closed. If you see a compile error about an undefined variable, confirm Option Explicit is at the top and every variable has a Dim. If results are blank, check for Null inputs and wrap them in Nz(). If the Immediate window shows nothing, make sure you are looking at the right window (Ctrl+G) and that the procedure actually reached the Debug.Print lines; step with F8 to confirm. If a function behaves differently in a query than in VBA, rewrite the expression using Access query syntax or move the logic into VBA and call it from the query. If the database blocks macros, enable content only for databases you trust; do not lower macro security settings for unknown files.
Frequently Asked Questions
Are these functions available in Access for the web?
No. Access for the web does not include the VBA editor, so VBA string functions are only available in the desktop application.
What is the difference between InStr and InStrRev?
InStr searches from the start of the string; InStrRev searches from the end and returns the position of the last match.
Why does my string function return Null?
The input was Null. Wrap the field or variable in Nz() before passing it to the function.
Can I use these functions in a query?
Yes, with the same names, but the query expression service evaluates them, and a few edge cases differ from VBA. Test the expression in a small query first.
How do I make Replace case-insensitive?
Convert both the source string and the search term with LCase or UCase before calling Replace, or write a loop that compares with StrComp(..., vbTextCompare).
Is there a built-in function to split text into parts?
Yes. Split returns a zero-based array of substrings based on a delimiter. Use LBound and UBound to loop through the results.