eHow launches Android app: Get the best of eHow on the go.

How To

How to Read & Write Text Files With Visual Basic 6.0

Contributor
By Robert Karr
eHow Contributing Writer
(0 Ratings)

Visual Basic 6.0 provides a straightforward way to open text files and read their contents into a text box on the screen, as well as write new text to a file. Only eleven lines of code are required for the first step, and ten will suffice for the second operation. The process uses the Input and Output functions in Visual Basic, as well as a small window to gather information from the user.

Difficulty: Moderately Easy
Instructions

Things You'll Need:

  • Visual Basic 6.0
  1. Step 1

    Open Visual Basic 6.0, click File and choose New Project. Select Standard EXE from the list of templates. Double-click the TextBox control in the Toolbox on the left to add this to the form. Change the height of the Form in the Properties pane on the right to 5000.

  2. Step 2

    Make the following changes to the TextBox Properties:
    Height = 3000
    Multiline=True
    Scrollbars=Both
    Width=3000

  3. Step 3

    Double-click the Button control in the Toolbox. Change the Caption property to Read File. Repeat this operation to add a second Button control, changing the Caption property here to Write File.

  4. Step 4

    Double-click the Read File button to open the code window. Enter this code on the code view page:
    Function ReadFile()
    Dim ReadFileName As String
    ReadFile = InputBox("Enter name of file to read:")
    Open ReadFile For Input As #1
    ReadFile = Input$(LOF(1), 1)
    Text1.Text = ReadFile
    Close #1
    End Function
    Private Sub Command1_Click()
    ReadFile
    End Sub

  5. Step 5

    Double-click the Write File button to open the code window. Enter this code:
    Function WriteFile()
    Dim WriteFileName As String
    WriteFileName = InputBox("Enter file name")
    Open WriteFileName For Output As #2
    Print #2, Text1.Text
    Close #1
    End Function
    Private Sub Command2_Click()
    WriteFile
    End Sub

  6. Step 6

    Run the program by pressing F5 and click on the Read File command first. Enter the name of a known text file, including the .txt suffix. The file should appear in the TextBox. If you now click on Write File and provide a different name, you will create a new but duplicate file. Alternatively, if you delete the text or just click on Read File before loading a file and then type something new, you can create a completely new file.

Tips & Warnings
  • To append new material to an existing file, change the line in the WriteFile section from Open WriteFileName For Output As #2 to Open WriteFileName For Append As #2. You must use different file numbers for each open file and close each file when finished. Modify the size of the text box to fit your own needs. For a more robust application, it would be helpful to add error trapping routines in case the user mistypes a file name or already has a file open.
  • Computer languages are intolerant of typos, other departures from approved code, or erroneous file names. If this example does not execute correctly, recheck your code and/or the name of the file.
Resources
Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

eHow Computers
eHow_eHow Technology and Electronics