Component 8, Slide 1 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton CP2030 Visual Basic For C++ Programmers.

Slides:



Advertisements
Similar presentations
Basics of Database Programming with VB6
Advertisements

© 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 10 – Designing.
Chapter 18 - Data sources and datasets 1 Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San.
XP Chapter 3 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Analyzing Data For Effective Decision Making.
The ADO Data Control. Universal Data Access Open Database Connectivity (ODBC) –standard for accessing data in databases OLE-DB –allows access to data.
ActiveX Data Object ISYS 562. ADO An ActiveX control ActiveX is build upon COM, a contract that defines a standard interface by which objects communicate.
VBA Data Access Object. Data Access Objects DAO With DAO we can: –Run queries –Update values in database tables –Create structure of databases Tables,
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
Visual Basic Database Access BICS546. Microsoft Universal Data Access OLE DB: The OLE database protocol –Allows a program to access information in any.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
CIS 338: Using DAO (Data Access Objects) Dr. Ralph D. Westfall February, 2003.
1 Chapter 8 Object-Based Programming in VBA. 8 Chapter Objectives Declare and use object variables Create procedures that use built-in form methods Find.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
© 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 3 – Finding, Filtering,
Chapter 8 Relational Databases ActiveX Database Controls 8 Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall, Inc. By Carlotta Eaton.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
Handling of data from multiple databases. Visual Basic Database Visual Basic application acts as a front-end to the database Visual Basic application.
Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
Tutorial 11 Using and Writing Visual Basic for Applications Code
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Access 2010 by Robert Grauer, Keith.
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San.
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.
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 11 Accessing Database.
LiveCycle Data Services Introduction Part 2. Part 2? This is the second in our series on LiveCycle Data Services. If you missed our first presentation,
Analyzing Data For Effective Decision Making Chapter 3.
Copyright © 2001 by Wiley. All rights reserved. Chapter 9: Introduction to Working with Databases in Visual Basic Database Concepts Relational Database.
© 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 9 – Building Links,
Lecture Note 10: Simple Database Techniques. Introduction –Database System –Access, SQL Server and others. –Microsoft Access - Interacting with this databases.
Visual Basic ADO Programming 56:150 Information System Design.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
CIS 338: Using Queries in Access as a RecordSource Dr. Ralph D. Westfall May, 2011.
Copyright  Oracle Corporation, All rights reserved. 4 Accessing a Database Using JBCL.
You can use Access forms to create an interface to your reports and queries. You can add: Buttons to initiate reports and queries Combo Boxes, List.
What is database?  Any Method for access info into Application from DataBase?  ODBC is standard for Accessing Data.  Problem with ODBC:  Information.
Access Project 3 Notes. Introduction Maintaining the Database  Modifying the data to keep it up-to-date Restructure the Database  To change the database.
ActiveX Data Objects (ADO) is Microsoft’s latest database object model. The goal of ADO is to allow VB developers to use a standard set of objects to refer.
Why are Databases Better than Files? Multiple users can all use the same database, and have access to the current, up to the minute values for the data.
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.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1 MIS309 Database Systems Introduction to Microsoft Access.
1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
3 Copyright © 2004, Oracle. All rights reserved. Working in the Forms Developer Environment.
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.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
CONTROL STATEMENTS. A conditional expression uses a comparison operator which results in true or false value. If the comparision is valid it results in.
CHAPTER 6 LESSON B Creating Custom Forms. Lesson B Objectives  Suppress default system messages  Create alerts and messages to provide system feedback.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 6: Accessing a database with PHP Rob Gleasure robgleasure.com.
Chapter 5 Introduction To Form Builder. Lesson C Objectives  Use sequences to automatically generate primary key values in a form  Create lists of values.
1 ADO Activex Data Objects. 2 ADO ADO allows users to access data easily from many existing databases (such as Access or Paradox) From ODBC compliant.
Week 1 Lecture 1 Slide 1 CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton CP2028 Visual Basic Programming 2 v Week.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
1 Chapter 6: Creating Oracle Data Block Forms. 2 Forms  Application with a graphical user interface that looks like a paper form  Used to insert, update,
Visual Basic - Break Processing
ASP.NET Programming with C# and SQL Server First Edition
Visual Basic Database Access
Data Access Objects .
Find, filter etc with connection to Access code internally
The Recordset Object.
Database vs. DBMS Database Database Management System
ربط الفيجوال بيسك VB مع قواعد البيانات
VISUAL BASIC INTRODUCTION TO DATA CONNECTIVITY.
Computer Science Projects Database Theory / Prototypes
Chapter 8 Advanced SQL.
Working With Databases
Unit – V Data Controls.
Presentation transcript:

Component 8, Slide 1 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton CP2030 Visual Basic For C++ Programmers v Week 8 - Databases continued: v Example using Further Features v Find Method v Reposition and Validate Event v Multiple Data Controls v Queries v Other Topics to Consider v Last Words on Databases

Component 8, Slide 2 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton Example of Other Features v Below is an example (dental3.mak) which is used to demonstrate further data control functionality – Modifying properties – Refresh method – Referencing fields – Unbound controls – EOF property – IsNull v The example shows the addition of a list box

Component 8, Slide 3 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton Example - Code v Sub Form_Load () v Data1.DatabaseName = "c:\files\uow\vb_sc_da\dental.mdb" v Data1.RecordSource = "Patients" v Data1.Refresh 'Open database & recordset v Do While Not Data1.Recordset.EOF v If Not IsNull(Data1.Recordset(“Surname”).Value) Then 'If not empty, then v List1.AddItem Data1.Recordset(“Surname”).Value 'add to the list v End If v Data1.Recordset.MoveNext 'Move to next record v Loop v Data1.Refresh 'Rebuild the recordset v End Sub

Component 8, Slide 4 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton Property Setting v The DatabaseName, RecordSource and Datafield (the field within a database to which a data aware control links) properties can all be set at design and run time v The DataSource property (the data control to which a data aware control links e.g. Data1) can only be set at design time v e.g. in above code Data1.DatabaseName = "c:\files\uow\vb_sc_da\dental.mdb" Data1.RecordSource = "Patients"

Component 8, Slide 5 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton Refresh Method v Used to open the database and build or reconstruct the dynaset in the controls recordset property at runtime v Has side affect of making the first record current in the recordset e.g. Data1.Refresh 'Open database & recordset v In the code example above it is used twice i. To initially open the database and recordset ii. To refresh the recordset, making the first record current, after the Do... Loop has completed

Component 8, Slide 6 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton Referencing Fields v Can move through records using MoveNext etc v To select individual fields within a record, need to reference it within the recordsets current record, can use any of the following e.g. v Data1.Recordset(“First Name”).Value Data1.Recordset.Fields(“First Name”) Data1.Recordset.Fields(0).Value Data1.Recordset.Fields(0) Data1.Recordset(“First Name”) v All equivalent since ‘Fields’ is a default collection the recordset and ‘Value’ is default property for a ‘Field’

Component 8, Slide 7 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton Unbound Controls v To display data in controls other than the 5 data aware controls requires coding to manipulate the data in and out of the control v In the above code the required field is referenced and then added to a list boxes list e.g. List1.AddItem Data1.Recordset(“Surname”).Value 'add to the list v Here we’re adding the second field (Surname) of each record into the list box

Component 8, Slide 8 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton EOF / BOF / IsNull v EOF is used to check if the end of the recordset has been reached, when EOF becomes true – Do While Not Data1.Recordset.EOF v In the example the code loops while the end of the file has not been reached v BOF is True when at beginning of recordset v IsNull is a Visual Basic command which checks if a variable is a null, here it is used to check the value of the referenced field of the current record v If Not IsNull(Data1.Recordset(1)) Then ‘If not empty, then

Component 8, Slide 9 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton Example - Code Revisited v Sub Form_Load () v Data1.DatabaseName = "c:\files\uow\vb_sc_da\dental.mdb" v Data1.RecordSource = "Patients" v Data1.Refresh 'Open database & recordset v Do While Not Data1.Recordset.EOF v If Not IsNull(Data1.Recordset(1)) Then 'If not empty, then v List1.AddItem Data1.Recordset(1) 'add to the list v End If v Data1.Recordset.MoveNext 'Move to next record v Loop v Data1.Refresh 'Rebuild the recordset v End Sub

Component 8, Slide 10 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton Alternative version (using For loop and RecordCount property) Data1.Recordset.MoveLast ‘ sets value of RecordCount Data1.Recordset.MoveFirst For I = 1 to Data1.Recordset.RecordCount If Not IsNull(Data1.Recordset(1)) Then 'If not empty, then List1.AddItem Data1.Recordset(1) 'add to the list End If Data1.Recordset.MoveNext 'Move to next record Next I

