Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Nov 2004
    Location
    Perth, UK
    Posts
    105
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Visual Basic 6 - Load from text file

    Before I implement a save/load feature into one of my other applications, I created a new program to test it. I put some textboxes, buttons and a couple of checkboxes on the form, and then entered text and used the checkboxes to disable/enable the buttons. Then I pressed my save key. No problems.

    The code I used to save was this.

    Code:
       Dim hFile As Long
       Dim sFilename As String
    
       sFilename = CurDir + "\savedata.txt"
       
       hFile = FreeFile
       Open sFilename For Output As #hFile
          Print #hFile, txtItemOne.Text
          Print #hFile, txtItemTwo.Text
          Print #hFile, txtItemThree.Text
          Print #hFile, Command1.Enabled
          Print #hFile, Command2.Enabled
          Print #hFile, Check1.Value
          Print #hFile, Check2.Value
       Close #hFile
    
    MsgBox "Data Saved. A file called 'savedata.txt' was created.", vbInformation Or vbOKOnly, "Save Data"
    I quit the application, restarted it and pressed the load button. Everything was put into one textbox. That is to say, the data I entered into the textboxes didn't appear where it should have and my checkboxes and buttons weren't checked/disabled and vice versa.

    The code I use to load is this:

    Code:
       Dim hFile As Long
       Dim sFilename As String
    
       sFilename = CurDir + "\savedata.txt"
       
       hFile = FreeFile
       Open sFilename For Input As #hFile
          txtItemOne.Text = Input$(LOF(hFile), hFile)
          txtItemTwo.Text = Input$(LOF(hFile), hFile)
          txtItemThree.Text = Input$(LOF(hFile), hFile)
          Command1.Enabled = Input$(LOF(hFile), hFile)
          Command2.Enabled = Input$(LOF(hFile), hFile)
          Check1.Enabled = Input$(LOF(hFile), hFile)
          Check2.Enabled = Input$(LOF(hFile), hFile)
       Close #hFile
    What am I doing wrong? I followed the instructions on the online tutorial exactly.
    Geodesic_D
    "I'd much rather be called GeodesicDragon."

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    I don't code VB6, but in VB.NET and other languages, there are often 2 commands to write to a file. One includes a newline. The other does not.
    Have you checked the file contents to see if there are newlines?

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

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