problem loading and saving file
i am using the following code which checks if a file exists if not it creates a file
Code:
Private Sub cmdload_Click()
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
If FileExists("test.txt") Then
Open "est.txt" For Input As #1
Print #1, label1.Caption
Close #1
Else
Call create_test
End If
End Sub
Sub create_test()
Open "test.txt" For Append As 1
Print #6, label1.Caption
Close 6
End Sub
Function FolderExists(sPath As String) As Boolean
FolderExists = CreateObject("Scripting.FileSystemObject").FolderExists(sPath)
End Function
Function FileExists(sFullFileName As String) As Boolean
FileExists = CreateObject("Scripting.FileSystemObject").FileExists(sFullFileName)
End Function
but when i click load it says "bad file mode"
what want to do is if after click load the file does not exist then it creates a file and loades the value in label1. but it seems i am doing something wrong. any ideas
Re: problem loading and saving file
To check whether if the file exists and write, you can use just the Append mode as below
Open "G:\test.txt" For Append As #1
Print #1, "abcdefghi"
Close #1
it will take care of creating a file if not exists...and keep appending if one exists...
Im Sorry, I dint go thru your post fully...
Re: problem loading and saving file
print #1 tries to write to a file that is open for input hence bad file mode,
you need to use input or lineinput to read the file
Re: problem loading and saving file
Quote:
Open "est.txt" For Input As #1
Print #1, label1.Caption
Close #1
Yeah, westconn1 is right.
For inputting, it should be
Code:
Open "est.txt" For Input As #1
Line Input #1, strTemp
Close #1
or
Code:
Open "est.txt" For Input As #1
Input #1, strTemp
Close #1
And for writing to a file,
Code:
Open "est.txt" For Output As #1
Print #1, strTemp
Close #1