Title: Advanced Interactive Programming Componentbased Programming cont' Componentbased Database Programmin
1Advanced Interactive ProgrammingComponent-based
Programming (cont.) Component-based Database
Programming
2Outline
- ActiveX code components
- Create code components in VB
- ADO (ActiveX Data Object) data controls
- Data-aware controls
- Data manipulation in VB
3ActiveX Code Components
- Similar to libraries of objects
- ActiveX code components are characterized by
their location relative to their clients. A
client refers to an application that uses a
components code by creating objects and calling
their properties and methods - Two types
- ActiveX DLL - in-process components
- ActiveX EXE - out-of-process components
4ActiveX DLL In-Process Components
- The fastest way of accessing objects without
marshalling across process boundaries. - Not executable by itself
- Used by others
- Usually no GUI
5ActiveX DLL
- These DLLs adhere to the Component Object Model,
i.e. COM, which defines a set of protocols about
how components interact with each other - DLLs announce their presence in the Windows
Registry - They reside in the operating system area rather
than with applications - They are called when needed --- a dynamic link -
this gives rise to the term Dynamic Link Library
or DLL
6ActiveX DLL Example SpellChecker
- Create an ActiveX DLL component that uses
Microsoft Words spell checker to check the
spelling of any string - Create a standard exe application to test the DLL
- The GUI of the testing application is shown below
- Function input a word, the program check if the
word is spelled correctly and show the
information in the label
7Step1 Start an ActiveX DLL Project
8Files in DLL Project
- Could contain Forms, standard modules and class
module - By default, at least one class module but could
contain more class modules - Design the interface that is, the properties,
methods, and events for each class provided by
your component
Name the project members
9Step2 Code Programming
Functions must be declared as public so that they
can be called by applications
Create a reference
Assign the reference an object
Call the method in the object
10Step3 Publishing the DLL
- Describe the DLL appropriately via project
property - Compile the project into .dll file and register
it in the Windows Registry
11Testing ActiveX DLL Components
- Could be done using a separate standard exe
application - Could use groups to facilitate testing DLLs
- This bypasses the need to open and close separate
projects while debugging a DLL with a client
application - Make reference to the DLL in the client
application - Declare one or more DLL class instances in client
12Step4 Design the Testing Application
Create a form that uses the DLL
13Step4 Design the Testing Application
Create a reference to the DLL
Assign an object to the reference
Call the public method of the DLL
14DLL References and Object Browser
- Use the References of the Project menu to make
references to the necessary DLL type libraries - You can declare objects for any type that are
defined within the type library
Reference to the DLL
View DLL interfaces
15ActiveX DLL Creation Summary
- Determine the features your component will
provide. - Determine what class modules are required to
divide the functionality of the component in a
logical fashion. - Create a ActiveX DLL project and design any forms
your component will display. - Design the interface that is, the properties,
methods, and events for each class provided by
your component.
16ActiveX DLL Creation Summary
- Implement the forms required by your component.
- Implement the interface of each class.
- Create a project group consisting of your
component project and a test project. - As you add each interface element or feature, add
features to your test project to exercise the new
functionality. - Compile your DLL and test it with all potential
target applications.
17Any Questions?
18ActiveX EXE Out-Process Components
- Components run in their own process
- It can run as stand-alone applications
- And it can also be
- executed through
- automation locally
- or remotely
- Can have GUI but
- not shown in automation
19ActiveX EXE
- Creating an ActiveX EXE component is more or less
similar to creating an ActiveX DLL - But use ActiveX EXE project
20Differences between DLL and EXE
- EXE is more likely to have a GUI supporting
stand-alone execution - Use Main as a start-up object
- Test EXE components in a separate test project,
usually a Standard Exe project
21ActiveX Documents
- Something like VB forms but more like an
application - Can appear in Internet explorer but contain no
HTML - Can package in either DLL or EXE
- Can be read or activated by an ActiveX enabled
container such as VB, MSOffice Binder and
Internet Explorer - Internet Explorer is the only browser with
ActiveX capability at the moment
22 23Component-based Database Programming
24Visual Basic and Databases
- VB provides capabilities to access and manipulate
the contents of various databases created by many
popular software packages - VB also provides capabilities to create front-end
GUIs to display and edit the contents of various
databases - These functionalities are achieved through
- An ADO (ActiveX Data Object) Data Control that
acts as a bridge between a VB application and a
database. - Several data-aware controls such as DataList,
DataGrid and DataCombo to present different views
to the data
25ADO Data Control
- The ADO data control manages the connection
between a database and other controls in the
application - It can access data stored in a variety of data
sources and formats, such as a database, a
spreadsheet, or even a text file - It can also query tables or joined tables to
retrieve selected data
26Using the ADO Data Control
- Add ADO data control to VB toolbox
- Attach an object of ADO data control to a form
- The buttons move the data control to the
beginning, end and next records in the database
27Visible vs Enabled
- If an application never allows the user to
interact with the ADO data control, then you
should hide the control. - If an 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
28Connecting ADO to a DB
- Specify the source of connection
- Typically, using Connection String to provide
- The DBMS type
- The DB name
- The location
- Security configuration
29Set DBMS Provider
30Set Connection
31Accessing Data in a DB
- The data that can be accessed via an ADO control
is configurable. - This provides a powerful feature for the ADO
control enable different users to access and
use different set of data from the same database.
- The RecordSource Tab in the Property Pages is
used to specify what data will be accessed.
32Accessing Data in a Table
- Use the adCmdTable Command object to specify a
table in the database that would be accessed and
viewed.
33Accessing Data from a Query
- Use the adCmdText Command object to specify an
SQL query that will access and view a subset of
the data in the database.
34Configuring an ADO Connection
- Set other properties including visible, timeout,
username and passwords, etc. - Little or no coding is required, only the use of
the properties window. - Once the DB and table have been identified, the
information can be displayed on the interface.
35Recordset, Field Objects Fields Collection
- Recordset object - the collection of records
selected by the ADO data controls 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
36Example Recordset, Field Objects Fields
Collection
For example the select from Authors command
will create the following Recordset object, Field
objects, and Fields collection Recordset object
All records stored in the table Fields
collection Au ID, Author, Year Born
3 Field objects
To refer to a field within the Fields collection
adocontrol.Recordset.Fields(fieldname)
37Methods of the Recordset Object
AddNew Add a new, blank record to the end of the
recordset CancelUpdate Cancel changes made to
the current record Delete Delete the current
record from the recordset MoveFirst Move the
record pointer to the first record
MoveNext Move the record pointer to the next
record Update Save the changes made to the
current record
38Recordset Objects EOF/BOF Property
- EOF - test for the end of the recordset
- True if the record pointer is positioned after
the last record in the recordset otherwise, the
property contains False - BOF test for the start of the recordset
- Used to detect record position and decide
termination - Private Command1_Click
- If (NOT adodc1.Recordset.EOF) Then
- adodc1.Recordset.MoveNext
- Else MsgBox (Already at last record - Cannot
move beyond this!) - End If
- End Sub
39Syntax of Recordset Objects Methods and
Properties
- The syntax for invoking a method of the Recordset
object is - adocontrol.Recordset.method
- The syntax for referring to a property of the
Recordset object is - adocontrol.Recordset.property.
40ADO Data Controls 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 controls RecordSource property. You
then need to use the ADO data controls Refresh
method to reopen the database and recreate the
Recordset object
41Adding or Deleting a Record
- To delete a record the following VB command can
be used - adodc1.Recordset.Delete
- To add a record (to the end of a table) the
following commands are required - adodc1.Recordset.AddNew - creates a new blank
record - adodc1.Recordset.Fields(field name)New Data
-specify the data - adodc1.Recordset.Update - Save the new record to
the DB table
42Example - Adding names to a DB
- The following screen allows the user to enter
details of a new entry to a DB. - The code attached to the Add Details button takes
the information from the text boxes and updates
the MS Access DB.
43Example - Adding names to a DB
- The code attached to the button Add Details would
look like - Private Sub CmdAdd_Click()
- adodc1.Recordset.AddNew
- adodc1.Recordset.Fields("LastName")
txtSurname.Text - adodc1.Recordset.Fields("FirstName")
txtForename.Text - If ((txtSurname.Text ltgt "") And (txtForename.Text
ltgt "")) Then - adodc1.Recordset.Update
- Else MsgBox ("You must enter some data!")
- End If
- End Sub
44Editing Records
- To edit a record, make sure first of all that it
is the record currently being displayed. - The following command tells VB that the record is
about to be edited - adodc1.Recordset.Edit
- Assign new data to the specific field whose
contents you wish to change as follows - adodc1.Recordset.Fields(field name) New
Data - Lastly, the update must be saved to the database
- adodc1.Recordset.update
45Data-aware Controls
46Data-aware Controls
- Refer to the controls for displaying and
manipulating data in databases, including
CheckBox, ComboBox, DataCombo, DaraGrid,
DataList, FlexGrid, ListBox, PictureBox, Image,
etc. - Common key properties include DataSource,
DataField, DataFormat, DataMember
47Key Common Properties
48Binding Controls
- Connecting a data-aware control to an ADO data
control is called binding, and the connected
controls are referred to as bound controls, which
usually involves - Set the controls DataSource property to the name
of the ADO data control - Set the controls DataField property to the name
of a field in the database - Always set the DataSource property before setting
the DataField property
49DataGrid Control
- Display a table representing records and fields
from a recordset object - Add, update, edit and delete records
- Can be quickly configured at design time with no
code - Automatically display data from recordset if
DataSource property is set at design time - DataSource property can be set programmatically,
able to view different tables or recordset from
different queries
50Example Scenario
51Using ADO and DataGrid
Two controls
No code
Configure properties
52Example (cont.)
53Example Displaying a Query Results
Input a query statement
Perform the query
Show the results
The ADO control
54Set Properties at Design Time
- Connecting String
- ProviderMicrosoft.Jet.OLEDB.3.51Persist
Security InfoFalseData SourceC\Program
Files\Microsoft Visual Studio\VB98\BIBLIO.MDB
55Code
Set the default query
Set the runtime query
Query the database
56DataList and DataCombo Controls
- Similar to standard ListBox and ComboBox controls
- Can be automatically filled from a database field
from the data control to which they are bound - Many other properties that provide flexibility
for database application, such as - Pass a selected field to a second data control,
making them ideal for "lookup table" applications
- Link database tables
57Database Introduction
58Example Scenario
- GUI includes a DataCombo, a DataList, a DataGrid
and a button - Functions it allows a user to select an author
and display its books ISBN, select an ISBN and
display some information about the book.
59Set properties at design time
60Example Code
61 62Summary
- ActiveX code components
- Create code components in VB
- ADO (ActiveX Data Object) data controls
- Data-aware controls
- Data manipulation in VB
63(No Transcript)