VB Tutor VB.NET 2022 Tutorial VB2019 Tutorial VB6 Tutorial VB Sample Code About Us
Visual Basic Sample Code

A Simple Database System

Created using Visual Basic 6 with VB.NET equivalent


Introduction

This is a simple database management system using a text file. The program demonstrates fundamental file handling operations in Visual Basic 6, including:

File Creation

Create and open text files for data storage

Data Appending

Add new data without overwriting existing content

Data Reading

Retrieve and display stored information

File Deletion

Remove database files when needed

The program also implements robust error handling to manage common file operations errors, such as attempting to read a non-existent file or deleting a file that doesn't exist.

Key Concept: The program uses Append mode instead of Output to preserve existing data when adding new records.

Interactive Demo

Experience how the database system works with this simulated version. The demo uses your browser's local storage to simulate the file operations.

No data in the file. Create a file and add students.

Note: Enter "finish" in the student name field to stop adding students.

Original VB6 Interface

Original VB6 Interface

The original interface of the Simple Database System in VB6

Implementation Code

VB6 Code
VB.NET Code

Visual Basic 6 Implementation

Dim studentname As String
Dim intMsg As String

Private Sub Command1_Click()
    'To read the file
    Text1.Text = ""
    Dim variable1 As String
    On Error GoTo file_error
    Open "D:\Liew Folder\sample.txt" For Input As #1
    Do
        Input #1, variable1
        Text1.Text = Text1.Text & variable1 & vbCrLf
    Loop While Not EOF(1)
    Close #1
    Exit Sub
    
file_error:
    MsgBox (Err.Description)
End Sub

Private Sub Command2_Click()
    'To delete the file
    On Error GoTo delete_error
    Kill "D:\Liew Folder\sample.txt"
    Exit Sub
    
delete_error:
    MsgBox (Err.Description)
End Sub

Private Sub create_Click()
    'To create the file or open the file for new data entry
    Open "D:\Liew Folder\sample.txt" For Append As #1
    intMsg = MsgBox("File sample.txt opened")
    Do
        studentname = InputBox("Enter the student Name or type finish to end")
        If studentname = "finish" Then
            Exit Do
        End If
        Write #1, studentname & vbCrLf
        intMsg = MsgBox("Writing " & studentname & " to sample.txt ")
    Loop
    Close #1
    intMsg = MsgBox("File sample.txt closed")
End Sub

Private Sub Form_Load()
    On Error GoTo Openfile_error
    Open "D:\Liew Folder\sample.txt" For Input As #1
    Close #1
    Exit Sub
    
Openfile_error:
    MsgBox (Err.Description), , "Please create a new file"
    create.Caption = "Create File"
End Sub

Private Sub Command3_Click()
    End
End Sub

VB.NET Equivalent

Imports System.IO

Public Class Form1
    Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
        Try
            Using writer As StreamWriter = File.AppendText("sample.txt")
                MessageBox.Show("File sample.txt opened")
                Dim studentName As String
                Do
                    studentName = InputBox("Enter the student Name or type finish to end")
                    If studentName.ToLower() = "finish" Then Exit Do
                    writer.WriteLine(studentName)
                    MessageBox.Show($"Writing {studentName} to sample.txt")
                Loop
            End Using
            MessageBox.Show("File sample.txt closed")
        Catch ex As Exception
            MessageBox.Show($"Error: {ex.Message}")
        End Try
    End Sub

    Private Sub btnRead_Click(sender As Object, e As EventArgs) Handles btnRead.Click
        Try
            txtOutput.Text = ""
            If File.Exists("sample.txt") Then
                Using reader As StreamReader = New StreamReader("sample.txt")
                    Dim line As String
                    While Not reader.EndOfStream
                        line = reader.ReadLine()
                        txtOutput.Text &= line & vbCrLf
                    End While
                End Using
            Else
                MessageBox.Show("File does not exist")
            End If
        Catch ex As Exception
            MessageBox.Show($"Error: {ex.Message}")
        End Try
    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        Try
            If File.Exists("sample.txt") Then
                File.Delete("sample.txt")
                MessageBox.Show("File deleted successfully")
            Else
                MessageBox.Show("File does not exist")
            End If
        Catch ex As Exception
            MessageBox.Show($"Error: {ex.Message}")
        End Try
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            If File.Exists("sample.txt") Then
                btnCreate.Text = "Add Records"
            Else
                btnCreate.Text = "Create File"
            End If
        Catch ex As Exception
            MessageBox.Show($"Error: {ex.Message}")
        End Try
    End Sub
End Class

Note: The VB.NET implementation uses modern .NET file handling classes and structured exception handling.

How It Works

Key Features

  • File Initialization: Checks if file exists on form load
  • Error Handling: Uses On Error GoTo for robust error management
  • Data Entry: Repeated input boxes for continuous data entry
  • Append Mode: Preserves existing data when adding new records
  • vbCrLf: Ensures proper line formatting in text file

Core Concepts

  • File Operations: Open, Append, Input, Kill
  • Loop Structures: Do...Loop While
  • Conditional Logic: Exit Do when "finish" is entered
  • User Interaction: MsgBox and InputBox
  • Error Handling: Error object with Description property

Exercise: Enhance the Database

Try these enhancements to improve the database system:

  1. Modify the program to store both student names and IDs
  2. Add search functionality to find specific students
  3. Implement the ability to update existing records
  4. Add a confirmation dialog before deleting the file
  5. Convert the system to use a CSV format for better data organization