GetOpenFileName(OpenFile) not working (2 Viewers)

andy1968

Registered User.
Local time
Today, 02:28
Joined
May 9, 2018
Messages
131
I have code to add hyperlinks for files to a text field on a form.


The code has worked for years, but suddenly stopped working on one users computer.


The code uses


Code:
 GetOpenFileName(OpenFile)
to open a directory to get the link to the document.


The machine in question is running Windows 10 - 64 bit. So are the other machines running the code.
 

Micron

AWF VIP
Local time
Today, 05:28
Joined
Oct 20, 2018
Messages
3,476
Someone correct me if I'm wrong, but isn't GetOpenFileName an Excel method? It seems that a lot of stuff that used to work no longer does with Access 2016 and up. It seems to be more particular about syntax. Given that this is a one pc issue, I'd suggest checking for missing or broken references on that pc. However, I'd do away with trying to use Excel functions from within Access if this is the case here, because you can use the msoFileDialogFolderPicker for this instead.
 

andy1968

Registered User.
Local time
Today, 02:28
Joined
May 9, 2018
Messages
131
Thanks Micron.


I'm not familiar with msoFileDialogFolderPicker but will look it up. Is there a way to direct this to a certain directory?
 

Micron

AWF VIP
Local time
Today, 05:28
Joined
Oct 20, 2018
Messages
3,476
Pretty sure one of it's properties is a starting directory. My recollection is that I once used it along with a user table field with exactly that, so it was specific to a user. Be aware that there is a folder picker and a file picker. One thing I'm not really fond of is that the folder picker dialog doesn't make it obvious to the user that they cannot pick a file, but you can also modify the dialog title bar to help with that. Then again, how often do people pay attention to title bars?
 

isladogs

MVP / VIP
Local time
Today, 09:28
Joined
Jan 14, 2017
Messages
18,186
Pardon me for jumping in

I think the issue is with 64-bit Access rather than 64-bit Windows
I stopped using GetOpenFileName & GetSaveFileName almost 5 years ago as despite converting the APIs, I could never get them to work properly in 64-bit Access.
I know that many others also had problems but unsure whether its solvable

However, as mentioned by Micron, FileDialog does the same job and works perfectly in both bitnesses.

Here is some example code for picking a file or a folder:

Code:
Private Sub cmdBrowse_Click()
    
On Error GoTo Err_cmdBrowse_Click

   ' Set options for the dialog box.
    Dim F As FileDialog
    Set F = Application.FileDialog(msoFileDialogFilePicker)
    F.Title = "Locate the Access database file and click on 'Open'"
    F.AllowMultiSelect = False
    
   ' Clear out the current filters, and add our own.
      F.Filters.Clear
      F.Filters.Add "Access Databases", "*.accdb;*.mdb"

   ' Set the start folder (modify as necessary)
    F.InitialFileName = "c:\"
    
' Call the Open dialog routine.
    F.Show

' Return the path and file name.
    strFilename = F.SelectedItems(1)
    
 'populate the form control
    Me.txtFile = strFilename

Exit_cmdBrowse_Click:
    Exit Sub

Err_cmdBrowse_Click:
    'err=5, user clicked cancel
    If Err.Number = 5 Then Exit Sub
        
    MsgBox "Error " & err.Number & " in cmdBrowse_Click procedure : " & err.Description
    
    Resume Exit_cmdBrowse_Click
    
End Sub

Code:
Function GetFolder(strPath As String) As String
    Dim fldr As FileDialog
    Dim sItem As String
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = strPath
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With
NextCode:
    GetFolder = sItem
    Set fldr = Nothing
End Function

Hope it helps
 
Last edited:

andy1968

Registered User.
Local time
Today, 02:28
Joined
May 9, 2018
Messages
131
I can figure out how to apply msoFileDialogFolderPicker in the code.


Here is the complete code I am using. This is part of a code to add a hyperlink to a text box on a form.


Code:
Public Function GetOpenFileNameGs( _
                            objForm As Form, _
                            Optional sInitialDir As String, _
                            Optional lFlag As Long = 0, _
                            Optional sFileFilter As String = mcsDefaultFileFilter _
) As String

If CurrentProject.AllForms("frmToDoListEntry").IsLoaded Then
    sInitialDir = Forms!frmTodoListEntry!frmToDoListEntryLinks.Form.filelocation
ElseIf CurrentProject.AllForms("frmSubmittalEntry").IsLoaded Then
    sInitialDir = Forms!frmSubmittalEntry!frmToDoListEntryLinks.Form.filelocation & "\Submittals"
ElseIf CurrentProject.AllForms("frmRFIInput").IsLoaded Then
    sInitialDir = Forms!frmRFIInput.Form.filelocation & "\RFI"
ElseIf CurrentProject.AllForms("frmDailyReportReviewInput").IsLoaded Then
    sInitialDir = "X:\MPOC\Daily Reports_Inspections Reports"
Else

  sInitialDir = "C:\"
End If
    
    Dim OpenFile As OPENFILENAME
    Dim lReturn As Long
    
    OpenFile.lStructSize = Len(OpenFile)
    OpenFile.hWndOwner = objForm.Hwnd
    OpenFile.hInstance = Application.hWndAccessApp
    OpenFile.lpstrFilter = ReplaceG(sFileFilter, "|", vbNullChar)
    OpenFile.nFilterIndex = 1
    OpenFile.lpstrFile = String(257, 0)
    OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
    OpenFile.lpstrFileTitle = OpenFile.lpstrFile
    OpenFile.nMaxFileTitle = OpenFile.nMaxFile
    OpenFile.lpstrInitialDir = sInitialDir
    OpenFile.lpstrTitle = "Open File"
    OpenFile.flags = lFlag
    
    lReturn = GetOpenFileName(OpenFile)
    If lReturn = 0 Then
       GetOpenFileNameGs = ""
    Else
       GetOpenFileNameGs = CleanStringMs(OpenFile.lpstrFile & "")
    End If
End Function
 

andy1968

Registered User.
Local time
Today, 02:28
Joined
May 9, 2018
Messages
131
isladogs Just saw your post - I'll pay around with this and see if I can get this going.


Thanks for your help.
 

isladogs

MVP / VIP
Local time
Today, 09:28
Joined
Jan 14, 2017
Messages
18,186
I was just in the process of replying saying you don't need all that code.
Also I'm fairly sure it wouldn't work in 64-bit Access as written anyway.

I've included similar code in the attached app which is meant for use when attaching files to emails sent using CDO from Access
 

Attachments

  • Browse&AttachFile.zip
    38.6 KB · Views: 411
Last edited:

andy1968

Registered User.
Local time
Today, 02:28
Joined
May 9, 2018
Messages
131
Got it to work. Thanks isladogs.


Needed to add a reference to Microsoft Office 16.0 Object Library.


Also, why do you have "F.show" two times in your code? I removed the first instance.
 

isladogs

MVP / VIP
Local time
Today, 09:28
Joined
Jan 14, 2017
Messages
18,186
Got it to work. Thanks isladogs.

Needed to add a reference to Microsoft Office 16.0 Object Library.

Also, why do you have "F.show" two times in your code? I removed the first instance.

Well spotted. I had merged two different code snippets and left the duplicate line in error. I've now corrected that code. Thanks
You were right to remove the first one.
 
Last edited:

Users who are viewing this thread

Top Bottom