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

Join 107,398 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,171 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! 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 - 08: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 - 11: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


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: 8/28/08 04:30PM

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