VB2017 VB2015 VB2013 VB2012 VB2010 VB2008 VB6 VB Sample Codes 中文VB About Us

A Simple Database System

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,it prompts the user to create the file by displaying the create button.

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 Labelwhere 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 Do...Loop 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
Input #1, variable1
Text1.Text = Text1.Text & variable1 & vbCrLf
Loop While Not EOF(1)
Close #1

Exit Sub
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
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")
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 ")
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
MsgBox (Err.Description), , "Please create a new file"
create.Caption = "Create File"
End Sub

Private Sub Command3_Click()
End Sub

The Interface


Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy