Answered by:
Runtime error 52 when using dir function

-
text/sourcefragment 7/2/2013 2:19:54 PM Will Knapp 0
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
Question
Answers
-
text/sourcefragment 7/5/2013 3:40:34 PM Will Knapp 0
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
- Marked as answer by Yoyo In SkyModerator Friday, July 19, 2013 6:22 AM
All replies
-
text/sourcefragment 7/2/2013 2:38:35 PM Hans Vogelaar MVP 0
-
text/sourcefragment 7/2/2013 2:59:40 PM Will Knapp 0
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
-
text/html 7/2/2013 3:01:33 PM Will Knapp 0
-
text/sourcefragment 7/2/2013 3:15:04 PM Hans Vogelaar MVP 1
-
text/sourcefragment 7/2/2013 3:17:33 PM Will Knapp 0
You could bypass the problem as follows:
On Error Resume Next Kill strPath & strFilename On Error GoTo 0
Regards, Hans Vogelaar
Will Knapp 20+ Year Access VBA Developer
-
text/html 7/2/2013 3:22:09 PM Lawrence Ellefson 0
-
text/html 7/2/2013 3:37:15 PM Will Knapp 0
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
-
text/html 7/2/2013 3:48:09 PM La Mole 0
-
text/sourcefragment 7/5/2013 3:40:34 PM Will Knapp 0
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
- Marked as answer by Yoyo In SkyModerator Friday, July 19, 2013 6:22 AM
-
text/sourcefragment 7/5/2013 3:57:49 PM Dirk Goldgar MVP 1
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- Edited by Dirk Goldgar MVPMVP Friday, July 19, 2013 11:40 AM typo