Solved

Ms ACCESS 2010 - code updated for 64-bit machine and then mark with the PtrSafe attribite

Posted on 2013-06-10
13
2,210 Views
Last Modified: 2013-07-01
I tried running an access 2010 db and encounter this error in the code:

This code in this project must be updated for use on 64-bit systems.  Please
review and update Delcare statements and then mark with the PtrSafe attribite

What do I need to do to prevent this error.

Option Compare Database
Option Explicit

'***************** Code Start **************
' This code was originally written by Ken Getz.
' It is not to be altered or distributed, 'except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code originally courtesy of:
' Microsoft Access 95 How-To
' Ken Getz and Paul Litwin
' Waite Group Press, 1996
' Revised to support multiple files:
' 28 December 2007

Type tagOPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    strFilter As String
    strCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    strFile As String
    nMaxFile As Long
    strFileTitle As String
    nMaxFileTitle As Long
    strInitialDir As String
    strTitle As String
    Flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    strDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Declare Function aht_apiGetOpenFileName Lib "comdlg32.dll" _
    Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean

Declare Function aht_apiGetSaveFileName Lib "comdlg32.dll" _
    Alias "GetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean
Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long

Global Const ahtOFN_READONLY = &H1
Global Const ahtOFN_OVERWRITEPROMPT = &H2
Global Const ahtOFN_HIDEREADONLY = &H4
Global Const ahtOFN_NOCHANGEDIR = &H8
Global Const ahtOFN_SHOWHELP = &H10
' You won't use these.
'Global Const ahtOFN_ENABLEHOOK = &H20
'Global Const ahtOFN_ENABLETEMPLATE = &H40
'Global Const ahtOFN_ENABLETEMPLATEHANDLE = &H80
Global Const ahtOFN_NOVALIDATE = &H100
Global Const ahtOFN_ALLOWMULTISELECT = &H200
Global Const ahtOFN_EXTENSIONDIFFERENT = &H400
Global Const ahtOFN_PATHMUSTEXIST = &H800
Global Const ahtOFN_FILEMUSTEXIST = &H1000
Global Const ahtOFN_CREATEPROMPT = &H2000
Global Const ahtOFN_SHAREAWARE = &H4000
Global Const ahtOFN_NOREADONLYRETURN = &H8000
Global Const ahtOFN_NOTESTFILECREATE = &H10000
Global Const ahtOFN_NONETWORKBUTTON = &H20000
Global Const ahtOFN_NOLONGNAMES = &H40000
' New for Windows 95
Global Const ahtOFN_EXPLORER = &H80000
Global Const ahtOFN_NODEREFERENCELINKS = &H100000
Global Const ahtOFN_LONGNAMES = &H200000
0
Comment
Question by:cookiejar
  • 4
  • 3
  • 3
  • +1
13 Comments
 
LVL 21

Accepted Solution

by:
Boyd (HiTechCoach) Trimmell, Microsoft Access MVP earned 500 total points
ID: 39236338
The code will work fine on Windows 7 64 bit with Access 210 #2-bit.

Are you using only the the Access 2010 64-bit version?


64 bit version with PtrSafe Qualifier, LongPtr 64 bit Pointer and LongLong 64 bit datatype

    Option Compare Database
    Option Explicit


    'This code was originally written by Ken Getz.
    ' It is not to be altered or distributed, 'except as part of an application.
    ' You are free to use it in any application,
    ' provided the copyright notice is left unchanged.
    '
    ' Code originally courtesy of:
    ' Microsoft Access 95 How-To
    ' Ken Getz and Paul Litwin
    ' Waite Group Press, 1996
    ' Revised to support multiple files:
    ' 28 December 2007
    ' ptrSafe added March, 2010
    ' LongPtr added March, 2010

    Type tagOPENFILENAME
    lStructSize As Long
    hwndOwner As LongPtr
    hInstance As LongPtr
    strFilter As String
    strCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    strFile As String
    nMaxFile As Long
    strFileTitle As String
    nMaxFileTitle As Long
    strInitialDir As String
    strTitle As String
    Flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    strDefExt As String
    lCustData As Long
    lpfnHook As LongPtr
    lpTemplateName As String
    End Type

    Private Declare PtrSafe Function aht_apiGetOpenFileName Lib "comdlg32.dll" _
    Alias "aht_apiGetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean

    Private Declare PtrSafe Function aht_apiGetSaveFileName Lib "comdlg32.dll" _
    Alias "aht_apiGetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean

Open in new window


also:

You will also need to find the place in the code where that call was made, converted it to LenB() and it should back in business.

    With OFN
    .lStructSize = LenB(OFN)
    .hwndOwner = Hwnd
    .strFilter = Filter

    ...

    End With

Open in new window



See:
http://gpgonaccess.blogspot.com/2010/03/work-in-progress-and-64-bit-vba.html

Compatibility Between the 32-bit and 64-bit Versions of Office 2010
0
 
LVL 12

Expert Comment

by:pdebaets
ID: 39236390
You can also use the "VBA7" compiler directive constant to run compatible code in all versions of Access.

#if VBA7 then
    '* Put your Access 2010 (and later) 64-bit code in here. Ex.:
    Dim lngMyVar as LongPtr
#else
    '* Put your 32-bit code in here. Ex.:
    Dim lngMyVar as Long
#end if

Open in new window

0
 

Author Comment

by:cookiejar
ID: 39236497
TheHiTechCoach.
I modified the code to reflect your recommendation.

I am getting this error:
Can't Find DLL entry point aht_apiGetOpenFileNameA in comdlg32.dll(453)

