By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,052 Members | 1,456 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,052 IT Pros & Developers. It's quick & easy.

Locating a file with code

100+
P: 443
In my application I backup all tables using acExportDelim and that creates a .txt file. When I do a restore I clear the table first then us the acImportDelim to restore the table. I want to check to be sure that the file is there before I delete any data from the table. I'm using the fReturnFilePath command to see if I have a file and if not post a message that the file isn't available. This works fine in Windows XP but when I compile the program and send it to a Vista user the error message always shows up even if the file is there. Does anybody have a suggestion as to what I might try.
Thanks for any help (I'm using MS Office XP developer Access 2002 SP 3)
Feb 7 '09 #1
Share this Question
Share on Google+
5 Replies


ADezii
Expert 5K+
P: 8,638
@CD Tom
You can attempt an alternative approach that I just proposed in another Thread:
Expand|Select|Wrap|Line Numbers
  1. 'Must set a Reference to the Microsoft Scripting Runtime 
  2. Dim fso As FileSystemObject 
  3. Dim fil As File 
  4.  
  5. Set fso = New Scripting.FileSystemObject 
  6.  
  7. If fso.FileExists("\\serverName\folderName\fileName.txt") Then 
  8.   'code execution here 
  9. Else 
  10.   MsgBox "File and/or Path cannot be found", vbCritical, "File Not Found" 
  11. End If 
Feb 7 '09 #2

NeoPa
Expert Mod 15k+
P: 31,494
This function should work for you (not tried on Vista). If not, using the Name statement to rename the file to itself is another good way that doesn't require any references you may not already have. The error number (you'd want to trap this with On Error Resume Next etc) returned for a failure (the file doesn't exist) is 53.
Expand|Select|Wrap|Line Numbers
  1. 'Exist returns true if strFile exists.
  2. '22/05/2003 Rewritten with better code.
  3. '20/05/2005 Added finding of R/O, System & Hidden files
  4. Public Function Exist(strFile As String, _
  5.                       Optional intAttrib As Integer = &H7) As Boolean
  6.     Exist = (Dir(PathName:=strFile, Attributes:=intAttrib) <> "")
  7. End Function
PS. I expect you've noticed that the Exist() function as shown can also handle wild-cards, in case of need.
Feb 8 '09 #3

ADezii
Expert 5K+
P: 8,638
@NeoPa
Hello NeoPa, just as a side note, the following code will fail if you try to test for the existence of a File using UNC on a Shared Drive, unless you specifically set an Error Trap for 52 (Bad file name or number). That is why I proposed the alternative approach. It appears that Dir() will not always return a Zero-Length String when it should.
Expand|Select|Wrap|Line Numbers
  1. 'Will fail and generate Error 52
  2. If Exist("\\ServerName\ShareName\fileName.???") Then
  3.   'code to process File here
  4. End If
Feb 8 '09 #4

NeoPa
Expert Mod 15k+
P: 31,494
I did some testing when I read this ADezii, but I couldn't find the problem - even for UNC references. That doesn't mean it can't happen of course.

Even so, it's easy enough to trap it if it is required. That's how it would work using the Name statement anyway.
Feb 8 '09 #5

100+
P: 443
Thanks for all the advise I've used the first one because it works with Vista and with Windows 7. Really appreciate all the help.
Feb 8 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.