A Simple Database Management System

 

 

Yesterday I have discussed a database management system using  a database file created in Microsoft Access. Today I will show you how to create a simple database system without using the MS Access file, you simply create a text file that can be updated from your program.

To create a text file, you can use the following command

Open "fileName" For Output  As #fileNumber

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

For example:

Open "c:\My Documents\sample.txt" For Output As #1

will create a text file by the name of sample.txt in the My Document folder. The accompanying file number is 1. If you wish to create and save the file in drive A, simply change the path, as follows"

Open "A:\sample.txt" For Output As #1

If you wish to create a HTML file, simple change the extension to .html

Open "c:\My Documents\sample.html" For Output As # 2

 

To read a file , you can use the input # statement. The syntax is shown below:

Open "fileName" For Input As #1

You have to open the file according to its file number and the variable that holds the data. You also need to declare the variable using the DIM command.

 

Example 1: To Create a File

Example 2: To Read a File

Private Sub create_Click ()

Dim intMsg As String
Dim StudentName as String

Open "c:\My Documents\sample.txt" For Output As #1
intMsg = MsgBox ("File sample.txt opened")
StudentName = InputBox ("Enter the student Name")
Print #1, StudentName
intMsg = MsgBox ("Writing a" & StudentName & ^to sample.txt ")

Close #1

          intMsg = MsgBox ("File sample.txt closed")
End Sub

 

Private Sub Reading_Click ()

Dim variable1 As String
Open "c:\My Documents\sample.txt" For Input As #1
Input #1, variable1
Text1.Text = variable1
Close #1

End Sub

*More examples will be shown tommorrow.

 

 [Back to VBToday]