Component 8, Slide 11 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton Find Instructions v The FindFirst, FindLast, FindNext and FindPrevious instructions are used to locate records in the recordset e.g. Data1.Recordset.FindLast “[First Name] = ‘Jeremy’ ” LocationField toCriteria methodsearchto find v A record which matches the criteria is made the current record, v Criteria can be variables, strings with wildcards Data1.Recordset.FindLast “[Surname] = ‘ “ & NameVar & “ ’ ” Data1.Recordset.FindLast “[First Name] = ‘J*’ ”

Component 8, Slide 12 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton Reposition Event v A Reposition event occurs after a new record becomes current. v First happens when the Data control loads and makes the first record in the recordset current v Is triggered by ‘Move’ or ‘Find’ method Data Control load New Code (Move / Find)RecordReposition CurrentEvent

Component 8, Slide 13 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton Validate Event v Allows validation of changes before database updates v A Validate event occurs before a new record becomes current. v It also occurs before an Update, Delete, Unload or Close operation Update/Delete Data Control load Unload/Close Code (Move / Find)ValidateNew EventRecord Current

Component 8, Slide 14 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton Data Control - Validate event v Private Sub Data1_Validate ( Action as Integer, Save as Integer ) v Action parameter identifies current operation – 1 MoveFirst (includes navigation arrow) – 2 MovePrevious ( “ “ “ ) – 3 MoveNext ( “ “ “ ) – 4 MoveLast ( “ “ “ ) – 5 AddNew – 6 Update – 7 Delete – etc… v Save parameter =True (-1) if any data attached to data control has changed

Component 8, Slide 15 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton Data Control - Validate event v Validate event can prevent the action from happening v E.g. Private Sub Data1_Validate ( Action as Integer, Save as Integer ) If val(txtAge) < 18 then MsgBox “Too young!” Action = 0 End If End Sub

Component 8, Slide 16 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton Validate Event v Save - normally True(-1), set False(0) to stop new data being saved v Note. Don’t use methods under the Validate event e.g. MoveNext, as this creates an infinite loop MoveNextValidate Event

Component 8, Slide 17 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton Multiple Data Controls v Can use two data controls to bring in data from two separate tables v The tables can either be in the same database or in separate databases v Databases could even be different types v But the data under each data control is totally independent of the other data control v The sets of data aware controls update separately; they are dependent on the data control they are attached to

Component 8, Slide 18 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton Queries v If want to relate the data from two tables together: Tables must be in same database, and have a common field to relate v Use a query to create the relationship v Queries are written using SQL, structured query language v Complex language, but some databases e.g. MSAccess allow you to build queries using blocks, it will then generate the SQL code which can be used by VB to link in a query

Component 8, Slide 19 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton Query Example v If you had two tables having a common field of Patient category, one table gives patient demographics and the second costing rates and descriptors for each category. v The SQL to use as the RecordSource to relate the patient name to the cost rate would look like Data1.RecordSource = "SELECT Patients.*, [Cost Rate] FROM Patients, Costing, Patients INNER JOIN Costing ON Patients.[Category] = Costing.[Category] Order by [Surname]" v It isn’t simple !!

Component 8, Slide 20 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton Other Topics To Consider v Transaction Statements - Used to control a series of updates to a database, can be used to allow the ability to undo updates v Error Event - Happens when an error occurs such as an invalid database name being specified v UpdateRecord as for Update except doesn’t trigger a validate event v UpdateControl - reads back values of current record from the recordset into bound controls, means changes to their values can be undone

Component 8, Slide 21 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton Other Topics To Consider v FieldSize - gives the size of a memo field v GetChunk - gets a 64k chunk of data from a memo field, it has an offset parameter for where to get the chunk v AppendChunk - appends data in 64k chunks to a memo field

Component 8, Slide 22 CP2030 Visual basic for C++ Programmers, ‘The VB Team’ Copyright © University of Wolverhampton Last Words on Database v Visual Basic Professional allows greater control over database development with v Ability to create dynaset in code without using a data control v Tools to create new databases and modify existing database structures v Visual Basic uses the MSAccess engine v Visual Basic 3.0 won’t link to MSAccess 2.0 developed database, need to install a fix file COMLAYER.EXE. This gives full compatibility and link ability.