Option Compare Database
Option Explicit


    'This code was originally written by Ken Getz.
    ' It is not to be altered or distributed, 'except as part of an application.
    ' You are free to use it in any application,
    ' provided the copyright notice is left unchanged.
    '
    ' Code originally courtesy of:
    ' Microsoft Access 95 How-To
    ' Ken Getz and Paul Litwin
    ' Waite Group Press, 1996
    ' Revised to support multiple files:
    ' 28 December 2007
    ' ptrSafe added March, 2010
    ' LongPtr added March, 2010

    Type tagOPENFILENAME
    lStructSize As Long
    hwndOwner As LongPtr
    hInstance As LongPtr
    strFilter As String
    strCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    strFile As String
    nMaxFile As Long
    strFileTitle As String
    nMaxFileTitle As Long
    strInitialDir As String
    strTitle As String
    Flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    strDefExt As String
    lCustData As Long
    lpfnHook As LongPtr
    lpTemplateName As String
    End Type

    Private Declare PtrSafe Function aht_apiGetOpenFileName Lib "comdlg32.dll" _
    Alias "aht_apiGetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean

    Private Declare PtrSafe Function aht_apiGetSaveFileName Lib "comdlg32.dll" _
    Alias "aht_apiGetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean
0
 
LVL 57
ID: 39237792
Would *strongly* suggest that unless 64 bit Office is an absolute requirement for you, that you go back to 32 bit's.

Even today, there are still very few non-native controls available for 64 bit that will work with Access.

Also, when converting to 64 bit, you have two jobs:

1. Converting calls with PtrSafe

2. And modifying API calls to 64 bit where the API call actually changes for 64 bit

  These are not one in the same tasks.

  You'll need to use both the VBA7 and WIN64 compiler directives if you want your app to work in both 32 and 64 bit Office installs.

   If you don't care about that, then simply forget about the compiler directives and convert everything to 64 bit.

 Someone already posted the first link below, which you really need to read through carefully.  Make sure you read the section ""Introducing the VBA 7 Code Base"
.  There are others which will help as well.

Jim.

Compatibility Between the 32-bit and 64-bit Versions of Office 2010
http://msdn.microsoft.com/en-us/library/ee691831(office.14).aspx

All the new 64 bit calls:
http://www.microsoft.com/download/en/confirmation.aspx?displaylang=en&id=9970

Code inspector for 64 bit:
Microsoft Office Code Compatibility Inspector user's guide
http://technet.microsoft.com/en-us/library/ee833946.aspx

All calls that were modified for 64 bit:
http://msdn.microsoft.com/en-us/library/aa383663(VS.85).aspx
0
 

Author Comment

by:cookiejar
ID: 39238386
Would *strongly* suggest that unless 64 bit Office is an absolute requirement for you, that you go back to 32 bit's.

I went back to the 32 bit machine code as shown in my post but how do I prevent this error using the code I currently have?
This code in this project must be updated for use on 64-bit systems.  Please
review and update Delclare statements and then mark with the PtrSafe attribite
0
 
LVL 57
ID: 39238506
<<I went back to the 32 bit machine code as shown in my post but how do I prevent this error using the code I currently have?
This code in this project must be updated for use on 64-bit systems.  >>

  Keep in mind that it is the version of Office that determines how your code needs to be written.

 You can run Office 32 bit under a 64 bit OS without issue or changes.

 If you want to stick with 64 bit Office, here's a link with a set of all the calls for 64 bit:

http://www.jkp-ads.com/Articles/apideclarations.asp

 really didn't look through what the others have posted, so this may be some duplication.

Jim.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 12

Expert Comment

by:pdebaets
ID: 39238630
"...how do I prevent this error using the code I currently have?"

You can also use the "VBA7" compiler directive constant to run compatible code in all versions of Access.

#if VBA7 then
    '* Put your Access 2010 (and later) 64-bit code in here. Ex.:
    Dim lngMyVar as LongPtr
#else
    '* Put your 32-bit code in here. Ex.:
    Dim lngMyVar as Long
#end if

Open in new window

0
 
LVL 21
ID: 39238965
I agree with pdebaets. Use the "VBA7" compiler directive.  This way you only have a single front end to maintain.

Note: If you will be compiling into a .accde you will have to compile separate a .accde for each version. . You will need both   Access 32 bit and Access 64 bit installed to create each .accde.
0
 

Author Comment

by:cookiejar
ID: 39242153
I used the following method so that it could run on both 32-bit, 64-bit platforms.  However, when compiling on a 64-bit machine, the statements after #else for the 32-bit did not compile. I had to comment out those statements
#if VBA7 then
    '* Put your Access 2010 (and later) 64-bit code in here. Ex.:
    Dim lngMyVar as LongPtr
#else
    '* Put your 32-bit code in here. Ex.:
    Dim lngMyVar as Long
#end if
0
 
LVL 57
ID: 39242337
What you have is not correct for accounting between 32 and 64 bit.

VBA7 tells you simply that it is running in A2010.

WIN64 is true if the code is running in 64 bits.

So you can have

VBA7     WIN64
False     False   - Something other then 2010.
True      False   - 2010 running in 32 bit
True      True    - 2010 running in 64 bit.

Jim.
0
 
LVL 21
ID: 39242423
However, when compiling on a 64-bit machine, ...
Did you mean compiling with 64-bit Access?
0
 
LVL 12

Expert Comment

by:pdebaets
ID: 39242567
Are you saying that

Dim lngMyVar as Long

...did not compile?
0
 
LVL 12

Expert Comment

by:pdebaets
ID: 39242592
Jim,

I don't think he's running into any issues with the LongLong data type, or API calls that have otherwise changed in 64-bit. For simplicity, the VBA7 compiler directive constant should work for his needs. The data type "LongPtr" works in both Access 2010 32-bit and Access 2010 64-bit.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now