|
-
Jul 4th, 2008, 12:59 PM
#1
Thread Starter
Addicted Member
[RESOLVED] problem saving file
i found this coding on the internet and am facing a problem if i start the program and enter text in the text box called text1 and click save it saves correctly, but if i exit the program and load the file and enter text in the text box the entered before and the data entered now will not just update but the data entered before is still visible.
e.g.
first attempt in saving
data entered below:
save 1 test
then i click the save button and type/select file name and hit save
it saves fine but if i enter data again it saves double
e.g.
second attempt in saving
first load file then enter data
i then try adding more data in the file
data entered before loaded:
save 1 test
data to be entered underneath save 1 test
save 2 test
i then click save select file name and save.
no problem yet?
load file and look at what you see.
save 1 test
save 1 test
save 2 test
now my question is how can i resolve this annoying problem?
form 1 coding
Code:
Private Sub Cmdsave_Click()
cdlg.ShowSave
SaveText Text1, cdlg.FileName
End Sub
Private Sub Cmdload_Click()
cdlg.ShowOpen
Text1 = LoadText(cdlg.FileName)
End Sub
Private Sub Command3_Click()
Text1 = ""
End Sub
module code
Code:
Option Explicit
Public Function LoadText(FromFile As String) As String
On Error GoTo Handle
'Checking if the file currently exists
If FileExists(FromFile) = False Then MsgBox "File not found. Check if the file is Currently exists.", vbCritical, "Sorry": Exit Function
Dim sTemp As String
Open FromFile For Input As #1 'Open the file to read
sTemp = Input(LOF(1), 1) 'Getting the text
Close #1 'Closing the file
LoadText = sTemp
Exit Function
Handle:
MsgBox "Error " & Err.Number & vbCrLf & Err.Description, vbCritical, "Error"
End Function
Public Function SaveText(Text As String, FileName As String) As Boolean
On Error GoTo Handle
Dim sTemp As String
sTemp = Text
Open FileName For Append As #1 'Opening the file to SaveText
Print #1, sTemp 'Printing the text to the file
Close #1 'Closing
If FileExists(FileName) = False Then 'Check whether the file created
MsgBox "Unexpectd error occured. File could not be saved", vbCritical, "Sorry"
SaveText = False 'Returns 'False'
Else
SaveText = True 'Returns 'True'
End If
Exit Function
Handle:
SaveText = False
MsgBox "Error " & Err.Number & vbCrLf & Err.Description, vbCritical, "Error"
End Function
Public Function FileExists(FileName As String) As Boolean
'This function checks the existance of a file
On Error GoTo Handle
If FileLen(FileName) >= 0 Then: FileExists = True: Exit Function
Handle:
FileExists = False
End Function
-
Jul 4th, 2008, 01:11 PM
#2
Thread Starter
Addicted Member
Re: problem saving file
is there any coding in which after loading the file it deletes the loaded file and am able to save after?
-
Jul 4th, 2008, 01:12 PM
#3
Re: problem saving file
That happens because you're using 'Append'. It adds the new data to the file without overwriting the existing data.
Code:
Open FileName For Append As #1
You should use 'Output'.
Code:
Open FileName For Output As #1
And don't hardcode the number, because you'll get an error if it's already in use. Use FreeFile instead.
Code:
Dim FF As Integer
FF = FreeFile
Open FileName For Output As #FF 'Opening the file to SaveText
Print #FF, sTemp 'Printing the text to the file
Close #FF
-
Jul 4th, 2008, 01:16 PM
#4
Thread Starter
Addicted Member
Re: problem saving file
ok thanks for the reply will check and test the coding and get back. thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|