2

I'm using VBA in MS Access, and one of the subs takes a file path in a network, checks if the file exists or not, and write the result of a query on it. The problem is that when I try to run the code, it gives me error 52 (Bad file name or number). But if I open the network path in windows explorer first, for example, after that the error doesn't happen anymore. Any ideas on what the problem might be?

Here is some of the code I'm running:

fpath = "\\networkpath\file.txt"
DeleteFile fpath

Sub DeleteFile(ByVal FileToDelete As String)
    FileExists(FileToDelete) Then
        SetAttr FileToDelete, vbNormal
        FileToDelete
    End If
End Sub

Function FileExists(ByVal FileToTest As String) As Boolean
    FileExists = (Dir(FileToTest) <> "") 'this is where the error happens
End Function
4
  • 1
    If it prompts error while running it without manually accessing the folder first, chances are there is permission issue accessing the folder Commented Dec 17, 2014 at 18:23
  • 1
    See if you can run with this: allenbrowne.com/func-11.html .. Play around with the file path as well. Commented Dec 17, 2014 at 18:24
  • 1
    Is this a WebDAV path ? I've seen this behavior with that type of path, but don't have a clean solution. You could try opening Explorer at that path via VBA before checking to see if the file exists. Commented Dec 17, 2014 at 18:31
  • Had no luck using the code in that link, so I had to resort to opening explorer, like Tim suggested, which works Commented Dec 17, 2014 at 19:20

1 Answer 1

2

Does the UNC path you use contain any non-Ascii characters, like accents? What is the exact path?

None of the file functions in VBA work well with Unicode anyway.

You could try to use the FileSystemObject to achieve the same a bit more reliably than the build-in VBA functions:

Public Function FileExists(filePath as string) as Boolean
    Dim o As Object
    Set o = CreateObject("Scripting.FileSystemObject") 
    FileExists = o.FileExists(filePath) 
End Function

An alternative using the Win32 API tha works in 32 and 64 bit environments:

Private Const INVALID_FILE_ATTRIBUTES As Long = -1

#If VBA7 Then ' Win API Declarations for 32 and 64 bit versions of Office 2010 and later
  Private Declare PtrSafe Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesW" (ByVal lpFileName As LongPtr) As Long
#Else ' WIN API Declarations for Office 2007
  Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesW" (ByVal lpFileName As Long) As Long
#End If

Public Function FileExists(fname As Variant) As Boolean
    If IsNull(fname) Or IsEmpty(fname) Then Exit Function
    ' Make sure that we can take care of paths longer than 260 characters
    If Left$(fname, 2) = "\\" Then
        FileExists = GetFileAttributes(StrPtr("\\?\UNC" & Mid$(fname, 2))) <> INVALID_FILE_ATTRIBUTES
    Else
        FileExists = GetFileAttributes(StrPtr("\\?\" & fname)) <> INVALID_FILE_ATTRIBUTES
    End If
End Function 
Sign up to request clarification or add additional context in comments.

1 Comment

sadly, it doesn't work. the problem, apparently, is something with permissions. +1 though for the tip and code for file reading

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.