Advanced Interactive Programming Componentbased Programming cont' Componentbased Database Programmin - PowerPoint PPT Presentation

1 / 63
About This Presentation
Title:

Advanced Interactive Programming Componentbased Programming cont' Componentbased Database Programmin

Description:

ActiveX code components are characterized by their location relative to their clients. ... ActiveX DLL ... Create an ActiveX DLL component that uses Microsoft ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 64
Provided by: ChrisN83
Category:

less

Transcript and Presenter's Notes

Title: Advanced Interactive Programming Componentbased Programming cont' Componentbased Database Programmin


1
Advanced Interactive ProgrammingComponent-based
Programming (cont.) Component-based Database
Programming
2
Outline
  • ActiveX code components
  • Create code components in VB
  • ADO (ActiveX Data Object) data controls
  • Data-aware controls
  • Data manipulation in VB

3
ActiveX 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

4
ActiveX 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

5
ActiveX 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

6
ActiveX 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

7
Step1 Start an ActiveX DLL Project
8
Files 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
9
Step2 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
10
Step3 Publishing the DLL
  • Describe the DLL appropriately via project
    property
  • Compile the project into .dll file and register
    it in the Windows Registry

11
Testing 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

12
Step4 Design the Testing Application
  • GUI design

Create a form that uses the DLL
13
Step4 Design the Testing Application
  • Code programming

Create a reference to the DLL
Assign an object to the reference
Call the public method of the DLL
14
DLL 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
15
ActiveX 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.

16
ActiveX 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.

17
Any Questions?
18
ActiveX 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

19
ActiveX EXE
  • Creating an ActiveX EXE component is more or less
    similar to creating an ActiveX DLL
  • But use ActiveX EXE project

20
Differences 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

21
ActiveX 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
  • Active document example

23
Component-based Database Programming
24
Visual 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

25
ADO 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

26
Using 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

27
Visible 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

28
Connecting 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

29
Set DBMS Provider
30
Set Connection
31
Accessing 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.

32
Accessing Data in a Table
  • Use the adCmdTable Command object to specify a
    table in the database that would be accessed and
    viewed.

33
Accessing 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.

34
Configuring 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.

35
Recordset, 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

36
Example 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)
37
Methods 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
38
Recordset 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

39
Syntax 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.

40
ADO 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

41
Adding 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

42
Example - 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.

43
Example - 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

44
Editing 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

45
Data-aware Controls
46
Data-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

47
Key Common Properties
48
Binding 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

49
DataGrid 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

50
Example Scenario
51
Using ADO and DataGrid
Two controls
No code
Configure properties
52
Example (cont.)
53
Example Displaying a Query Results
Input a query statement
Perform the query
Show the results
The ADO control
54
Set Properties at Design Time
  • Connecting String
  • ProviderMicrosoft.Jet.OLEDB.3.51Persist
    Security InfoFalseData SourceC\Program
    Files\Microsoft Visual Studio\VB98\BIBLIO.MDB

55
Code
Set the default query
Set the runtime query
Query the database
56
DataList 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

57
Database Introduction
58
Example 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.

59
Set properties at design time
60
Example Code
61
  • Screenshots

62
Summary
  • ActiveX code components
  • Create code components in VB
  • ADO (ActiveX Data Object) data controls
  • Data-aware controls
  • Data manipulation in VB

63
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com