How to Fix VBA Declare Errors in 64-Bit Microsoft Access
Question details
Users need to resolve VBA errors occurring in older Microsoft Access databases when running command buttons that execute Windows API code incompatible with 64-bit versions of Office.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- Migrating or opening an older Microsoft Access database in a 64-bit version of Microsoft Office.
- Observed behavior
- The database displays compilation errors because the older 32-bit Windows API VBA declarations lack the required pointer-safe data types for VBA7.
Before modifying your code, open the VBA Editor in Access and use the search function to locate all modules containing Windows API 'Declare' statements.
Update Windows API Declarations with PtrSafe and LongPtr
Modify existing VBA declarations to make them natively compatible with 64-bit Office by using the PtrSafe keyword and updating specific pointer data types.
In 64-bit versions of Office, VBA requires the PtrSafe keyword to explicitly declare that the API call is safe for 64-bit execution. Additionally, variables that represent memory pointers or window handles must be updated to the LongPtr data type.
Press Alt + F11 while in your Microsoft Access database to open the Visual Basic for Applications (VBA) editor.
Search your code modules for the word 'Declare'. Find the statements defining the external Windows API functions.
Insert 'PtrSafe' immediately after the 'Declare' keyword. For example, change 'Declare Function SetCursorPos...' to 'Declare PtrSafe Function SetCursorPos...'.
Identify any parameters or return values that handle memory addresses or window handles (such as 'hwnd'). Change their data type from 'Long' to 'LongPtr'.

Use Conditional Compilation for VBA6 and VBA7 Cross-Support
If your database needs to run on both older 32-bit versions (VBA6) and newer 64-bit versions (VBA7) of Office, use conditional compilation blocks.
Looking for a Lightweight Alternative to Microsoft Office?
While Microsoft Access is a powerful database tool, handling complex VBA API compatibility issues during upgrades can be frustrating. If your primary work involves documents, spreadsheets, and presentations, WPS Office offers a free, highly compatible, and lightweight alternative with robust macro support.
- 1. Download WPS Office: Visit the official WPS website and download the free installation package for your operating system.
- 2. Install and Launch: Run the installer, follow the simple setup instructions, and launch the unified WPS workspace.
- 3. Open Your Office Files: Directly open your existing DOCX, XLSX, and PPTX files to continue working seamlessly without format conversion.

Frequently Asked Questions
What does the PtrSafe keyword do in VBA?
The PtrSafe keyword asserts to the compiler that a Declare statement is safe to run in 64-bit versions of Office. It is required in VBA7 to indicate that the macro correctly handles 64-bit pointers and memory addresses.
When should I use LongPtr instead of Long?
You should use LongPtr for any parameters or return values that represent memory pointers or window handles (such as hwnd). In 32-bit Office environments, LongPtr resolves to a 32-bit Long, while in 64-bit Office, it dynamically resolves to a 64-bit LongLong, preventing memory overflow errors.
Do I always need conditional compilation (#If VBA7)?
No, you only need conditional compilation if your database must be used by users on older versions of Office that do not support VBA7 (Office 2007 and earlier). If all users are on Office 2010 or newer, you can simply use the updated PtrSafe declaration without the conditional blocks.




