Country Database


This program will access and modify a database file, which holds information about various countries. We'll begin by creating the database file, then creating the Visual Basic interface which accesses that file.

Now we'll set up Country Name as our primary index for the database.

OK! Now we've got a database designed, but with no data in it. Next we'll add actual data.

Note that you can't change the structure of your fields once you've created them in the Visual Data Manager. All you can do is Remove and Add fields.

A country rich in natural resources

The database is now complete! Exit the Visual Data Manager.

Now we'll add the controls we need to access the database.

Form1      Caption        Country Database
Data1      Caption        Countries
           DatabaseName   A:\Country.mdb
           Exclusive      False
           Options        0
           ReadOnly       False
           RecordSource   Country
           EOFAction      2 - Add New
Text1      DataField      Country Name
           DataSource     Data1
Text2      DataField      Capital Name
           DataSource     Data1
Text3      DataField      Population
           DataSource     Data1
Text4      DataField      Comments
           DataSource     Data1
           MultiLine      True
           ScrollBars     2-Vertical
           TabIndex       0
Label1     Alignment      2-Center
           Caption        Country
Label2     Alignment      2-Center
           Caption        Capital
Label3     Alignment      2-Center
           Caption        Population
Label4     Alignment      2-Center
           Caption        Comments

Now start the application, and use the arrow buttons on the Data control to move through the records. When you're satisfied that all the information you entered earlier is now accessible, return to the first record and enter the following text in the Comments field (note how the text wraps):

Filled with lakes, both big and small, Canada could be called the "Lake" country. Canada has more than 40 lakes, each 50 miles long.

Stop the application. Now we'll include the ability to add and delete records from the Visual Basic form.

Now run the program to verify that it's working correctly. Click the New button, and enter the following data:

Country      Japan
Capital      Tokyo
Population   123778000
Comments     An economic powerhouse.
Dim Msg As String
Msg = "Are you sure you want to delete" & Chr$(10)
Msg = Msg & Text1.Text
If MsgBox(Msg, 17, "Delete Record?") = 1 Then
  ' User clicked OK
  ' (MsgBox 17 is Stop icon with OK & Cancel buttons)
  Data1.Recordset.Delete
  Data1.Recordset.MoveNext
  If Data1.Recordset.EOF Then
    Data1.Recordset.MovePrevious
  End If
End If

Run the application. Select the record you just entered, Japan, and click Delete. You can click either OK or Cancel; for our purposes it doesn't matter. If you click OK, Japan will be deleted. If you click Cancel, the Japan record will remain intact.

Extra Improvements:

  1. Add another field to the database, Continent, which can hold the name of the continent on which that country is situated (North America, South America, Asia, Europe, or Africa). Add this field as another item on the form.
  2. Make the Continent field on the form a ComboBox, from which the user can select one of the continents.
  3. Add a second button, and a list box, which displays the result of a query run on the data.

Visual Basic by Example is hosted by GeoCities