Download presentation
Presentation is loading. Please wait.
Published byVanessa Harrison Modified over 9 years ago
1
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San Diego
2
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Ch 11: Accessing Data Files Creating DB files for use by VB Data control and data-bound controls Viewing a database table Using list and combo boxes as bound controls Adding a lookup table & navigation Updating a database file
3
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Visual Basic and Database files Database formats supported –dBASE, Excel, FoxPro, Lotus –Paradox, text files –With ODBC: SQL server, Oracle, DB2 Database terminology –File, Table, Row –Column, Record, Field –Key field
4
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Database Terminology File: collection of tables Table: consists of rows and columns Row also known as a record Field (column): data element of a table Key field (primary key): unique identifier for row Current Record: available record
5
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Creating DBs for use by VB Cannot use VB Standard Edition or Learning Edition to create new database You can run the Visual Data Manager add- in that comes with VB –Select Visual Data Manager from the Add-Ins menu –The VisData application window opens
6
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Using the data control Data tool (toolbox): Data control: Data control on form:
7
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Data Control (continued) Set three properties on the Data Control: –Connect-database type (Access) –DatabaseName-name/location of DB –RecordSource-table/query (view) name Set ReadOnly property to True if you want to prevent updates to database Next, you can set up data-bound controls
8
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Data-Bound Control Properties After placing text boxes, option buttons, etc. on the form, you can bind each control to a column (field) of the table or query whose name is in the RecordSource property. Set DataSource-point to Data Control Set DataField-specify Table field
9
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Viewing a Database File Design & create the form Set the properties for the data control Set the properties for the data-bound controls Write the code Run the project
10
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Navigating the database in code Data control provides navigation controls You can hide the data control and provide record movement in code Recordset object is defined by RecordSource property of Data control Tip: use App.Path to set DatabaseName property
11
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Navigating Database (continued) Using the MoveNext, MovePrevious, MoveFirst, and MoveLast methods –Provide Recordset methods to navigate DB Recordset has two handy properties called BOF and EOF BOF set when just before first record or record set is empty EOF set when just after last record
12
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Using List & Combo Boxes as Data- Bound Controls List boxes and combo boxes are data aware Set up a lookup table for a field by using a combo box You can bind the combo box to a database field and give the List property initial values
13
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Lookup Table & Navigation Modify the user interface Change the properties Write the code
14
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Updating a Database File Recordset object has an Update method that updates the underlying database Update occurs automatically when a navigation button is clicked The AddNew method adds records to DB –All bound controls are cleared –Save with Update method or move off the record
15
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Deleting Records Delete method of the Recordset object deletes existing record After a delete operation, move to another record because deleted record is no longer valid With datBooks.Recordset.Delete.Movenext End with
16
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Preventing Errors It is bad form for users to encounter errors Error Trapping intercepts errors before users see them Limit user’s action when an operation starts CancelUpdate method cancels record alteration (add or update) and restores a record to its original form
17
Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Hands on Programming Example
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.