View Full Version : Visual Basic 6 - Load from text file

10-12-2007, 02:22 AM
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.

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:

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.

10-13-2007, 12:24 AM
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?

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum