load file gives unknown characters
i have a save a load procedure in my app in this case i am using text box1. so the problem starts when i load a file.
i.e. if i enter data in textbox1 and click save the file saves successfully and if i exit the app and reopen it and click select load and load the file just saved it loads with two mysterious and unknown characters at the end. but if i select property of text box 1 and click multi line to true then the characters will not be displayed.
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
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
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 '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
Re: load file gives unknown characters
Try altering your SaveText function slightly...
Re: load file gives unknown characters
The two "mysterious and unknown characters" are probably carriage returns.
Do they look like little squares?
Re: load file gives unknown characters
thanks milk seems to have sorted the problem.
by the way it was looking like || but bolder.
load file gives unknown characters
how would i save more text boxes to save and load?
Re: load file gives unknown characters
you could set a global variable when a textbox is changed so that the variable contains the name of the last textbox changed (or selected may be better) then change the code in your command buttons
like
vb Code:
Private Sub Cmdload_Click()
cdlg.ShowOpen
me.controls(mytxtbox).text = LoadText(cdlg.FileName)
End Sub
where mytxtbox is variable containg the name of the last change or selected textbox