Search Tools Links Login

Saving Files

FOR BEGGINERS - This article explains clearly about how to save files in VB 6.0, it is SUPER EASY to fallow and explains everything very well.

Original Author: MasterDave


Start by making a new project, Standard EXE. Make one Textbox and one Command Button


Now for the code...
First make a variable called "F" as integer.

Open the code window for the command button and we will review this code
Type in the code for the command button's click event (The click event is the default event that loads when you view an objects code for the first time, so really you don't have to change it)

F = FreeFile

Open App.Path & "X.txt" for Output as #F

'*NOTE* The code above is only the first 2 lines

"Open" - When you type this it simply means you are going to open some file to put something in the file, get something from the file, or do whatever you need to do with it. Just remember you need to open it before you can do something with it :)

"App" - App just supplies information about the program that you are making. For example, one of the things it supplies is the location for your program that you will be making.

"Path" - Path simply means that the string that you will write after it will be a Path or Location to a file on your computer.

"X.txt" - This is the location of the file that you will want to open. If the file does not already exist it will create it for you. Since we typed X.txt without some drive like "C:" that means the file will be created in the path/location of your program.

"For Output" - This means that you will open the file for output, output means that it will erase all the current information inside the file and replace it with whatever you want to replace it with. There is also "Append" which will continue from an existing file and not erase the data inside it. If the file does not exist, it will be created for you.

"#F" - #F is just a number to refer to the file.
Would you want to always type the path of the file every time you wanted to refer to it? That's why you can just say #F. You

'*NOTE* The first line just opens the file to process with it. (File Processing)


3rd line of code (still in the same code window)
Type the fallowing line underneath

Open App.Path & "X.txt" for Output as #F

Print #F, Text1.Text

'*NOTE* this is only the 2nd line of code.

Now lets review the code for this line
"Print" - This is telling your program to print something. You can also put "Write" instead of Print, but if you put "Write" you will see Quotes (" ") around your text inside the file. I suggest you put "Print".

"#F," - Remember how I said this is a reference to the file? Well that's how the "Print" statement will know what to print to. If you just put "Print Text1.Text" it will print the text on your form instead of the file. Don't forget you need the "," so you can add what you want it to print after that.

"Text1.Text" - This means that the program will print a line of whatever is currently in the text of the textbox "Text1" to the file.

'*NOTE* So the 3rd line of code will just print something inside the file.


Now the 4th line, the easiest one
Underneath the first 3 lines write:

"Close #F" - This will just close the file that is currently in "#F". What was in "#F"? "X.txt" is in #F remember? So it will just close the file. Why do we need to close the file you ask? We need to close the file so other programs can open it when they need to. Like the WordPad program might need to open it so you can view the text inside it. Also you might want to overwrite the file from your program, if you don't close it you cannot overwrite it again. Another thing, if you don't close the file, it will take up space in your system memory and will run your computer slower than it should be running.

'*NOTE* The 4th line just closes the file that you opened in the first line.

So all together you will have 4 lines of code, which will be:


F = FreeFile

Open App.Path & "X.txt" For Output as #F

Print #F, Text1.Text

Close #F


If there is anything wrong with my code please leave feedback telling me what was wrong.
If you liked it, remember you can vote for me if you want to.

Please try not to insult me when leaving feedback :)

About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 89 times


Visual Basic 6


No attachments for this post

Loading Comments ...


No comments have been added for this post.

You must be logged in to make a comment.