Click to See Complete Forum and Search --> : How do i create a text file using vb code?


tim900000
October 16th, 2001, 05:01 PM
I have a program that determins whether a text file exists or not and works fine if it does exist.

If the file does not exist, how do I write the vb code to create it?

Thanks in advance...



If Dir("C:\abc.txt") = "" then
MsgBox "File with settings does not exist"
else
FileNum = FreeFile
Open "c:\abc.txt" for input as #FileNum
for i = 0 to 23
input #FileNum, Reading
Text1(i) = Reading
next

Close #FileNum
End If

michi
October 16th, 2001, 05:17 PM
I prefer user FSO(File System Object):

Sub OpenTextFileTest()
Const ForAppending = 8
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject")
' Note: Set the last parameter to True, it will automatically create a new text file if it doesn't exist
Set f = fs.OpenTextFile("c:\testfile.txt", ForAppending, True)
f.Write "Hello world!"
f.Close
End Sub

Regards,

Michi

Robert Moy
October 17th, 2001, 09:24 PM
Hello:

You can use Commondialog to do it:

private Sub Command1_Click()
CommonDialog1.ShowSave
MsgBox "My file to be saved:" & CommonDialog1.FileName
End Sub





private Sub Command2_Click()
Open "c:\abc.txt" for output as #FileNum
for i = 0 to 23
input #FileNum,Reading
Text1(i) = Reading
next
Close #FileNum




Good Luck