XK8ER
asked on
vb6 - file not found
hello there..
how can I make this code create a new file if the file is not found.. instead of crashing..
thanks
how can I make this code create a new file if the file is not found.. instead of crashing..
thanks
Private Sub Command1_Click()
Dim fs As Object, a As Object
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(App.Path & "\names.txt", 8)
a.WriteLine (Text2.Text)
a.Close
End Sub
Use the isFileExist property of the FileSystemObject to determine if the fileExist.
Sorry, my previous post wasn't quite complete, should be:
Sub Command1_Click()
Dim fs As Object, a As Object
If Dir(App.Path & "\names.txt") <> "" Then
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(App.Path & "\names.txt", 8)
a.WriteLine (Text2.Text)
a.Close
Else
Msgbox "File " & App.Path & "\names.txt does not exist!"
End If
End Sub
Private Sub Command1_Click()
Dim fs As Object, a As Object
Set fs = CreateObject("Scripting.Fi leSystemOb ject")
If fs.FileExist(App.Path & "\names.txt") = false then
fs.CreateTextFile(App.Path & "\names.txt", True)
end if
Set a = fs.OpenTextFile(App.Path & "\names.txt", 8)
a.WriteLine (Text2.Text)
a.Close
End Sub
Dim fs As Object, a As Object
Set fs = CreateObject("Scripting.Fi
If fs.FileExist(App.Path & "\names.txt") = false then
fs.CreateTextFile(App.Path
end if
Set a = fs.OpenTextFile(App.Path & "\names.txt", 8)
a.WriteLine (Text2.Text)
a.Close
End Sub
ASKER
jackofph your code marks this line as yellow
If fs.FileExist(App.Path & "\names.txt") = False Then
object does not support this property
If fs.FileExist(App.Path & "\names.txt") = False Then
object does not support this property
Sorry, it should be FileExists not FileExist
Here is the revice code...
Private Sub Command1_Click()
Dim fs
Dim a
Set fs = CreateObject("Scripting.Fi leSystemOb ject")
If fs.FileExist(App.Path & "\names.txt") = false then
fs.CreateTextFile(App.Path & "\names.txt", True)
end if
Set a = fs.OpenTextFile(App.Path & "\names.txt", 8)
a.WriteLine (Text2.Text)
a.Close
End Sub
Here is the revice code...
Private Sub Command1_Click()
Dim fs
Dim a
Set fs = CreateObject("Scripting.Fi
If fs.FileExist(App.Path & "\names.txt") = false then
fs.CreateTextFile(App.Path
end if
Set a = fs.OpenTextFile(App.Path & "\names.txt", 8)
a.WriteLine (Text2.Text)
a.Close
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window