Interacting with Databases Chapter 10
VB and Databases u It is often useful to have a VB program access data stored in a file other than a text file Access, dBaseIV, FoxPro, etc. u Such files, called databases have a special, complex structure which supports multiple data sets The sequential file access methods we've learned will not help us access these files
File Database Structure u Databases are a collection of data tables (like files) Tables are sets of records Records are sets of fields Fields are variable types All records in the same table contain the same fields Database Table Record Field 1 Field 2 Field 3 Field 4 Field 5
The Data Control u VB provides a special control, the Data Control, for accessing database content The Data Control can also be used with Excel files record-based text files Go to first record Go to previous record Caption Go to next record Go to last record
Using the Data Control u When using the Data Control, you may need to configure following Properties: Name Caption Connect DatabaseName RecordSource
Name, Caption and Connect u Name Property This should start with dat datNavigation u Caption Property Change this user-visible property to something which describes the data source Browse Student Data u Connect Property The connect property specifies the type of data source you are using A table from a database (several formats) An Excel spreadsheet A text file with record-based data
DatabaseName u This property allows you to browse through the files in the file system u In this example, since the Connect property has been set to Access 2000, the browse window will only display files created with MS Access
RecordSource u This property allows you to select one table from the set of all tables inside the database Select a table Now the Data Control is linked to the given table of the database The linked table is know as the RecordSet An Excel worksheet can also be used as the source for a RecordSet
Displaying Data Fields u To view/alter the contents of the RecordSet, you must associate each field with a data-aware control on the form A label or textbox can be used as a data-aware control Data-aware controls have DataSource and DataField properties To connect a data-aware control to a particular field of the RecordSet Change DataSource property of the control to the name of a Data Control Change DataField of the control to name of some field in the RecordSet
Contents of the Student Table
Using RecordSet Methods u Here are some methods that you can use to move the record pointer within the RecordSet (table) datStudent.RecordSet.MoveFirst Move to the first record datStudent.RecordSet.MoveLast Move to the last record datStudent.RecordSet.MoveNext Move to the next record datStudent.RecordSet.Previous Move to the previous record u If you use these in button code instead of the arrows on the Data Control, it probably makes sense to make the Data Control invisible
Using RecordSet Methods with Buttons Private Sub cmdFirst_Click() datNavigation.Recordset.MoveFirst End Sub Private Sub cmdNext_Click() datNavigation.Recordset.MoveNext End Sub Private Sub cmdPrev_Click() datNavigation.Recordset.MovePrevious End Sub Private Sub cmdLast_Click() datNavigation.Recordset.MoveLast End Sub