How to save text to a text file

Firstly, to start off, open up Visual Basic 6.0.

When you've done that, it should prompt you with a dialog box asking you what type of project you want to make. Choose Standard EXE and press open.

First Step.

Once you have done that, you should have something that looks like this:

Step 2.

Now we are going to set up the design of the application. To do this we are going to need one textbox, one command button and one Common Dialog.

So go over to your toolbox (the thing that says "General" and has lots of little icons), and select the command button (the one circled in red in the picture below) and create one of them on the form. Then select the textbox (the one circled in green) and create one of them on the form.

Step 3

Step4.2

Now we need to insert the common dialog into the toolbox. We do this because it is not there when we start up Visual Basic 6.0. So go ahead and press CONTROL+T. This will bring up the components window. Scrooooolll down until you find something that says, "Microsoft Common Dialog Control 6.0" or similar. Select it and press OK.

Step 5

You will notice a new icon in your toolbox. Click it and drag it onto your form.

Step 6

Now we need to name these objects. To do this, click on the textbox and go to the properties dialog (it's on the right hand side).

Step 7

Go to the name arena. Change the value from Text1 to txt_Input. Do the same for the command button. But instead, call it cmd_save. And the same for the Common Control, except call it CD1.

You can now, also, change the caption of the Command button to Save or whatever you like. Do this the same way we did for the name. Except look for "Caption" instead of Name. You can change the value of the text box as well. Click on it and scroll down to Text on the properties dialog. Change it to whatever you want. You can always change it when the application is running.

.Now for the fun part, coding!

Double click on the command button. It should come up with code like this:

Private Sub cmd_save_Click()

End Sub

In the middle of this, insert this code

Dim hFile As Long
Dim sFilename As String
CD1.ShowSave
sFilename = CD1.filename

hFile = FreeFile
Open sFilename For Output As #hFile
Print #hFile, txt_input.Text
Close #hFile

You should now have this code

Private Sub cmd_save_Click()
Dim hFile As Long
Dim sFilename As String
CD1.ShowSave
sFilename = CD1.filename

hFile = FreeFile
Open sFilename For Output As #hFile
Print #hFile, txt_input.Text
Close #hFile

End Sub

What this is telling the application to do is, when you press the command button it is dimming sFilename as a string. This means we can use the word filename throughout our project and it will recognize it for what the value is. We made the value CD1.Filename. We did this by typing sFilename = CD1.filename

Then we had CD1.ShowSave. This is telling the common dialog to show the save dialog.

Then we have

hFile = FreeFile
Open sFilename For Output As #hFile
Print #hFile, txt_input.Text
Close #hFile

This is telling the app to open "sFilename" (remember that it is a string. So it's the name that you selected when the Common idalog showed up) for output. And then it's saying to print the text in "txt_input.text" to the file. You could also use "Write" instead of Print. But this will ad Quotes to the file.

Now press F5. This will run your application. Alternitivly you can press this button (the one highlighted in blue):

You should be able too, now, when your application is running, be able to press one button and it opens a save dialog. You should then choose a file oe make one. Then click save. It should then save the text that you typed to that file.

When you are done with your application, exit out of it and save.

 

Download Project | Back to tutorials

 

Comment on this article:

Admin

Name: *
Email:
Homepage:
Message: *

Date:2009-01-04 - 03:39:43
Name:tony
Homepage:http://www.v3dates.com/
Message:oh yah, im back.

dude you should make tuts for VB 2008, coz this shits kinda confusing..
older versiion

c yah.. lol.

Date:2009-01-04 - 03:38:34
Name:tonyy
Homepage:http://www.v3dates.com/
Message:hey buddz, great site, really helpin me learn my way arround VB..

n peter, fukk off this sites good, im learnin shit :) visual is pretty
sick, if i knew what i was doin..

pce.

Date:2008-10-21 - 21:11:46
Name:Eliot
Message:Yes, shit. Just like your grammar and spelling.

I made this tutorial for people that do not know how to do this. Because we
aren't all as 'good' as you when it comes to programming. Why don't you
make a Visual Basic tutorial site? Maybe one that isn't shit like this
tutorial as you so very well pointed out. One that will teach people stuff?
Suck a goat, asshole.

Date:2008-10-16 - 05:27:49
Name:peter mackoy
Homepage:http://www.google.com
Message:you mother fuckers who cant program for shit
do u like dis tut it tis shit

Date:2008-07-31 - 07:43:09
Name:vig
Homepage:http://www.tutorialized.com/view/tutorial/Save-fil
Message:hi