Results 1 to 6 of 6

Thread: load file gives unknown characters

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    182

    Question 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
    Last edited by adam786; Jul 17th, 2008 at 03:15 PM.

  2. #2
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: load file gives unknown characters

    Try altering your SaveText function slightly...
    Code:
    Print #FF, sTemp;

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: load file gives unknown characters

    The two "mysterious and unknown characters" are probably carriage returns.

    Do they look like little squares?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    182

    Re: load file gives unknown characters

    thanks milk seems to have sorted the problem.

    by the way it was looking like || but bolder.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    182

    load file gives unknown characters

    how would i save more text boxes to save and load?

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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:
    1. Private Sub Cmdload_Click()
    2.     cdlg.ShowOpen
    3.     me.controls(mytxtbox).text = LoadText(cdlg.FileName)
    4. End Sub
    where mytxtbox is variable containg the name of the last change or selected textbox
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width