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

Posted on 2012-09-13
1 Ratings
Last Modified: 2012-09-28
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
Question by:edrz01
    LVL 6

    Expert Comment

    runs for me and I am running 64 bit...
    LVL 6

    Expert Comment

    what is you error message...
    LVL 3

    Expert Comment

    by:Oliver Wastell
    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
    LVL 3

    Expert Comment

    by:Oliver Wastell
    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.
    LVL 56

    Expert Comment

    by:Jim Dettman (Microsoft MVP/ EE MVE)
    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:

    LVL 3

    Accepted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
    In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
    In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
    The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …

    779 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

    9 Experts available now in Live!

    Get 1:1 Help Now