interacting with databases chapter 10. vb and databases u it is often useful to have a vb program...

12
Interacting with Databases Chapter 10

Upload: clarence-hicks

Post on 28-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

Interacting with Databases

Chapter 10

Page 2: 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

VB and Databases

It is often useful to have a VB program access data stored in a file other than a text file Access, dBaseIV, FoxPro, etc.

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

Page 3: 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

File

Database Structure

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

Table

Table

Record

Record

Record

Record

Record

Record

RecordField 1

Field 2

Field 3

Field 4

Field 5

Page 4: 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

The Data Control

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

Page 5: 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

Using the Data Control

When using the Data Control, you may need to configure following Properties:

Name Caption Connect DatabaseName RecordSource

Page 6: 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

Name, Caption and Connect Name Property

This should start with dat datNavigation

Caption Property Change this user-visible property to something

which describes the data source Browse Student Data

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

Page 7: 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

DatabaseName This property allows you to browse

through the files in the file system In this example, since the Connect

property has been set to Access 2000, the browse window will only display files created with MS Access

Page 8: 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

RecordSource

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

Page 9: 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

Displaying Data Fields

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

Page 10: 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

Contents of the Student Table

Page 11: 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

Using RecordSet Methods 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 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

Page 12: 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

Using RecordSet Methods with Buttons

Private Sub cmdFirst_Click() datNavigation.Recordset.MoveFirstEnd 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