none
Runtime error 52 when using dir function

    Question

  • I'm trying to delete a file in a network location. Sometimes the file is there, sometimes it's not, so I'm using the following code:

    strPath = "\\network drive\My Folder\"
    strFilename = "MyFile.doc"
    If Not Dir(strPath & strFilename) = "" Then Kill strPath & strFilename

    Occasionally, I get a runtime 52 error when the file doesn't exist. I don't understand why I'm getting this when, if the file doesn't exist, the Dir function should return "".

    I searched the forums for Runtime 52, and didn't find anything pertaining to this.

    Thanks in advance for any help.

    Will

    Will Knapp 20+ Year Access VBA Developer


    • Edited by Will Knapp Tuesday, July 02, 2013 3:00 PM Updated Code to reflect current state
    Tuesday, July 02, 2013 2:19 PM

Answers

  • After much trial and error, I was finally able to determine that offending strFilenames (which are derived from Mailitem.ConversationTopics) had one or more tab characters (Ascii 9) which are forbidden when using either the Dir or Kill statements.  I had alread run code to remove any other disallowed characters from filenames, so it took a while to figure out that this was even an option.

    Here's the code I used to fix this by putting spaces in the place of the tab characters:

        strFilename = Replace(strFilename, Chr(9), " ")

    Since then, the code has run smoothly.

    I am still, however, unable to figure out why I couldn't trap the runtime error, but that's not causing as much of a problem anymore.

    Thanks for your help, everyone!


    Will Knapp 20+ Year Access VBA Developer

    Friday, July 05, 2013 3:40 PM

All replies

  • Why do you compare the result of Dir (a string) to 0 (a number)? I get a Type Mismatch error when I try that. I'd use

    If Not Dir(strPath & strFilename) = "" Then Kill strPath & strFilename


    Regards, Hans Vogelaar

    Tuesday, July 02, 2013 2:38 PM
  • My mistake - I cut and pasted mid-edit.  At one point, I had tried

    If len(dir(strpath & strfile) = 0 then...

    because simply using dir(strpath * strfile) would produce the error.  I've tried using "<>" too.  The problem is with the dir function, and it's intermittent.

    If I could just trap the error, I'd be fine.  I've tried On Error Resume Next, but it still calls the error..


    Will Knapp 20+ Year Access VBA Developer

    Tuesday, July 02, 2013 2:59 PM
  • Original post updated to reflect my typo...

    Will Knapp 20+ Year Access VBA Developer

    Tuesday, July 02, 2013 3:01 PM
  • You could bypass the problem as follows:

        On Error Resume Next
        Kill strPath & strFilename
        On Error GoTo 0


    Regards, Hans Vogelaar

    Tuesday, July 02, 2013 3:15 PM
  • You could bypass the problem as follows:

        On Error Resume Next
        Kill strPath & strFilename
        On Error GoTo 0


    Regards, Hans Vogelaar

    I like the looks of that!  I never thought of avoiding the Dir statement that way.  I'll give it a spin...

    Will Knapp 20+ Year Access VBA Developer

    Tuesday, July 02, 2013 3:17 PM
  • I think the Dir is incomplete. Try:

    If len(dir(strpath & strfile, vbNormal) = 0 then...

    Tuesday, July 02, 2013 3:22 PM
  • I think the Dir is incomplete. Try:

    If len(dir(strpath & strfile, vbNormal) = 0 then...

    Isn't vbNormal the default option?

    Also, I'm compelled to point out that when I type the Dir statement using a literal file path and file name into the Immediate window prompt, while the VB is in break mode pondering Runtime Error 52, I do in fact get the empty string, so this seems to be an intermittent thing.  I have trouble recreating it, as it only pops up about once every 100 iterations.


    Will Knapp 20+ Year Access VBA Developer

    Tuesday, July 02, 2013 3:37 PM
  • Another alternative may be using API to delete any files.   I have never had any problem with it.

    Tuesday, July 02, 2013 3:48 PM
  • After much trial and error, I was finally able to determine that offending strFilenames (which are derived from Mailitem.ConversationTopics) had one or more tab characters (Ascii 9) which are forbidden when using either the Dir or Kill statements.  I had alread run code to remove any other disallowed characters from filenames, so it took a while to figure out that this was even an option.

    Here's the code I used to fix this by putting spaces in the place of the tab characters:

        strFilename = Replace(strFilename, Chr(9), " ")

    Since then, the code has run smoothly.

    I am still, however, unable to figure out why I couldn't trap the runtime error, but that's not causing as much of a problem anymore.

    Thanks for your help, everyone!


    Will Knapp 20+ Year Access VBA Developer

    Friday, July 05, 2013 3:40 PM
  • After much trial and error, I was finally able to determine that offending strFilenames (which are derived from Mailitem.ConversationTopics) had one or more tab characters (Ascii 9) which are forbidden when using either the Dir or Kill statements.  I had alread run code to remove any other disallowed characters from filenames, so it took a while to figure out that this was even an option.

    Here's the code I used to fix this by putting spaces in the place of the tab characters:

        strFilename = Replace(strFilename, Chr(9), " ")

    Since then, the code has run smoothly.

    I am still, however, unable to figure out why I couldn't trap the runtime error, but that's not causing as much of a problem anymore.

    For what it's worth, error 52 is "Bad file name or number".  You can determine that by entering this in the Immediate Window:

        ?AccessError(52)

    That's a handy thing to know when you are presented with an error number and want to know he message associated with it. 


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html


    Friday, July 05, 2013 3:57 PM