Link to home
Start Free TrialLog in
Avatar of XK8ER
XK8ERFlag for United States of America

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
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

Open in new window

Avatar of Earshel
Earshel



 
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")
    End If

Open in new window

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

Open in new window

Private Sub Command1_Click()
    Dim fs As Object, a As Object
   
    Set fs = CreateObject("Scripting.FileSystemObject")
    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
Avatar of XK8ER

ASKER

jackofph your code marks this line as yellow
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.FileSystemObject")
    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
ASKER CERTIFIED SOLUTION
Avatar of JackOfPH
JackOfPH
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial