|
-
Jan 18th, 2000, 04:20 PM
#1
Thread Starter
Hyperactive Member
How can I check if I a file exists when using the open function?
e.g
Open "c:\filename.txt" For Input As #1
if the file does`nt exist a "run-time" error window occurs. Is there anyway to stop this?
Either a
exists = doesfileexist("C\filename.txt")
or an extension of the Open function?
Thanks in advance.
-
Jan 18th, 2000, 04:57 PM
#2
Lively Member
The only true VB way to check if a file exists is to use the dir function.
The Open function will create the file for you if it does not exist.
So to check, use the following:
If Dir("c:\filename.txt") <> "" Then
'|File does exist
Else
'|File does not exist
End If
I don't know where your runtime error comes from; it might be syntax error or something like that.
If you want to, send me the code and I’ll have a look at it for you. Or else I will create you a small sample.
Just let me know.
Hope it helps.
-----------------------
Maartin
[email protected]
-----------------------
[This message has been edited by Maartin (edited 01-19-2000).]
-
Jan 18th, 2000, 05:06 PM
#3
-
Jan 18th, 2000, 05:09 PM
#4
Thread Starter
Hyperactive Member
Cheers chaps, that works a treat
[This message has been edited by Rick H (edited 01-19-2000).]
-
Jan 18th, 2000, 05:50 PM
#5
Junior Member
Just a quick comment. First of all Rick H got the error because he was trying to open a file that didn't exist. Second, you can get errors if you try to use Dir() together with the filename AND the full path, if the path doesn't exist. You can check if a path exist by using Dir(path$, vbDirectory).
-
Jan 18th, 2000, 06:15 PM
#6
Hyperactive Member
Yes, but you can also just easily trap the error...
Code:
On error goto errhandler
Dim iFile As Integer
iFile = FreeFile
Open PathAndFileName For Input As #iFile
' do something
Close #iFile
Exit Sub ' or function
errhandler:
' trap the several errors that can occur here...
' don't forget to close the file if another error occured then "does not exist"
msgbox Err.Number & " - " & Err.Description
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|