Page 1 of 1

Getting information from a text file Rate Topic: -----

#1 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Posted 30 April 2008 - 08:22 AM

This tutorial will show you how to get information from a text file into a text box on a form. It will also show you how to change the data.

For this you will need a standard form, two text boxes and two buttons. You will also need a text file.

First thing you need to do is to create the form with the text boxes and buttons. Under the first button we will put the code to get the data from the text file and to put it into the first text box:
Private Sub Command1_Click()
'This will declare the variable data as a string
Dim data As String

'This will find the file that we are using
Open "\the address\name of file.txt" For Input As #1
		Input #1, MyData
 'This puts the data in the textbox   
		 Text1.Text = MyData
	Close #1
End Sub



In the text file put a number into the it and save. now when you press F5 and press the first button it should show you the number. Simple now we look at changing the data.

This next part is a bit bigger but what it does is that it will change the data in the text file but not add to it.

Private Sub Command2_Click()

	Dim data As String
	
	Open "\vb\tutorials\datafile.txt" For Input As #1
		Input #1, MyData
	Close #1
	
		Open "\vb\tutorials\datafile.txt" For Output As #1
	
		mydata2 = Val(Text2.Text) + Val(MyData)
	Print #1, mydata2
	myTimeStamp = Now
	Print #1, "data"; myTimeStamp
	
	Close #1
		Open "\vb\tutorials\datafile.txt" For Input As #1
		Input #1, MyData
	Close #1
	
	Text2.Text = MyData
End Sub



It is similar to the first part but now it will change the data and add the date and time.

If you have any problems then just comment and I will do all that I can to help

Is This A Good Question/Topic? 0
  • +

Replies To: Getting information from a text file

#2 jakerman999  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 07-August 08

Posted 10 August 2008 - 11:22 PM

for reading the data, wouldn't it be more affective to load myData in to the textbox, change the data in the box, the write the textbox to myData and write to the file again? seems inefficient to me.

also, as you print instead of write, you won't be able to load it back in. it needs "quotation marks" on each end.
Was This Post Helpful? 0
  • +
  • -

#3 akhileshbc  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 179
  • Joined: 27-September 08

Posted 27 September 2008 - 10:16 PM

Your code is little bit more tough to do:) The below code will load the first line of a text file into a text box. And you can make changes to the data in the Textbox. When you click the save button, the text in the textbox will be saved into the file....:)
Private Sub cmdOpen_Click()
Dim FF As Integer
Dim myData As String

FF = FreeFile
Open "c:\akhilesh.txt" For Input As #FF
Input #FF, myData
Text1.Text = myData
Close #FF
End Sub

Private Sub cmdSave_Click()
Dim FF As Integer

FF = FreeFile
Open "c:\akhilesh.txt" For Output As #FF
Print #FF, Text1.Text
Close #FF
End Sub 

Was This Post Helpful? 0
  • +
  • -

#4 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Posted 28 September 2008 - 01:33 AM

If this is supposed to load the first line what about the rest that is in the file?
Was This Post Helpful? 0
  • +
  • -

#5 akhileshbc  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 179
  • Joined: 27-September 08

Posted 28 September 2008 - 03:16 AM

 Private Sub cmdOpen_Click()
Dim FF As Integer
Dim myData As String
''''you have to set the MultiLine property of textbox to True
FF = FreeFile
Open "c:\akhilesh.txt" For Input As #FF
While Not EOF(FF)
Line Input #FF, myData
Text1.Text = Text1.Text1 & myData & vbNewLine
Wend
Close #FF
End Sub

Private Sub cmdSave_Click()
Dim FF As Integer

FF = FreeFile
Open "c:\akhilesh.txt" For Output As #FF
Print #FF, Text1.Text
Close #FF
End Sub


This will load the entire text file into a textbox. But remember, you have to set the MultiLine property of textbox to True

-Regards :)
Akhilesh B Chandran
Was This Post Helpful? 0
  • +
  • -

#6 ravmonster  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 162
  • Joined: 20-July 09

Posted 22 July 2009 - 05:01 AM

how about if i want to add a search to search the textfile to be edited
what should be the code??????
Was This Post Helpful? 0
  • +
  • -

#7 Adisco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-October 09

Posted 02 October 2009 - 04:48 AM

View Postakhileshbc, on 28 Sep, 2008 - 02:16 AM, said:

 Private Sub cmdOpen_Click()
Dim FF As Integer
Dim myData As String
''''you have to set the MultiLine property of textbox to True
FF = FreeFile
Open "c:\akhilesh.txt" For Input As #FF
While Not EOF(FF)
Line Input #FF, myData
Text1.Text = Text1.Text1 & myData & vbNewLine
Wend
Close #FF
End Sub

Private Sub cmdSave_Click()
Dim FF As Integer

FF = FreeFile
Open "c:\akhilesh.txt" For Output As #FF
Print #FF, Text1.Text
Close #FF
End Sub


This will load the entire text file into a textbox. But remember, you have to set the MultiLine property of textbox to True

-Regards :)
Akhilesh B Chandran





What if i want to display this text data automatically in the text box. I mean read the text one after the other by just a single click
Was This Post Helpful? 0
  • +
  • -

#8 firebolt  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 92
  • View blog
  • Posts: 5,561
  • Joined: 20-February 09

Posted 03 October 2009 - 02:01 AM

Make a post in the VB6 forum and people can help you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1