How to Fix Microsoft Access Crashes When Using InStr Function
Question details
Microsoft Access crashes or stops responding when executing an InStr expression in the AfterUpdate event of a form.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- Running VBA code in a form's AfterUpdate event to search for specific string values to enable or disable controls.
- Observed behavior
- The application stops responding completely. The issue persists even after compacting, repairing, debugging, or recreating the Access database.
Before modifying your VBA code, open the VBA Editor and compile your database to rule out any glaring syntax errors, and temporarily disable the AfterUpdate event to confirm it is the source of the crash.
Replace the InStr Function with a Custom Split Function
Create a custom function in a general module using the Split method to bypass the instability caused by InStr in the AfterUpdate event.
In certain specific form event scenarios, the built-in InStr function can cause an unhandled loop or memory issue, causing Access to freeze. Utilizing a custom search function based on the Split command accomplishes the same goal more reliably.
Press Alt + F11 while in Microsoft Access to open the Visual Basic for Applications (VBA) editor.
Click on 'Insert' in the top menu and select 'Module' to create a new general module outside of your specific form's code.
Write a custom function (e.g., named Occurs) that uses the VBA Split function to search for the required string values and returns the count.
Navigate back to your form's AfterUpdate event code. Replace the existing InStr calls with calls to your new Occurs function.
Configure the event to check if the Occurs function returns a value greater than zero, and use that result to enable or disable the required controls (e.g., SCAP_Countries or SIAP_Type).

Decompile and Recompile the Database
If replacing the VBA function does not fully resolve the crash, decompiling the database can clear corrupted compiled code states.
Manage Data Smoothly with WPS Office
While WPS Office does not feature a direct database application like Microsoft Access, you can easily handle extensive data sets, lists, and form logic using WPS Spreadsheet. Avoid complex VBA crashes and easily organize your data in a highly compatible environment.

Frequently Asked Questions
Why does the InStr function cause Microsoft Access to crash?
While InStr is a standard VBA function, combining it with specific data-bound form events like AfterUpdate can sometimes trigger an unhandled loop or conflict with the form's control states, causing the application to hang or crash.
Will compacting and repairing the database fix VBA crashes?
Compacting and repairing is excellent for fixing general database corruption and bloat. However, it rarely resolves code-level bugs or specific function incompatibilities like the InStr crash within an AfterUpdate event.
How do I identify redundant VBA code in my Access project?
Open the VBA Editor and manually review your event handlers to ensure you aren't running duplicate checks, repeating the same string searches, or running unnecessary nested loops. Consolidating repeated actions into a single general-module function helps reduce redundancy.




