• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2179
  • Last Modified:

Need assistance in determining why GetOpenFileName Lib runs in Access 2010 32-bit but will not run in 64-bit

I have a Declare Function GetOpenFileName Lib "comdlg32.dll" Alias that runs in Access 2010 32-bit but will not run in 64-bit. I have registered the comdlg32.ocx from the SysWOW64 folder and that folder does have the comdlg32.dll in it.

I need to make sure the function runs in both 32-bit and 64-bit.

The primary Form uses the following command button code to find the Declare Function in a module:
'''''' Open Command Dialog Control to find Path
Private Sub cmdBrowse_Click()
 txtFilenamePath1 = OpenLoc1(Me)
    If txtOpenFileName > "" Then
       Me.cmdImpEvtSprdsht.Enabled = True
       Me.cmdImpEvtSprdsht.Enabled = False
    End If
End Sub

Below is the entire modCmdBtn1 module code:
Option Compare Database

Dim strtxtOpenFileName As String

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long

    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    Flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Function OpenLoc1(strForm As Form) As String
'Dim strtxtOpenFileName As String
    Dim OpenFile As OPENFILENAME
    Dim lReturn As Long
    Dim sFilter As String
    OpenFile.lStructSize = Len(OpenFile)
    OpenFile.hwndOwner = strForm.hwnd
    'sFilter = "All Files (*.*)" & Chr(0) & "*.*" & Chr(0) & _
      "JPEG Files (*.JPG)" & Chr(0) & "*.JPG" & Chr(0)
    sFilter = "Excel Files (*.csv)" & Chr(0) & "*.csv"
    OpenFile.lpstrFilter = sFilter
    OpenFile.nFilterIndex = 1
    OpenFile.lpstrFile = String(257, 0)
    OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
    OpenFile.lpstrFileTitle = OpenFile.lpstrFile
    OpenFile.nMaxFileTitle = OpenFile.nMaxFile
    ''''OpenFile.lpstrInitialDir = "C:\"
    OpenFile.lpstrTitle = "Select a file to LINK"
    OpenFile.Flags = 0
    lReturn = GetOpenFileName(OpenFile)
        If lReturn = 0 Then
            MsgBox "A file was not selected!", vbInformation, _
              "Select a file to Link"
            OpenLoc1 = Trim(Left(OpenFile.lpstrFile, InStr(1, OpenFile.lpstrFile, vbNullChar) - 1))
            'MsgBox OpenFile.lpstrFileTitle
            strtxtOpenFileName = OpenFile.lpstrFileTitle
            Forms.frmdataimport.txtOpenFileName = strtxtOpenFileName
         End If
End Function
  • 3
  • 2
1 Solution
runs for me and I am running 64 bit...
what is you error message...
Oliver WastellMemberCommented:
I found the following article useful when I had a similar issue:

fhlio_admin - you said that you're running 64bit and it works for you.  Is that 64bit Windows but 32bit Office?  I would expect that two Declare statements will be required, one for 32bit Office and another for 64bit Office, i.e.:

32bit Office
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long

64bit Office
Private Declare PtrSafe Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As LongPtr

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Oliver WastellMemberCommented:
OK, I've had a further look...

I believe that this should do the trick:

#If VBA7 Then
    Private Declare PtrSafe Function GetOpenFileName Lib "comdlg32.dll" Alias _
    "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As LongPtr
    Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
    "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
#End If

In the function OpenLoc1 you will need to change the variable declaration lReturn to variant to avoid a data type mismatch when running under 64bit office.
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
Note that you only need to use compiler directives if you want your DB to run in both 32 and 64 bit Office.

Besides the link above, some other helpful links:

All the new 64 bit calls:


There is also a code inspector for 64 bit:

Microsoft Office Code Compatibility Inspector user's guide

and here's a list of all the calls that were modified for 64 bit:

Oliver WastellMemberCommented:
Although I can get the code to compile in both Access 2007 (32 bit) and Access 2010 (64bit), it still doesn't work in Access 2010 (64 bit).  The API call is working, but it is returning the message 'A file was not selected!' without opening the dialogue box to allow you to select the file in the first place.  

The following article suggests using FileDialogue which is built into Access (i.e. no external dependencies) and works with all versions of Access from 2003 including the latest 64bit version:

The article also makes the following point:
But you should really ask why you're using 64-bit Access. It's really not at all recommended by MS that anyone use 64-bit Office unless they have specific reasons why they need it (such as needing to use the extra memory it provides, particularly for things like complex Excel spreadsheet models). Access is definitely not one of the apps that benefits much from the conversion to 64-bit.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now