Created using Visual Basic 6 with VB.NET equivalent
This is a simple database management system using a text file. The program demonstrates fundamental file handling operations in Visual Basic 6, including:
Create and open text files for data storage
Add new data without overwriting existing content
Retrieve and display stored information
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.
Experience how the database system works with this simulated version. The demo uses your browser's local storage to simulate the file operations.
The original interface of the Simple Database System in VB6
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
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
On Error GoTo for robust error managementTry these enhancements to improve the database system: