Results 1 to 9 of 9
  1. #1
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    110

    Question Folder remains "locked" after FileDialog is closed

    Hello Brains Trust

    I'm looking for a pointer to help resolve this issue.



    I have a FileDialog on a form that is used to retrieve the location (path) of an image. The image path location is then stored in a table and it all works very nicely.

    I've discovered that when I close the FileDialog, the folder from which the image was chosen remains "locked" and Windows will not allow any folder operations on that folder until Access is closed (or I open a new FileDialog and navigate away from the folder. Then the problem moves to the new folder).

    I have done some digging around and come across the FileDialog.RestoreDirectory property, however it doesn't seem to be available in Access. Or maybe I'm not using it correctly.

    Does anyone know how I might resolve this? Code below for reference.

    Thank you in advance.

    Code:
    Private Sub Image1_Place_Holder_Click()
     Dim fd As FileDialog
     Dim InitialFileName As String
    
     If IsNull(Me.txtImage1) Then
        InitialFileName = Environ$("USERPROFILE")
        Else
        InitialFileName = Me.txtImage1
     End If
     Set fd = Application.FileDialog(msoFileDialogFilePicker)
           With fd
               .AllowMultiSelect = False
               .Title = "Please select image to add."
               .Filters.Clear
               .Filters.Add "JPEG Files", "*.jpg"
               .InitialFileName = InitialFileName
               If .Show = True Then      'if OK is pressed
                  txtImage1 = .SelectedItems(1) 'write the selected filename in the textbox
               Else
                  Exit Sub  'click Cancel or X box to close the dialog
               End If
           End With
    Set fd = Nothing
    End Sub

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    10,642
    Not experienced this problem but note your Set fd = Nothing line won't execute if you have clicked the cancel or X button although it should go out of scope when exiting the sub anyway. Perhaps comment out the Else and Exit sub lines to see if it makes a difference

  3. #3
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    110
    Hi CJ

    Thanks for the reply. I tried as you suggested, but still have the same issue. For a bit more context I've included a partial screenshot of the form in question. The form shows the image that has been selected in a box (Image Control type) on the form (here there is only 1 image selected). The box is bound to a field in a table. The file path to the image is displayed in a text box under the image, which is also bound to the same field in the table.

    I also have an OnClick event on the Image box that invokes the file dialog, so it basically acts like a button. I can't see any reason why this should make any difference.

    I'm not sure if this is of use at all, and appreciate your time.

    Click image for larger version. 

Name:	Screenshot 2023-01-25 111351.jpg 
Views:	17 
Size:	22.7 KB 
ID:	49540

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    10,642
    I don’t think that would make a difference

    Have you tried the usual fallbacks- compact/repair, decompile/recompile and reboot?

  5. #5
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    110
    HJi CJ. Yep, tried all of that.

  6. #6
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    10,642
    I'm out ideas - did you try the suggestion in post #2?

    Only other thing I can suggest is upload a db to demonstrate the problem

  7. #7
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    110
    Quote Originally Posted by CJ_London View Post
    I'm out ideas - did you try the suggestion in post #2?

    Only other thing I can suggest is upload a db to demonstrate the problem

    Hi CJ. Yes, I tried everything you suggested.

    I'm going to create a simple DB (not split) and see if I can replicate the issue.

    Once again, appreciate your help.

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    3,320
    Not the code, it works fine on my PC with a few alterations

    Code:
    Private Sub Text34_DblClick(Cancel As Integer)
    'Me.Text34 = Time()
    Dim fd As FileDialog
     Dim InitialFileName As String
    
    
     If IsNull(Me.Text34) Then
        InitialFileName = Environ$("USERPROFILE")
        Else
        InitialFileName = Nz(Me.Text34, "F:\Temp\")
     End If
     Set fd = Application.FileDialog(msoFileDialogFilePicker)
           With fd
               .AllowMultiSelect = False
               .Title = "Please select image to add."
               .Filters.Clear
               .Filters.Add "JPEG Files", "*.jpg"
               .InitialFileName = InitialFileName
               If .Show = True Then      'if OK is pressed
                  Me.Text34 = .SelectedItems(1) 'write the selected filename in the textbox
               Else
                  Exit Sub  'click Cancel or X box to close the dialog
               End If
           End With
    Set fd = Nothing
    End Sub
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Posts
    1,280
    I would try making your procedure a public function rather than a sub in a form module. Makes it more portable.

    something along the lines of
    Code:
    Function fBrowse(PickType As MsoFileDialogType, Optional StartFolder As String = "", Optional strTitle As String = "") As String
    
        On Error GoTo fBrowse_Error
    
        Dim mso As Object
        Dim varFile As Variant
    
        If Nz(strTitle, "") = "" Then
    
            Select Case PickType
    
                Case 3
                    strTitle = "Select a File"
                Case 4
                    strTitle = "Select a Folder"
                Case Else
                    strTitle = ""
    
            End Select
    
        Else: strTitle = strTitle
    
        End If
    
        Set mso = Application.FileDialog(PickType)
    
        With mso
    
            .Title = strTitle
            .AllowMultiSelect = False
            .InitialFileName = StartFolder
            .Filters.Clear
    
            If PickType = msoFileDialogFilePicker Then
            
                .Filters.Add "All Files", "*.*"
                .Filters.Add "PDF file", "*.pdf"
                .Filters.Add "Image Files", "*.jpg,*.jpeg,*.BMP,*.Png,*.TIFF,"
                .Filters.Add "Audio Files", "*.Wav,*.mp4"
    
            End If
            
            If .Show = True Then
    
                If .SelectedItems.Count = 0 Then
    
                    fBrowse = ""
                End If
    
                For Each varFile In .SelectedItems
                    fBrowse = varFile
                Next
    
            Else
    
                fBrowse = ""
    
            End If
    
        End With
    
        On Error GoTo 0
        Exit Function
    
    fBrowse_Error:
    
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure fBrowse, line " & Erl & "."
    
    End Function
    In your click event you would then use

    Code:
    Me.text34 = fbrowse(msoFileDialogFilePicker)
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 28
    Last Post: 03-23-2021, 11:02 AM
  2. Replies: 3
    Last Post: 02-13-2019, 01:49 AM
  3. Replies: 6
    Last Post: 09-04-2014, 02:03 PM
  4. Replies: 1
    Last Post: 10-26-2012, 12:52 PM
  5. Replies: 3
    Last Post: 08-12-2012, 11:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums