Presentation is loading. Please wait.

Presentation is loading. Please wait.

Handling of data from multiple databases. Visual Basic Database Visual Basic application acts as a front-end to the database Visual Basic application.

Similar presentations


Presentation on theme: "Handling of data from multiple databases. Visual Basic Database Visual Basic application acts as a front-end to the database Visual Basic application."— Presentation transcript:

1 Handling of data from multiple databases

2 Visual Basic Database Visual Basic application acts as a front-end to the database Visual Basic application provides the interface between the user and the database interface allows the user to tell the database what he or she needs allows the database to respond to the request displaying the requested information in some manner

3 A Visual Basic application cannot directly interact with a database There are two intermediate components between the application and the database –data control –database engine

4 Data control It is a Visual Basic object It connects the application to the database via the database engine. It is the conduit between the application and the engine, passing information back and forth between the two Database engine It is the heart of a Visual Basic database management system It is the actual software that does the management. Having this engine saves programmers a lot of work. The database engine native to Visual Basic is known as the Jet engine. It is the same engine used by Microsoft Access for database management. Hence, it is primarily used to work with Access databases, but it can also work with others. It requires less code to connect to an existing database View all information within that database modify any and all information within that database Add and delete all information within that database

5 ID No 1 2 3 Bob Jones Mary Rodgers Sue Williams Name Date of Birth 01/04/58 WeightHeight Field Record Table 11/22/61 06/11/57 72 65 68 170 125 130 Simple database

6 Data Access Object (DAO) It is a structure of objects for accessing databases through your code. All the functionality of the Data control is also available to your code, through the DAO. Record Set: Record sets are objects that represent collections of records from one or more tables. You can’t access the tables of a database directly. The only way to view or manipulate records is via RecordSet object. A RecordSet is constructed of columns and rows and is similar to a table. But it can contain data from multiple table. Three types of RecordSets are DynaSets  Which are updatable views of data SnapShots  Which are static( read only) views of data Tables  Which are direct views of tables.

7 Three types of recordsets, established via the RecordsetType property: TableRepresentation of a native database table (not formed via a query). You can add, change, or delete records. DynasetThe default type, a Dynaset is formed as the result of a database query. You can add, change, or delete records from the underlying table(s). This is the most flexible Recordset type. SnapshotA static copy of records that cannot be updated. Used if you are just viewing or searching a database table.

8 DAO Data Control The DAO data control is selected from the Visual Basic toolbox window. It’s icon looks like this: The DAO data control is the primary interface between a Visual Basic application and a database The data control (or tool) can access databases created by other programs besides Visual Basic (or Microsoft Access)

9 The data control can perform the following tasks Connect to a database. Open a specified database table. Create a virtual table based on a database query. Pass database fields to other Visual Basic tools, for display or editing. Such tools are bound to the database, or data bound controls. Add new records, delete records, or update records. Trap any errors that may occur while accessing data. Close the database.

10 DAO Data Control Properties Important properties of this data control are: AlignDetermines where data control is displayed. CaptionPhrase displayed on the data control. ConnectType of database. Default is Microsoft Access (or Jet). DatabaseNameReturns or sets the name of the source database for the data control. Must be a fully qualified path and file name. ExclusiveIndicates whether the underlying database is opened for single- user or multi-user access. ReadOnlyIndicates whether the data can be edited or not. RecordsetA set of records defined by a data control’s Connect, DatabaseName, and RecordSource properties. Run-time only. RecordsetTypeIndicates type of Recordset you want data control to create RecordSourceDetermines the table (or virtual table) the data control is attached to. VisibleEstablishes whether the data control appears on the form at run-time.

11 Move to first record (row) Move to last record (row) Move to next record (row) Move to previous record (row) After placing a DAO data control on a form, set the DatabaseName property first. Then, set the RecordSource property. Recordset Object When we set the RecordSource property (either select a table from the database or form a virtual table via a query) The data control (using the Jet engine) retrieves the needed records and places them in the Recordset object for our use.

12 The relationship between the data control, its two primary properties (DatabaseName and RecordSource), and the Recordset object is:

