Results 1 to 12 of 12
  1. #1
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    340

    File Exists Function fails

    Code:
    Function fileExists(s_path As String) As Boolean
        Dim obj_fso As Object
        Set obj_fso = CreateObject("Scripting.FileSystemObject")
        fileExists = obj_fso.fileExists(s_path)
        Set obj_fso = Nothing
    End Function
    I've just found if file s_path exists in C:\Users\Test\Documents the function returns True.


    Is it known why that is ?
    How might I best correct it so if no Path is passed in it will return False? Thanks

  2. #2
    madpiet is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    122
    Don't think I've ever done it that way. I've always done something like

    (DIR(s_Path) = "")

  3. #3
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,643
    You should probably change your function name as it is the same as the fso method.

    Not sure I get your question. If the path is to a valid file the fso.fileexists method will return true. If not it will return false. I don't think it even has to be a path. You can pass "xyz" to it and it will return false.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  4. #4
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    340
    Are you saying obj_fso.fileExists should return true if s_path Exists?
    This isn't what was intended. It should only return true if s_path (including a filename) exists.
    This is normally OK, but in one instance s_path contained the file name with no path, and it returned true.
    It defaulted to "C:\Users\Test\Documents" which by coincidence did contain that file.
    So my question was why did that happen, should it not return false as the full filespec wasn't given?
    And why that path? LOL so many questions...
    I can ensure s_path must contain ":" but wonder if that's the right approach.


  5. #5
    madpiet is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    122
    Quote Originally Posted by madpiet View Post
    Don't think I've ever done it that way. I've always done something like

    (DIR(s_Path) = "")
    Sorry... there's the corrected code.

    Code:
    Option Compare Database
    Option Explicit
    
    
    Public Function FileExists(ByVal strFilePath As String) As Boolean
    
    
        FileExists = Len(Dir(strFilePath)) > 0
    
    
    End Function

  6. #6
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,643
    filespec Required. The name of the file whose existence is to be determined. A complete path specification (either absolute or relative) must be provided if the file isn't expected to exist in the current folder.
    If I had to guess it may be related to "if the file isn't expected to exist in the current folder."
    I've never had or heard of your issue so really can't tell you the why.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  7. #7
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,397
    if i'm reading your question correctly, you need to test s_path for zls

    Code:
    Function fileExists(s_path As String) As Boolean
        Dim obj_fso As Object
        if s<>"" then 
            Set obj_fso = CreateObject("Scripting.FileSystemObject")
            fileExists = obj_fso.fileExists(s_path)
            Set obj_fso = Nothing
        end if
    End Function
    edit - although I use something similar to madpiet's suggestion in post #5

  8. #8
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,643
    Using a ZLS will still return a false.
    Code:
    MsgBox fso.FolderExists("")
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    I don't understand OP's original question. I tested function. If path does not include a filename, I get False. Same for empty string.
    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
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,643
    I think what he's trying to say is:

    he has a file at "C:\Users\Test\Documents\SomeFile.xyz"

    when he runs fso.FileExists(
    "Somefile.xyz") he is getting true when he should be getting false because it is not a full path.


    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    Okay, my guess is FileDialog searches Documents by default.

    FileDialog is an object with properties - InitialFileName is one. In my test, FileDialog seems to retain last folder location searched as a starting point. With that in mind, I tested function again and I get True when only filename is input for file in that last filepath search.
    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.

  12. #12
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,643
    Quote Originally Posted by June7 View Post
    Okay, my guess is FileDialog searches Documents by default.

    FileDialog is an object with properties - InitialFileName is one. In my test, FileDialog seems to retain last folder location searched as a starting point. With that in mind, I tested function again and I get True when only filename is input for file in that last filepath search.
    That's kinda what I was thinking in post#6. The language in the spec definition seems to indicate there's some kind of pointer involved.
    provided if the file isn't expected to exist in the current folder.
    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: 12
    Last Post: 11-02-2022, 02:08 PM
  2. Function To Check If Table Exists
    By chalupabatman in forum Programming
    Replies: 6
    Last Post: 11-16-2017, 01:23 PM
  3. Update query with replace function fails
    By Keven in forum Queries
    Replies: 5
    Last Post: 07-11-2016, 08:59 AM
  4. Replies: 3
    Last Post: 01-08-2013, 11:51 AM
  5. Table exists function in A2007
    By gg80 in forum Programming
    Replies: 8
    Last Post: 09-04-2010, 01:35 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