VBA & Macro Problems

How to Fix VBA Declare Errors in 64-Bit Microsoft Access

Anum MushtaqAnum Mushtaq Oct 29, 2026 870 views

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.

How to Fix VBA Declare Errors in 64-Bit Microsoft Access
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 you start

Before modifying your code, open the VBA Editor in Access and use the search function to locate all modules containing Windows API 'Declare' statements.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 while in your Microsoft Access database to open the Visual Basic for Applications (VBA) editor.

2
Locate Declare Statements

Search your code modules for the word 'Declare'. Find the statements defining the external Windows API functions.

3
Add the PtrSafe Keyword

Insert 'PtrSafe' immediately after the 'Declare' keyword. For example, change 'Declare Function SetCursorPos...' to 'Declare PtrSafe Function SetCursorPos...'.

4
Update Pointer Data Types to LongPtr

Identify any parameters or return values that handle memory addresses or window handles (such as 'hwnd'). Change their data type from 'Long' to 'LongPtr'.

Update Windows API Declarations with PtrSafe and LongPtr
Compilation Check: After updating the declarations, click 'Debug' > 'Compile Database' in the top menu to ensure all 64-bit compatibility errors have been resolved.
Free Microsoft Office alternative

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. 1. Download WPS Office: Visit the official WPS website and download the free installation package for your operating system.
  2. 2. Install and Launch: Run the installer, follow the simple setup instructions, and launch the unified WPS workspace.
  3. 3. Open Your Office Files: Directly open your existing DOCX, XLSX, and PPTX files to continue working seamlessly without format conversion.
Free to use with a familiar, intuitive interface similar to Microsoft Office.Excellent format compatibility with Microsoft Word, Excel, and PowerPoint files.Advanced spreadsheet capabilities with robust built-in VBA and macro support.Lightweight installation that runs smoothly without demanding extensive system resources.
microsoft office alternative - wps office

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.