?
Solved

vb6 - file not found

Posted on 2009-02-19
7
Medium Priority
?
239 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:XK8ER
  • 4
  • 2
7 Comments
 
LVL 3

Expert Comment

by:Earshel
ID: 23688783


 
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

0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 23688794
Use the isFileExist property of the FileSystemObject to determine if the fileExist.
0
 
LVL 3

Expert Comment

by:Earshel
ID: 23688803
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

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:JackOfPH
ID: 23688816
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
0
 
LVL 1

Author Comment

by:XK8ER
ID: 23689005
jackofph your code marks this line as yellow
If fs.FileExist(App.Path & "\names.txt") = False Then
object does not support this property
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 23689108
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
0
 
LVL 15

Accepted Solution

by:
JackOfPH earned 2000 total points
ID: 23689111
Here is the correct code... :)

Private Sub Command1_Click()
    Dim fs
    Dim a
   
    Set fs = CreateObject("Scripting.FileSystemObject")
    If fs.FileExists(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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question