Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Info needed on code

Status
Not open for further replies.

compnut24

Technical User
Jun 11, 2001
67
US
hi all, i am currently writing a small program that my teacher has assigned to me. i thought it would be a simple task so i said sure, and began work. i have till next tuesday to complete it. problem is, i dont know how to make certain parts of it. What i need to be able to do is this: using 4 text boxes (txt1 txt2 txt3 txt4) the user will imput information. Using one big main textbox (bodymain) the user will type for example a story. the information from the 4 small textbox's and the info from bodymain and write them both to a .txt file. the 4 small textbox info will be written as header info into the txt file, and bodymain will be written below it. here is what the txt file would look like when opened in notepad. the line# is the line of the txtfile.
1John McColton
2Story for Ms. Craton
3November 14 2000
4Story for english
5
6Hello, my name is John. Im an writing my story
7for Ms. Craton. My story is about my dog.
8
9
10
etc.

my program will also be able to open the txt file. i want the info in lines 1-4 to be loaded into the 4 small textbox's and body main so you can edit and save them again. i know what i want to acomplish with my code, i just dont know what code to write. if anyone can help me out, or give me some suggestions it would be greatly appreciated.
 
To open a file, look up the Open statement (Open "Filename" For Output/Append AS #1 depending on whether you want to overwrite what is there or append to what is there, or Open "Filename" For Input AS #1 to read from the file)
To write to the file, look up Print / Put / Write
To read from the file look up Input or Line Input

Do you need help with any specific parts of this??

It seems from the original post that you want help with all of it. Shall we take it one step at a time??

Simon
 
yeah, the whole think about making this program is a little confusing to me, i am just a novice vb programmer, so help in every area is needed please.
 
OK. Let's consider writing to file first.
The first question is "Do you really want the output from the large text box to be split into multiple lines??"
It will make it much harder / time consuming if you want to do this as then you will have to parse the text in the textbox and split it yourself - having to check that you are not splitting in the middle of a word,....
You should have a button on your form to write out to a file. The code behind the button should:
1. Open the file in the correct mode
2. Write out the information
3. Close the file

Consider first of all, opening the file in 'append' mode, so that we do not overwrite any existing information:

Open "c:\aa\sample.txt" For Append As #1

write out the lines for the first 4 text boxes:

Print #1, "1" & Text1(0).Text
Print #1, "2" & Text1(1).Text
Print #1, "3" & Text1(2).Text
Print #1, "4" & Text1(3).Text

Close the file:

Close #1

Before you close the file, you will need to write out the main part of the data. I have ommitted that here as you have to answer the question above before you can decide how to do this, but the code above should be enough for you to be able to do that.

Simon
 
ok, i dont want to split up the main part of the text (bodymain) i just want to write the contents of bodymain below line number 4. btw, thanks, thanks a lot for helping me out.
 
Have you looked up the Open statement in the help files??

It is not a good idea to open explicitly as '#1', but it is better to use a variable instead, so that if you have more than 1 file open at a time, you do not get mixed up with the numbers and accidentally write to the wrong file.

Look up "FreeFile" in the VB help.

Simon
 
remember append only adds stuff to the bottom of a file. output clears the file and starts from the begginging (or it makes the file if it dosnt exist)
Karl Pietri
lordhuh.pota.to

 
Working with the Write and Read commands is fine (and probably a little bit easier) but you can also look up the File System Object in the help files. FSO might be a little bit more complicated but I think its a more useful object in the long run.

You'll need to add a reference to the File System Object from the Project-Reference menu option. YOu will want to select Microsoft Scripting Runtime.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top