Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,475 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,319 people online right now. Registration is fast and FREE... Join Now!




Getting information from a text file

 
Reply to this topicStart new topic

> Getting information from a text file

sam_benne
Group Icon



post 30 Apr, 2008 - 07:22 AM
Post #1


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:
CODE

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.

CODE

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
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

jakerman999
*



post 10 Aug, 2008 - 10:22 PM
Post #2
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.
Go to the top of the page
+Quote Post

akhileshbc
Group Icon



post 27 Sep, 2008 - 09:16 PM
Post #3
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....smile.gif
CODE
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
Go to the top of the page
+Quote Post

sam_benne
Group Icon



post 28 Sep, 2008 - 12:33 AM
Post #4
If this is supposed to load the first line what about the rest that is in the file?
Go to the top of the page
+Quote Post

akhileshbc
Group Icon



post 28 Sep, 2008 - 02:16 AM
Post #5
CODE
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 smile.gif
Akhilesh B Chandran
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 11/22/08 02:36PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month