Lesson 17: Creating Files in Visual Basic 6

Master file handling in VB6 to create, write, and read persistent data

Key Takeaway

VB6 file handling allows you to create, write, and read files for persistent data storage beyond the runtime of your application.

Welcome to Lesson 17 of our Visual Basic 6 Tutorial! In this lesson, you'll learn how to work with files in VB6, enabling you to create persistent data storage solutions. Unlike previous lessons where data vanished when the program ended, file handling allows you to save information to disk and retrieve it later.

17.1 Introduction to File Handling

Up until Lesson 16, our focus has been on creating programs that accept data at runtime, with the drawback that the data vanishes once the program ends. However, in this lesson, we will delve into a valuable skill: learning how to create and store files. By utilizing a customized VB program, we'll be able to write these files to a storage device and subsequently retrieve their contents through reading operations.

Runtime Data

  • Data stored in memory
  • Vanishes when program ends
  • Limited to program execution
  • Cannot be shared between sessions

File Storage

  • Data saved to disk
  • Persists after program ends
  • Can be shared between programs
  • Supports large datasets

17.2 Creating Files

To create a file in VB6, we use the following command:

File Creation Syntax

Open "fileName" For Output As #fileNumber

Each file created must have a file name and a file number for identification. For the file name, you must also specify the path where the file will reside.

FileCreation.vb
' Create a text file in My Documents folder
Open "c:\My Documents\sample.txt" For Output As #1

' Create an HTML file in the same location
Open "c:\My Documents\sample.html" For Output As #2

17.2.1 Sample Program: Creating a Text File

This program creates a text file, prompts the user for input, and writes that input to the file.

CreateTextFile.vb
Private Sub create_Click() 
    Dim intMsg As String
    Dim StudentName As String
    
    ' Open file for writing
    Open "c:\My Documents\sample.txt" For Output As #1 
    intMsg = MsgBox("File sample.txt opened")
    
    ' Get student name
    StudentName = InputBox("Enter the student Name")
    
    ' Write to file
    Print #1, StudentName 
    intMsg = MsgBox("Writing " & StudentName & " to sample.txt")
    
    ' Close file
    Close #1 
    intMsg = MsgBox("File sample.txt closed")
End Sub

File Content Preview

sample.txt contents will appear here after creation

17.3 Reading Files

To read a file created in section 17.2, you can use the input # statement. However, we can only read the file according to the format when it was written. You have to open the file according to its file number and the variable that holds the data. We also need to declare the variable using the DIM command.

17.3.1 Sample Program: Reading a File

This program reads the contents of a text file and displays it in a textbox.

ReadTextFile.vb
Private Sub Reading_Click()
    Dim variable1 As String
    
    ' Open file for reading
    Open "c:\My Documents\sample.txt" For Input As #1 
    
    ' Read file content
    Input #1, variable1
    
    ' Display in textbox
    Text1.Text = variable1
    
    ' Close file
    Close #1 
End Sub

File Reading Output:

17.3.2 Sample Program: Using Common Dialog Box

This example uses the common dialog box to create and read text files, which is more user-friendly than specifying paths directly.

CommonDialog.vb
Dim linetext As String

Private Sub open_Click()
    ' Set filter for text files
    CommonDialog1.Filter = "Text files (*.txt)|*.txt"
    CommonDialog1.ShowOpen
    
    If CommonDialog1.FileName <> "" Then
        Open CommonDialog1.FileName For Input As #1
        
        ' Read file until end
        Do
            Input #1, linetext
            Text1.Text = Text1.Text & linetext
        Loop Until EOF(1)
    End If
    
    Close #1
End Sub

Private Sub save_Click()
    ' Set filter for text files
    CommonDialog1.Filter = "Text files (*.txt)|*.txt"
    CommonDialog1.ShowSave
    
    If CommonDialog1.FileName <> "" Then
        Open CommonDialog1.FileName For Output As #1
        Print #1, Text1.Text
        Close #1
    End If
End Sub
File Dialog Interface
Figure 17.1: Common dialog interface for file operations

Lesson Summary

In this lesson, you've mastered VB6 file handling techniques:

File Creation

Using the Open...For Output command to create files

Writing Files

Using Print # to write data to text files

Reading Files

Using Open...For Input and Input # to read file contents

Common Dialog

Using the CommonDialog control for user-friendly file operations

Best Practice

Always close files after you finish working with them using the Close statement. This ensures that all data is properly written to disk and releases system resources.

Practice Exercises

Test your understanding of VB6 file handling with these exercises:

Exercise 1: Student Records

Create a program that saves student names to a text file and allows users to retrieve them later.

Exercise 2: Configuration Saver

Develop an application that saves user preferences to a configuration file and loads them on startup.

Exercise 3: CSV Generator

Create a program that writes comma-separated values to a CSV file that can be opened in Excel.

Exercise 4: Log File Creator

Build an application that appends timestamped log entries to a text file each time an action is performed.

Exercise 5: File Encryption

Create a program that writes encrypted text to a file and can decrypt it when reading back.

Next Lesson

Continue your VB6 journey with Lesson 18: Creating Graphics in VB6.

Related Resources

Full VB6 Tutorial Index

Complete list of all VB6 lessons with descriptions

Explore Tutorials

Visual Basic Examples

Practical VB6 code samples for real-world applications

View Examples

Arrays in VB6

Learn about arrays in VB6

Previous Lesson

Creating Graphics

Learn about graphics creation in VB6

Next Lesson