Tutorial 91 Databases A database is an organized collection of related information stored in a file on a disk A database allows companies to store information about employees, customers, inventory, and so on
Tutorial 92 Visual Basic and Databases You can use Visual Basic to access databases created by many popular software packages Visual Basic allows a company to use one interface to access all of a company’s data You can use Visual Basic to create a Microsoft Access database because both Visual Basic and Microsoft Access contain the Jet engine
Tutorial 93 Relational Databases A relational database stores data in tables, which are composed of columns and rows Each column in the table represents a field and each row in the table represents a record
Tutorial 94 Field, Record, Table, Relational Database Field – single item of information about a person, place, or thing Record – group of related fields that contain all of the information about a person, place, or thing Table – group of related records; each record contains the same fields Relational database – one or more tables
Tutorial 95 One-table Relational Database
Tutorial 96 Two-table Relational Database The records are related by the CD number
Tutorial 97 Advantages of Relational Databases Provide easy and quick retrieval of information Allow you to display the data in any order Allow you to control how much of the data (which fields and records) are displayed at any one time
Tutorial 98 Five Design Steps Identify the information the user will need from the database Organize the information into one or more tables Establish relationships between tables (if the database contains more than one table) Create one or more indexes, if necessary Define data validation rules, if necessary
Tutorial 99 Index A special table that is created and maintained by Visual Basic Allows you to arrange the records in a specific order, and also quickly search for information Create an index only for fields on which you plan either to order or to search
Tutorial 910 Index For each record, the index stores two fields Index field – stores values contained in the field being indexed Pointer field – stores a number that represents (points to) the location of the record in the table
Tutorial 911 Index and Pointer Fields
Tutorial 912 Data Validation Data validation refers to the process of ensuring that the data in the database meets certain criteria A data validation rule places restrictions on the data entered in a field
Tutorial 913 Summary Chart Table name:tblPatron Field names:fldSeat, fldName, fldPhone Relationships:None Index: Create an index named indSeat for the fldSeat field Validation rule: The fldSeat field must contain a number that is greater than or equal to 1 and less than or equal to 48
Tutorial 914 Visual Data Manager Application that comes with Visual Basic Use to create a Microsoft Access database, including indexes and data validation rules Click Add-Ins on the Visual Basic menu bar, then click Visual Data Manager, and then open a new or existing Microsoft Access 7.0 database
Tutorial 915 Visual Data Manager
Tutorial 916 Table Structure Dialog Box
Tutorial 917 Add Field Dialog Box
Tutorial 918 Fields Shown in the Database Window
Tutorial 919 Add Index Dialog Box
Tutorial 920 Validation Rules
Tutorial 921 Dynaset Window
Tutorial 922 Find Record Dialog Box
Tutorial 923 Like Operator and * Wildcard
Tutorial 924 UDA, OLE DB, ADO UDA – Microsoft’s approach to solving the data access problem OLE DB – provides uniform access to data stored in a variety of formats; Visual Basic programmers gain access to the complex OLE DB interfaces through ADO ADO – allows you to access the OLE DB interfaces
Tutorial 925 ADO Data Control Microsoft’s ADO (ActiveX Data Object) data control can access the data stored in a variety of formats, such as a database, a spreadsheet, or even a text file The ADO data control establishes a link between the database and the other controls in the application’s interface
Tutorial 926 ADO Data Control
Tutorial 927 Provider Tab
Tutorial 928 Connection Tab
Tutorial 929 General Tab
Tutorial 930 RecordSource Tab
Tutorial 931 Binding Controls Connecting a control to an ADO data control is called binding, and the connected controls are referred to as data-aware controls or bound controls Any control that has a DataSource property can be bound to an ADO data control
Tutorial 932 Binding Controls Set the control’s DataSource property to the name of the ADO data control Set the control’s DataField property to the name of a field in the database Always set the DataSource property before setting the DataField property
Tutorial 933 SQL Stands for Structured Query Language A set of commands that allow you to access and manipulate the data stored in many database management systems on computers of all sizes SQL commands can be used to store, retrieve, update, and sequence data
Tutorial 934 SQL Select Statement select fields from table [where condition] [order by field] Fields is one or more field names, separated by commas Table is the name of the table containing the fields Where condition clause allows you to limit the records that appear when displayed Order by field clause allows you to control the order in which the records appear when displayed
Tutorial 935 Select Command Example
Tutorial 936 Select Command Example
Tutorial 937 Where Clause Examples To view seat 24’s record, select * from tblPatron where fldSeat = 24 To view all records, in seat number order, whose seat numbers are greater than 20, select * from tblPatron where fldSeat > 20 order by fldSeat To view Candy Sprott’s record, select * from tblPatron where fldName = “sprott, candy” To view all records whose phone number begins with 333, select * from tblPatron where fldPhone like “333%”
Tutorial 938 RecordSet and Field Objects and the Fields Collection Recordset object - the collection of records selected by the ADO data control’s RecordSource property Each field contained within the Recordset object is referred to as a Field object The collection of Field objects within a Recordset object makes up the Fields collection To refer to a field within the Fields collection: [form.]adocontrol.Recordset.Fields(field)
Tutorial 939 Recordset and Field Objects and the Fields Collection The select * from tblPatron command will create the following Recordset object, Field objects, and Fields collection: Recordset object: All records stored in the tblPatron table Fields collection: fldSeat, fldName, fldPhone 3 Field objects
Tutorial 940 Methods of the Recordset Object AddNewAdd a new, blank record to the end of the recordset CancelUpdateCancel changes made to the current record DeleteDelete the current record from the recordset MoveFirstMove the record pointer to the first record in the recordset MoveNextMove the record pointer to the next record in the recordset UpdateSave the changes made to the current record
Tutorial 941 Recordset Object’s EOF Property EOFTest for the end of the recordset The EOF property contains True if the record pointer is positioned after the last record in the recordset; otherwise, the property contains False
Tutorial 942 Syntax of Recordset Object’s Methods and Properties The syntax for invoking a method of the Recordset object is: [form.]adocontrol.Recordset.method The syntax for referring to a property of the Recordset object is: [form.]adocontrol.Recordset.property.
Tutorial 943 ADO Data Control’s Refresh Method adocontrol.Refresh To create a new Recordset object while an application is running, you need first to enter, into a code window, an instruction that resets the ADO data control’s RecordSource property. You then need to use the ADO data control’s Refresh method to reopen the database and recreate the Recordset object
Tutorial 944 Visible vs Enabled If the application never allows the user to interact with the ADO data control, then you should hide the control If the application allows the user to interact with the ADO data control when a certain condition is met, then you should disable the control and enable it when the condition is met
Tutorial 945 Debugging Technique The Debug menu’s Step Into command allows you to execute the application’s code one statement at a time You also can use the Step Into button on the Debug toolbar to access the Step Into command; or you can press the F8 key on your keyboard