|
|
 |
 |
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98
Chapter 25 Working With Database Objects In Code
- If you need an immediate solution to:
- A Full-Scale DAO Example
- Using The Daocode Example To Create And Edit A Database
- DAO: Creating A Database
- DAO: Creating A Table With A TableDef Object
- DAO: Adding Fields To A TableDef Object
- DAO: Adding An Index To A TableDef Object
- DAO: Creating A Record Set
- DAO: Opening A Database
- DAO: Adding A Record To A Record Set
- DAO: Editing A Record In A Record Set
- DAO: Updating A Record In A Record Set
- DAO: Moving To The First Record In A Record Set
- DAO: Moving To The Last Record In A Record Set
- DAO: Moving To The Next Record In A Record Set
- DAO: Moving To The Previous Record In A Record Set
- DAO: Deleting A Record In A Record Set
- DAO: Sorting A Record Set
- DAO: Searching A Record Set
- DAO: Executing SQL
- A Full-Scale RDO Example
- RDO: Opening A Connection
- RDO: Creating A Result Set
- RDO: Moving To The First Record In A Result Set
- RDO: Moving To The Last Record In A Result Set
- RDO: Moving To The Next Record In A Result Set
- RDO: Moving To The Previous Record In A Result Set
- RDO: Executing SQL
- A Full-Scale ADO Example
- ADO: Opening A Connection
- ADO: Creating A Record Set From A Connection
- ADO: Binding Controls To Record Sets
- ADO: Adding A Record To A Record Set
- ADO: Refreshing The Record Set
- ADO: Updating A Record In A Record Set
- ADO: Moving To The First Record In A Record Set
- ADO: Moving To The Last Record In A Record Set
- ADO: Moving To The Next Record In A Record Set
- ADO: Moving To The Previous Record In A Record Set
- ADO: Deleting A Record In A Record Set
- ADO: Executing SQL In A Record Set
In Depth
Programming database objects is an enormously complex topic that in itself can take up a dozen volumes. There is a careers worth of work here, so well have our hands full in this chapter.
Here, were going to perform many of the tasks we first saw in the previous chapter, but while we used the data, remote data, and ADO data controls in that chapter, well execute those tasks in code directly in this chapter, using the Visual Basic data object libraries. Working with the data object libraries provides more flexibility, more powerand a great deal more complexity.
DAO
Well use Data Access Object (DAO) methods to do what we did in the beginning of the last chapter: build a database and allow users to move through that database, editing it as they like. To construct a database, well create it, create a table with fields and add it to that database, and also construct an index for the database that will let us sort it.
Working with DAO, you can use the Database and Recordset Data Access Objects in your procedures. The Database and Recordset objects each have properties and methods of their own, and you can write procedures that use these properties and methods to manipulate your data.
TIP: Note that in the Learning Edition of Visual Basic, you cant declare (with the Dim keyword) variables as Data Access Objects in code. This means that only the data control can create Database and Recordset objects, not your code.
To open a database in DAO, you just open a Database object or create a new one. This object can represent a Microsoft Jet database (.mdb) file, an ISAM database (for example, Paradox), or an ODBC database connected through the Microsoft Jet database engine. When the Database object is available, you create a Recordset object and use that objects methods, like MoveFirst and MoveNext, to work with the database.
DAO also supports a client/server connection mode called ODBCDirect. ODBCDirect establishes a connection directly to an ODBC data source, without loading the Microsoft Jet database engine into memory, and is a good solution when you need ODBC features in your program.
In the ODBCDirect object model, the Connection object contains information about a connection to an ODBC data source, such as the server name, the data source name, and so on. It is similar to a Database object; in fact, a Connection object and a Database object represent different references to the same object. (In this chapter, well stick with the Database/Recordset model.)
RDO
With the Remote Data Objects (RDO) library of data objects, you establish an rdoConnection to an ODBC data source, then create an rdoResultset (please note, it is not an rdoRecordset). The Remote Data Objects behave like the DAO objects in many ways, because there is a core set of methods that work with both record sets and result sets.
The big difference between DAO and RDO objects is that the RDO objects are largely SQL-driven. For example, although you can move through a database using methods like MoveNext and MoveLast, just as you would with the DAO objects, programmers often update and modify RDO data sources using SQL statements directly with the rdoConnection objects Execute method. (In this book, well stick to what you can do with Visual Basic.)
|