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.
A:\Country in the File Name text box to save the database, using the name Country, on your floppy disk. Click the Save button. In VisData, two windows will appear: "Database Window" and "SQL Statement"
Field Name Field Type Field Size ----------------------------------------- Capital Name Text 20 Population Long Comments Memo
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.
Country Name Capital Name Population ------------------------------------------- France Paris 55632000 Germany Berlin 78420000 Great Britain London 57142000 Mexico Mexico City 81163000
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):
Stop the application. Now we'll include the ability to add and delete records from the Visual Basic form.
Data1.Recordset.AddNewText1.SetFocus |
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: