• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

Visual Basic 6.0 - Writing to a data file

I need help (ya I'm New at this) writing to a data file.  I have 8 txt boxes that need to be saved to my data.txt file.  Can't seem to get it to work.  Any suggestions????
3 Solutions
Try this.. You will have to go to the "project | reference" menu and add a reference to "Microsoft Scripting Runtime Library" in vb.

Private Sub WriteTextFile(ByVal FileName As String, ByVal Text As String)
   Dim FS As New FileSystemObject
   Dim Stream As TextStream
   Set Stream = FS.OpenTextFile(FileName,ForWriting,False)
   Call Stream.Write(Text)
   Set FS = Nothing
End Sub
I havnt dont this forever and I use .net now but I think that works.
or use the built in commands:

sub WriteFile(byval sData as string, byval sFilePath as string)

  dim iFile%
  iFile = FreeFile
  Open sFilePath For Append As iFile
  Print #iFile, sData
  Close iFile

end sub
You can place this code in the click event of a button:

' Start the code to Write To File
Private Sub Command1_Click()

Open "C:\Path\data.txt" for Output as #1
 Print #1, text1.text
 Print #1, text2.text
 Print #1, text3.text
 Print #1, text4.text
 Print #1, text5.text
 Print #1, text6.text
 Print #1, "Hello"   ' you can also put string in
 Print #1, 123       ' you can also put numbers in

close #1

' End the code to Write To File

' Start the code to Read from File

' A Little bit more tricky. You have to create a variable,
' then load the file one line a time into the variable,
' then use the variable to display info.

Private sub Command2_Click()

open "C:\path\data.txt" for input as #1

 dim InfoHolder as String

 do while Not Eof(1)
 Line Input #1, InfoHolder
 Text1.Text = Text1.Text & vbcr & InfoHolder

Close #1
Edn Sub

' Of course if you know there are always only a certain
' amount of lines, eg, Name, Surname, Age, you can declare
' three variable and use them.

Private Sub Command2_Click()

Dim Namevar as String
Dim SurNamevar as String
dim Agevar as Integer

open "C:\path\data.txt" for input as #1

 Line Input #1, Namevar
Line Input #1, SurNamevar
Line Input #1, Agevar

Close #1

 Text1.Text = Namevar
 Text2.Text = SurNamevar
 Text3.Text = Agevar
End Sub

The #1 statement is very important. You cannot open two files as #1 at the same time. If you need to open two files use #2, #3, etc.

You also can't try to open a file if it's already open.
Check out VB's Help files for Sequential files, and random access files for more info

' End the code to Read from File
End Sub
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now