McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 12 Advanced Data Handling.

Slides:



Advertisements
Similar presentations
Basics of Database Programming with VB6
Advertisements

Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control.
Chapter 10 Database Applications Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Tutorial 3 Queries and Table Relationships
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
Chapter 10 Accessing Database Files Programming In Visual Basic.NET.
XP Chapter 3 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Analyzing Data For Effective Decision Making.
Creating Custom Forms. 2 Design and create a custom form You can create a custom form by modifying an existing form or creating a new form. Either way,
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Using Objects and Properties
Copyright 2003 : Ismail M.Romi, PPU. All Rights Reserved 1 Lab10 Interacting with Data (Database Management)
1 Pertemuan 09 Database Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Chapter 3 Maintaining a Database
CHAPTER 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
Copyright © 2010 Pearson Education, Inc. Publishing as Prentice Hall 1 1. Chapter 2: Relational Databases and Multi-Table Queries Exploring Microsoft Office.
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Unit J: Creating a Database Microsoft Office Illustrated Fundamentals.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. ACCESS 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 4 – Creating New.
ASP.NET Programming with C# and SQL Server First Edition
Handling of data from multiple databases. Visual Basic Database Visual Basic application acts as a front-end to the database Visual Basic application.
XP New Perspectives on Microsoft Access 2002 Tutorial 51 Microsoft Access 2002 Tutorial 5 – Enhancing a Table’s Design, and Creating Advanced Queries and.
Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid.
DAY 15: ACCESS CHAPTER 2 Larry Reaves October 7,
Microsoft Access Intro Class 5 Working with Queries.
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
DAY 14: ACCESS CHAPTER 1 Tazin Afrin October 03,
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 11 Accessing Database.
McGraw-Hill Technology Education © 2004 by the McGraw-Hill Companies, Inc. All rights reserved. Office Access 2003 Lab 3 Analyzing Data and Creating Reports.
Analyzing Data For Effective Decision Making Chapter 3.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. ACCESS 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 6 – Designing.
Copyright © 2001 by Wiley. All rights reserved. Chapter 9: Introduction to Working with Databases in Visual Basic Database Concepts Relational Database.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
Lesson 2.  To help ensure accurate data, rules that check entries against specified values can be applied to a field. A validation rule is applied to.
Microsoft Access 2000 Presentation 2 Creating Databases Part I (Creating Tables)
McGraw-Hill/Irwin © 2008 The McGraw-Hill Companies, All Rights Reserved Plug-In T8: Decision Making Using Access 2007 Business Driven Technology.
McGraw-Hill/Irwin The O’Leary Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Access 2002 Lab 2 Modifying a Table and Creating.
Chapter 17 Creating a Database.
Microsoft Office 2007 Access Chapter 3 Maintaining a Database.
SESSION 3.1 This section covers using the query window in design view to create a query and sorting & filtering data while in a datasheet view. Microsoft.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. WORD 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 15 Advanced Tables.
BUSINESS DRIVEN TECHNOLOGY Plug-In T5 Touring Access.
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.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. a lesson approach Microsoft® Excel 2010 © 2011 The McGraw-Hill Companies,
Databases,Tables and Forms Access Text by Grauer Chapters 1 & 2.
Microsoft Access. Microsoft access is a database programs that allows you to store retrieve, analyze and print information. Companies use databases for.
ACCESS Part 2. OBJECTIVE  Use the Query Wizard.  Work with data in a query.  Use Query Design View.  Sort and find data.  Filter data Apply AND criteria.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
Database Applications – Microsoft Access Lesson 4 Working with Queries 36 Slides in Presentation.
McGraw-Hill Technology Education © 2004 by the McGraw-Hill Companies, Inc. All rights reserved. Office Access 2003 Lab 2 Modifying a Table and Creating.
Notes on ADO from other projects Please use speaker notes for additional information!
CHAPTER 1 – INTRODUCTION TO ACCESS Aliya Farheen March 5, 2014.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
© 2001 ComputerPREP, Inc. All rights reserved. Access 2000: Module II.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
XP New Perspectives on Microsoft Office Access 2003, Second Edition- Tutorial 6 1 Microsoft Office Access 2003 Tutorial 6 – Creating Custom Forms.
DATABASE SYSTEM VB LINK TO MS-SQL. 2 Visual ProgrammingChapter 6 Select Project -> Component.
ADO and DataList, DataCombo and DataGrid Controls Please use speaker notes for additional information!
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Building a User Interface with Forms
Developing Forms and Subforms.
Microsoft Office Access 2003
Microsoft Office Access 2003
Introduction to Access
Working With Databases
Chapter 10 Accessing Database Files
Presentation transcript:

McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 12 Advanced Data Handling - Grids, Validation, Selection, and Sorting

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 2 McGraw-Hill/Irwin DataGrids - Microsoft DataGrid Control 6.0 (dbg prefix) Presents the table data in rows and columns like a spreadsheet or datasheet view in Access In order to use the datagrid with ADO, you must first add the Microsoft ADO Data Control 6.0 (adodc) to the toolbox –Project, Components, Microsoft ADO Data Control 6.0 Then add the datagrid itself to the toolbox –Project, Components, Microsoft DataGrid Control 6.0

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 3 McGraw-Hill/Irwin DataGrids - Microsoft DataGrid Control 6.0 (dbg prefix) cont. Draw the ADODC on your form Set the properties of the ADODC to point to the database –Name, ConnectionString, CommandType, RecordSource Draw the DataGrid on your form Link the DataGrid to the ADODC using the DataSource property Right Click the DataGrid, Retrieve Fields To modify, Right Click, Edit, then select columns for deletion or resize columns

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 4 McGraw-Hill/Irwin Accessing DataGrid Properties Some properties are available in the Properties window More properties are available from the Property Pages –Properties Window, Custom –OR, Right Click the DataGrid and select Properties

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 5 McGraw-Hill/Irwin DataGrid - Property Pages Properties AllowAddNew –Allow user to add new database records from the grid AllowDelete –Allow user to delete database records from the grid Row height Column width

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 6 McGraw-Hill/Irwin Record Count and Record Number RecordCount property of the recordset holds the number of records in the recordset AbsolutePosition property of the recordset hold the position of the current record in the recordset –AbsolutePosition=0 at BOF and EOF intRecordCount=adoBooks.Recordset.RecordCount intCurrentRecord=adoBooks.Recordset.AbsolutePosition

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 7 McGraw-Hill/Irwin Displaying Record Info Some programmers display the record info in the Caption of the ADODC To display the record number as the user moves from record to record use the ADODC's MoveComplete event which occurs each time a new record becomes current

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 8 McGraw-Hill/Irwin Displaying Record Info -SetRecordNumber Subprocedure Private Sub SetRecordNumber( ) Dim intCurrentRecord as Integer Dim intRecordCount as Integer With adoBooks.Recordset intCurrentRecord =.AbsolutePosition intRecordCount =.RecordCount End With If adoBooks.Recordset.EOF Then adoBooks.Caption = "EOF" Else adoBooks.Caption="Record " & intCurrentRecord & _ " of " & intRecordCount End IF End Sub

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 9 McGraw-Hill/Irwin MoveComplete Event -Calling SetRecordNumber Private Sub adoBooks_MoveComplete(ByVal adReason As _ ADODB.EventReasonEnum, _ ByVal pError as ADODB.Error, _ adStatus as ADODB.EventStatusEnum, _ ByVal pRecordset as ADODB.Recordset) SetRecordNumber End Sub

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 10 McGraw-Hill/Irwin Preventing User Errors Set TextBox's MaxLength equal to database field length Use TextBox's Lock property when you don't want the user to change data, like primary key when not adding a record Validate user data BEFORE updating the database

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 11 McGraw-Hill/Irwin Validating Data-Validate Event Use Validate event to perform data validation CausesValidation property=True will invoke the Validate event just before the control loses focus (default is True!) Always set CausesValidation to False for Cancel command button to allow the user "a way out"

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 12 McGraw-Hill/Irwin Validate Event Example Private Sub txtEmpID_Validate(Cancel as Boolean) If Not IsNumeric(txtEmpID) Then MsgBox "Employee Id must be numeric", vbOKOnly,"Invalid Emp ID" With txtEmpID.SelStart = 0.SelLength = Len(.Text) End With Cancel =True'resets focus to txtEmpID End If End Sub

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 13 McGraw-Hill/Irwin Trapping Errors with On Error Include error handling for every subprocedure that accesses the database Open Add Delete Update Move Trap errors and inform user

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 14 McGraw-Hill/Irwin Handling Errors on Moves Code On Error Resume Next to ignore the error and continue execution Private Sub cmdFirst_Click( ) On Error Resume Next adoBooks.Recordset.MoveFirst End Sub

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 15 McGraw-Hill/Irwin Handling Errors On Delete Notify user using MsgBox Code On Error GoTo 0 to turn off error trapping and return control to the user

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 16 McGraw-Hill/Irwin Handling Errors On Delete Example Private Sub cmdDelete_Click( ) On Error GoTo ErrorHandler With adoBooks.Recordset.Delete.MoveNext... see P496 for complete code ErrorHandler: Dim strMsg as String strMsg="Cannot delete record." & vbCrLf & Err.Description MsgBox strMsg, vbExclamation, "Database Error" On Error GoTo 0 End Sub

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 17 McGraw-Hill/Irwin Handling Errors on Add Depending on the thoroughness of your data validation in the Validate event various errors can be generated during Add Most common error is duplicate, invalid, or missing primary key Error handling code will vary depending on what you want to do as a programmer –Give the user another chance to correctly enter data –Abort add

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 18 McGraw-Hill/Irwin Handling Errors on Add Example Review textbook example –Pages –Error: primary key error, ISBN field –Error Handling: allows the user to try again to enter a valid primary key

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 19 McGraw-Hill/Irwin Searching for Records Find Method –Returns single record matching specified criteria Filter Property –Returns a subset a records that match the specified criteria

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 20 McGraw-Hill/Irwin Find Method Syntax adoDatControl.Recordset. Find Criteria [, [RowsToSkip], [SearchDirection] ] Criteria: String to specify what to search for RowsToSkip: Optional, specify number of rows/records to skip from current record before starting to search SearchDirection: Optional –adSearchForward (default) –adSearchBackward

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 21 McGraw-Hill/Irwin Find Method Examples adoMembers.Recordset.Find " Amount > 100" adoMembers.Recordset.Find " Amount > " & txtAmt.Text Dim strCriteria as String strCriteria = " [Last Name] = 'Weldon' " adoPatient.Recordset.Find strCriteria Important Rules: 1.Strings must be enclosed in double quotes, strings inside strings must be enclosed in single quotes 2.Database field names that contain spaces must be enclosed in square brackets

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 22 McGraw-Hill/Irwin Search String Examples Field name does not contain spaces –"Sex = ' M ' " If field name contains spaces add square brackets –"[First Name] = ' Smith ' " If data searched for is numeric single quotes are omitted –" Age < 21"

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 23 McGraw-Hill/Irwin More Search String Examples If data searched for is in a textbox –"[Patient Number] = ' " & txtPatNum.Text & " ' " If data searched for is a date use # symbols –" Birthdate > # 1/1/1980 # " Wild Cards –"[Last Name] Like ' S* ' "

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 24 McGraw-Hill/Irwin Preparing for "No Record Found" Set a Bookmark before beginning Find so you can return to the record which was current before the find if no record found If no record is found the recordset will be at EOF Always test for EOF after Find method

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 25 McGraw-Hill/Irwin Bookmarks Store in variable of variant data type (prefix vnt) Store before Find method in the variable by using the Recordset's Bookmark property to save the current record If no record found, set the Recordset's Bookmark property equal to the previously stored variable

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 26 McGraw-Hill/Irwin Find with Bookmark Example Private Sub cmdFind_Click( ) Dim strCriteria as String Dim vntCurrentRecord as Variant If txtLastName "" Then strCriteria="[Last Name] = ' " & txtLastName & " ' " With adoPatient.Recordset vntCurrentRecord=.Bookmark.MoveFirst.FindNext strCriteria If.NoMacth=True Then MsgBox "Record Not Found", vbInformation, "Find".Bookmark=vntCurrentRecord End If End With Else MsgBox "Enter a name to be found" txtLastName.SetFocus End Sub

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 27 McGraw-Hill/Irwin Filter Property Creates a new temporary recordset by selecting all records than match the specified criteria No changes are made to the underlying table

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 28 McGraw-Hill/Irwin Filter Property Syntax Object.Recordset.Filter = Criteria Criteria: String to specify what to search for Use the same rules for constructing the criteria string as shown on previous slide for Find method

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 29 McGraw-Hill/Irwin Removing the Filter Remove the filter to display entire recordset Set Filter property to adFilterNone –adoBooks.Recordset.Filter=adFilterNone

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 30 McGraw-Hill/Irwin Sort Property You can sort a recordset by any field using the recordset's Sort property You can sort by multiple fields by separating them with commas Create a menu or a listbox for user to select field(s) to sort by To return recordset's default sort order usually sort by the primary key

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 31 McGraw-Hill/Irwin Sort Property Syntax & Examples adoBooks.Recordset.Sort = "Author" adoBooks.Recordset.Sort = "Author, Title" adoBooks.Recordset.Sort = "ISBN"

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 32 McGraw-Hill/Irwin Working with Database Fields If your controls ever need to appear empty you can write code to work directly with the database fields If user needs to pick from a list of values and based on user's selection other controls are automatically populated Gives you more programmatic control

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 33 McGraw-Hill/Irwin The exclamation point is called the BANG operator. Referring to Database Fields All these are equivalent: Field Names containing spaces - adoAVB.Recordset![Last Name] adoAVB.Recordset!("Last Name") adoAVB.Recordset!"Last Name" Field Names without spaces - adoAVB.Recordset!Insurance adoAVB.Recordset!("Insurance")

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 34 McGraw-Hill/Irwin The exclamation point is called the BANG operator. Code Examples Populating a form's textbox control txtLName=adoAVB.Recordset![Last Name] Writing to the database field from a form's textbox control adoAVB.Recordset![Last Name]=txtLName

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 35 McGraw-Hill/Irwin Populating a Combo or List (p508) Use Do Until.EOF for the Recordset during Form_Load or Form_Activate Private Sub Form_Load ( ) With adoAVB.Recordset Do Until.EOF If !Lname <> " " Then cboLName.AddItem !LName End If.MoveNext Loop.Close End With End Sub

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 36 McGraw-Hill/Irwin Data Environment Designer Not available in the Working Model-new in VB 6 Must be added - Project, Add Data Environment Visual Interface that allows programmers to set up all the database connections and recordsets in one location After the connections and recordsets are setup, the programmer can simply drag objects from the DE designer to a form to generate data bound-controls automatically

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 37 McGraw-Hill/Irwin Data Environment (de prefix) The Data Environment has a hierarchical interface –The Data Environment is at the top of the hierarchy, used instead of the ADO control –The Data Environment is made up of Connections, used instead of the ConnectionString of the ADO control –Connections are made up of Commands, used instead of the RecordSource –Commands are made up of Fields

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 38 McGraw-Hill/Irwin Data Environment - graphically deAdvancedVision PatientNumber conAVB Patient LastName Street FirstName DataEnvironment (like ADODC) DEConnection (like ConnectionString) DECommand (like RecordSource) Fields

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 39 McGraw-Hill/Irwin Steps For Using DE, 1 of 5 Several possible methods for adding DE, use one –Project Menu, Add Data Environment –Project, Add ActiveX Designers, Data Environment –Project, Components, Designers tab, Data Environment

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 40 McGraw-Hill/Irwin Steps For Using DE, 2 of 5 (cont.) Once the DE has been added, the DE window appears Rename DE by clicking Rename Connection by clicking Set Connection properties by right clicking to access properties window –Data Link Properties Provider Connection

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 41 McGraw-Hill/Irwin Steps For Using DE, 3 of 5 (cont.) Add Command object by right clicking Connection Rename Command by clicking Set Command properties by right clicking to access properties window –CommandType ==> adCmdTable –CommandText ==> actual table name

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 42 McGraw-Hill/Irwin Steps For Using DE, 4 of 5 (cont.) Add Command object by right clicking Connection Rename Command by clicking Set Command properties by right clicking to access properties window –CommandType ==> adCmdTable –CommandText ==> actual table name

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 43 McGraw-Hill/Irwin Steps For Using DE, 5 of 5 (cont.) View list of fields in table by clicking plus symbol beside command object If necessary, resize/move DE window so that you can see both the DE window and form Create bound controls on form by dragging fields from DE to form

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 44 McGraw-Hill/Irwin Recordset Object When using the DE Designer the Command object automatically creates a Recordset object named as follows: –Prefix of "rs" + Command object's name –Ex: Command object name=Patient, Recordset name used in code is rsPatient Reference in code: –deAVB.rsPatient.property or method –Example: deAVB.rsPatient.MoveNext

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 45 McGraw-Hill/Irwin Simple Code Comparison: ADODC and DE ADODC: adoPatient.RecordSet.MoveNext DE: deAdvancedVision.rsPatient.MoveNext

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 46 McGraw-Hill/Irwin Open a Database (DE) VB automatically handles the opening of a database using the Data Environment Designer VB uses the hard-coded connection string of the DE's Connection Object Your VB Project is not portable unless you remove the hard-coded connection string and add code using App.Path to create the connection string, just like ADODC

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 47 McGraw-Hill/Irwin Using App.Path with DE to Open the Database Use DE's Initialize event (rather than Form_Load event as with ADODC) –Initialize Event fires before any connections are made –Code the Connection object's ConnectionString (before we coded the ADODC's ConnectionString) Private Sub DataEnvironment_Initialize( ) conAVB.ConnectionString="Provider=Microsoft.Jet.OLEDB.3.51;" & _ "Persist Security Info=False;" & _ "Data Source=" & App.Path & "\AVB.mdb" End Sub

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 48 McGraw-Hill/Irwin conAVB.Close Close a Database (DE) Use the Close method of the DE's Connection object to close a database Place this code in the Data Environment's Terminate event (rather than in a form's Unload event as with ADODC)

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 49 McGraw-Hill/Irwin Private Sub DataEnvironment_Terminate( ) conAVB.Close End Sub Closing the Database Example Use DE's Terminate event (rather than Form_Unload event as with ADODC)

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 50 McGraw-Hill/Irwin One-to-Many Relationships, Data Hierarchies (Pages ) Use the DE Designer to create hierarchies of data Similar to relationships that join tables Data hierarchies are useful for displaying One-to-Many (1-M) relationships Use MSHFlexGrid control to display the hierarchies created in the DE Designer –Project, Components, Microsoft Hierarchical FlexGrid Control 6.0 (OLE DB)

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 51 McGraw-Hill/Irwin Creating a Relation Hierarchy Create DE as described previously Add the Command objects for –Parent table (One side of 1-M) –Child table (Many side of 1-M) Right click Child table to access Properties –Relation tab Check Relate to a Parent Command Object Select parent table's command object For Relation Definition, select fields in each table upon which the relationship can be established Click Add command button

Programming in Visual Basic 6.0 Update Edition © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 52 McGraw-Hill/Irwin Displaying the Relation Drag the Parent Command Object's fields to the top of the form Add the MSHFlexGrid –Project, Components, Microsoft Hierarchical FlexGrid Control 6.0 (OLE DB) Set the properties of the MSHFlexGrid to display the Child Command Object's fields