Visual Basic 6.0 - Writing to a data file

Posted on 2003-02-23
Medium Priority
Last Modified: 2010-04-17
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????
Question by:Yaya
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

colmstea earned 136 total points
ID: 8006035
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.

Assisted Solution

dwfallin earned 132 total points
ID: 8006752
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

Assisted Solution

JSMCM earned 132 total points
ID: 8006786
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

Expert Comment

ID: 9447562
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes
Suggested Courses
Course of the Month7 days, 20 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question