13 Data Bound Controls To view the information, we use data bound controls that are special controls with properties established by database fields A data bound control is needed for each field (column) in the Recordset (database table) you need to view Standard data bound data controls are: LabelCan be used to provide display-only access to a specified text data field. Caption property is data bound. Text BoxCan be used to provide read/write access to a specified text data field. Probably, the most widely used data bound tool. Text property is data bound. Check BoxUsed to provide read/write access to a Boolean field. Value property is data bound. Picture BoxUsed to display a graphical image from a bitmap, icon, gif, jpeg, or metafile file. Provides read/write access to a image/binary data field. Picture property is data bound. Image BoxUsed to display a graphical image from a bitmap, icon, gif, jpeg, or metafile file (uses fewer resources than a picture box). Provides read/write access to a image/binary data field. Picture property is data bound.

14 Data Bound Control Properties DataChangedIndicates whether a value displayed in a bound control has changed. DataFieldSpecifies the name of a field in the table pointed to by the respective data control. DataSourceSpecifies which data control the control is bound to (indirectly specifying the database table).

15 Follow these steps (in order listed) in placing the controls on a form: Draw the bound control on the same form as the data control to which it will be bound Set the DataSource property. Click on the drop-down arrow to list the data controls on your form. Choose one Set the DataField property. Click on the drop-down arrow to list the fields associated with the selected data control records. Make your choice Set all other properties, as needed

16 The relationships between a data bound control (DataSource and DataField properties) and the DAO data control (Recordset property) are:

17

18 DAO Data Control Events Important DAO data control events: ErrorTriggered when a data access error occurs and Visual Basic code is not being executed. RepositionTriggered after data control pointer moves to a new record. Use to update information from non-data bound controls. ValidateEvent triggered when the pointer is about to move away from the current record. This event can be used to cancel an update of a record or a move to a new record.

19 DAO Data Control Methods important methods. These methods perform certain actions on the data control: RefreshRequeries the database based on contents of the RecordSource property. UpdateControlsRestores the value of bound controls to original values (if no update has been performed). UpdateRecordSaves the values of bound controls to the database without triggering the data control Validate event.

20 DAO Data Control Recordset Properties Important data control Recordset properties are: AbsolutePositionLong integer that either gets or sets the position of the current record. BOFReturns True when the current record is positioned before any data. BookmarkSets or returns a bookmark to the current record. Used as a place marker in database management tasks. EditModeIndicates the state of editing for the current record. EOFReturns True when the current record is positioned past any data. PercentPositionSingle data type that sets or gets the position of the current record as a percentage of total records. Used for status indicators. RecordCountThe total number of records in the Recordset. UpdatableRead-only at run-time. If True, records in the Recordset can be modified. If False, records are read-only.

21 DAO Data Control Recordset Methods Important Recordset methods are: AddNewAdds a new record to the Recordset. All fields are set to null and this record becomes the current record. CancelUpdateUsed to cancel any pending updates (either with Edit or AddNew method) CloseCloses a Recordset. DeleteThe current record is deleted from the Recordset. EditPlaces the current record in the Recordset into edit mode. MoveFirstMoves the current record pointer to the first record in the Recordset. MoveLastMoves the current record pointer to the last record in the Recordset. MoveNextMoves the current record pointer to the next record in the Recordset. MovePrevious Moves the current record pointer to the previous record in the Recordset. RequeryUpdates the data in a Recordset object by re-executing the query on which the object is based. UpdateSaves the current contents of all data bound controls.

22 Private Sub cmdPrevious_Click() datTitles.Recordset.MovePrevious If datTitles.Recordset.BOF Then datTitles.Recordset.MoveFirst End If End Sub Private Sub cmdNext_Click() datTitles.Recordset.MoveNext If datTitles.Recordset.EOF Then datTitles.Recordset.MoveLast End If End Sub

23 DAO Example Dim wstemp As Workspace Dim dbtemp As Database Dim rstemp As Recordset Set wstemp = DBEngine.Workspaces(0) Set dbtemp = wstemp.OpenDatabase("", False, False, "ODBC;DSN=Oracle;USR=scott;PWD=tiger") Set rstemp = dbtemp.OpenRecordset(myquery.Text, dbOpenDynaset, dbSQLPassThrough) howmany = 0 Combo1.Clear Do Until rstemp.EOF msgbox rstemp(0) rstemp.MoveNext howmany = howmany + 1 Loop


Download ppt "Handling of data from multiple databases. Visual Basic Database Visual Basic application acts as a front-end to the database Visual Basic application."

Similar presentations


Ads by Google