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
module codeCode: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
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




Reply With Quote