Create a Simple
Text Database

(Advantage: file size
and easy creation)

by Rick Meyer

If you click on the tab in the connect property for a data control there is a drop down list of all the types of databases you can connect to. Way down at the bottom is Text;. There is little documentation to assist you with this choice. Surprisingly it allows you to manipulate simple text files like you would a database.

The immediate advantage is the size of the database. Where most database sizes start at around 50,000 bytes, this database may be only a few hundred bytes! This may be desirable when you wish to backup, copy, or send it over the internet.

Text Databases are also referred to as Comma Separated Values or CSV.

There is one great drawback to Text Databases. Although you can easily add new records, you can not edit previous records.
One fundamental concept to understand is that your text file will be a single table. The second thing is that the first line of the file will describe the fields. Each subsequent line of the file will be a record with fields (corresponding to the first line describing the fields).

Accordingly, apply these princlples and create a database with Notepad **

1. Open Notepad.exe
2. Type the following on the first line:    "FirstName","LastName","PhoneNo"
3. Press the Enter key so the cursor goes to the second line.
4. Save As phoneno.txt in your VB directory.
You have just created the database! It will have three fields for you to keep track of phone numbers for people. If you wanted to create an Access database, you would need to own a copy of Access or use rather complicated code such as in Demo 6. This is the other advantage of a simple text database.
      Now follow these steps:

1. Start a new standard exe.
2. On Form1 put a data control named Data1.
3. On Form1 put 3 Textboxes.

      Form1 should look like the picture above.

Now go to the Properties Window and select Data1. Find the property called Connect. Click the tab and choose Text; at the bottom.

Next find the property named DatabaseName. When you click the tab you will be allowed to browse to find a database. Choose the phoneno.txt file that you just saved with Notepad.

Finally find the property named EOFAction. Click the tab and choose 2 - Add New.

Now connect each Textbox to Data1.

To do this find the DataSource property of each Textbox. Click on the tab and choose the Data1 control from the dropdown list.

Now connect each Textbox with a field in the database.

To do this find the DataField property of each Textbox. Click the tab for each Textbox and choose one of the three fields for each.

That is all there is to it! You don't have to write any code!
  • Run the program
  • Click on the right arrow tab of the Data1 control after you have entered a name and number.

  • Click the left arrow to view previous records.
  • All information will be automatically saved to the text file!
  • ** You can also create the same database with the following code:
    Private Sub createTxt()
        Dim f%, fname$
        
        fname = "phoneno.txt"
        If Dir$(fname) <> "" Then Exit Sub
        f = FreeFile
        
        Open fname For Output As f
        Write #f, "FirstName", "LastName", "PhoneNo"
        Close f
    End Sub