You are on page 1of 2

Simple Database System

VB Code

Database Software

Access Database

Database Program

This is a simple database management system using a text


file. First of all, the program will check whether the text file is
open or not and if the file does not exist, the program prompts
the user to create the file by displaying the create button.
However, if the file is already there, the program will change the
caption of the createbutton to open file. The program uses
Append in the place of Output so that new data will be added to
the end of the file instead of overwriting the old data. The
program will also show the input box repeatedly so that the user
can enter data continuously until he or she enters the word
finish.
The program also introduces the error handler to handle errors
while reading the file or deleting the file because the program
cannot read or delete the file when the file has not been
created. The syntax for error handler is On Error Goto Label
where the label is an error handling sub procedure. For example,
when the program is trying to read the file when the file does not
exist, it will go the label file_error and the error handling object
err will display an error message with its description property
which takes the format err.description. The program uses the
vbCrLf constant when reading the data so that the data will
appear line by line instead of a continuous line. The vbCrLf
constant is equivalent to the pressing of the Enter key (or return
key) so that the next data will go to the newline. The program is
uses the DoLoop to read all the data until it reaches the end
of the file by issuing the command Loop While Not EOF( On the
right is the whole program:

The Code
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 Command3_Click()
End
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

The Interface

Database System

SQL Database

System Management

[Back to Sample VB Programs]


Copyright 2008 Dr.Liew Voon Kiong . All rights reserved |Contact: admin@vbtutor.net
[Privacy Policy]

Example

You might also like