Results 1 to 12 of 12
  1. #1
    KWarzala is offline Advanced Beginner
    Windows XP Access 2010 64bit
    Join Date
    Sep 2009
    Posts
    35

    File dialog Cancel issue

    Hello,

    I have the following code to open a file dialog so a person can select a file:

    Code:
    Dim f As Object
    Set f = Application.FileDialog(3)
    
    f.AllowMultiSelect = False
    
    If f.Show = -1 Then
         For i = 1 To f.SelecteItems.Count
                sFile = Filename(f.SelectedItems(i), sPath)
                Me.txtSelectedFile = sPath & s File
         Next
    Else
    If f.Show = 0 Then
         MsgBox "No file selected"
    End If 
    End If
    The problem i'm having is if i open the file dialog box and select Cancel, it pops the file dialog up again, then this time when i hit Cancel it shows me my MsgBox "No file selected". I know it has something to do with the For/Next statement, but i can't get it to just Cancel and show MsgBox



    I appreciate any help!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Is Filename() a custom function? Where is sPath declared and set?

    I see a typo in f.SelectedItems.Count
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    KWarzala is offline Advanced Beginner
    Windows XP Access 2010 64bit
    Join Date
    Sep 2009
    Posts
    35
    Yes, Filename() is a custom function

    the code works fine i just don't know how to change it to not have it pop up twice

    Code:
    Public Function Filename(ByVal strPath As String, sPath) As String
        sPath = Left(strPath, InStrRev(strPath, "\"))
        Filename = Mid(strPath, InStrRev(strPath, "\") + 1)
    End Function

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Try:
    Code:
    f.AllowMultiSelect = False
    If f.Show = 0 Then
         MsgBox "No file selected"
    Else
         For i = 1 To f.SelectedItems.Count
                sFile = FileName(f.SelectedItems(i), sPath)
                Me.txtSelectedFile = sPath & s File
         Next
    End If
    However, if you don't allow multiple selection, then don't use loop.

    Code:
    f.AllowMultiSelect = False
    If f.Show = 0 Then
        MsgBox "No file selected"
    Else
        sFile = FileName(f.SelectedItems(1), sPath)
        Me.txtSelectedFile = sPath & s File
    End If
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    KWarzala is offline Advanced Beginner
    Windows XP Access 2010 64bit
    Join Date
    Sep 2009
    Posts
    35
    Sorry, didn't see the second part of your post, i will try that code

    thanks

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    It does not for me. But your code is not quite same as mine. And I did not test the Filename() function. Let me try now.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Where is sPath declared and set? It doesn't seem to be used for anything in the Filename() function.

    I still don't get the repeat popup for either code structure.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    KWarzala is offline Advanced Beginner
    Windows XP Access 2010 64bit
    Join Date
    Sep 2009
    Posts
    35
    It no longer pops up twice, but it also no longer sets Me.txtSelectedFile - sPath & sFile

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    It should. I am not testing with textbox, just Debug.Print. The filename is printed to immediate window so I know it is captured and setting value in textbox should work in place of the Debug.Print.

    Have you step debugged? Refer to link at bottom of my post for debugging guidelines.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    KWarzala is offline Advanced Beginner
    Windows XP Access 2010 64bit
    Join Date
    Sep 2009
    Posts
    35
    that was very helpful, thanks for that.

    Now i have a new issue... How do i make it so the person can either double click the file OR click the open button? it doesn't seem to want to let me jump back and forth between those options

  11. #11
    KWarzala is offline Advanced Beginner
    Windows XP Access 2010 64bit
    Join Date
    Sep 2009
    Posts
    35
    it's just my computer being weird, i have the same procedure on another form and it works with either method on that one. Thank you for all your help!

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Not an issue for me. Double click file or single click file then click OK.

    One oops. In the code without loop, change the i variable to 1. I edited earlier post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Dialog windows and file requestor
    By daveinuk in forum Programming
    Replies: 1
    Last Post: 03-06-2014, 07:09 AM
  2. Replies: 1
    Last Post: 08-02-2013, 09:37 AM
  3. Search for associated folders in file dialog box
    By john_billau in forum Programming
    Replies: 1
    Last Post: 01-27-2012, 03:48 PM
  4. Can't cancel the ‘find’ dialog in modal form
    By AndrewAfresh in forum Access
    Replies: 0
    Last Post: 09-23-2011, 11:12 AM
  5. How to get a load file dialog?
    By degras in forum Programming
    Replies: 4
    Last Post: 04-21-2011, 07:45 AM

Tags for this Thread

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