6 Chapter Six Using Visual Basic 6.0 to Create Web-Based Database Applications

Preview:

Citation preview

6 ChapterSix

Using Visual Basic 6.0 to Create Web-Based

Database Applications

6 Chapter Objectives

• Learn about the Visual Basic development environment

• Learn about the components of a Visual Basic database application

• Create a Visual Basic application using the ADO data control to display database data

6 Chapter Objectives

• Create programmatic data controls to process data in a Visual Basic program

• Create a Visual Basic ActiveX document that is run from a Web page

6 ActiveX Documents

• ActiveX documents– A program that users can download from a Web

site and install on their computers

Figure 6-1: Installing an ActiveX document

6 Overview of Visual Basic

• Visual Basic– Can be used to create almost any kind of

Windows-based program

– An event-driven programming language• Program statements execute in response to user

actions or to system actions

6 Components of a Visual Basic Program

• Project file– Specifies properties of the program and the names

of the individual component of the program

• Form– Main components of a VB project

– Composed of the visible interface that the user sees and interacts with, and the associated code that responds to user and system actions

6 Components of a Visual Basic Program

• Standard module– Contains only code– Used to store declarations for variables that are used in

multiple forms

• Procedure– Manipulates variable values

• Function– Returns a single value based on parameters that are

passed to the function

6 Components of a Visual Basic Program

• Controls are the visible items on a form

Figure 6-2: Form Controls

6 Visual Basic Projects

• When VB is started, a new project is created, which corresponds to an individual user application

• When you create a new project, you can select the type of project it will be, which specifies how the project file is compiled

6 Visual Basic Projects

Figure 6-3: Visual Basic project types

6 The Visual Basic Development Environment

• Integrated development environment (IDE)– Environment for developing programs

Figure 6-4: VB IDE components

6 The Visual Basic Development Environment

• Main IDE components include:– The toolbar provides quick access to menu bar

commands for opening

– The Object window shows the form and the position of the controls on the form

– The toolbox contains icons that are used to create form controls

6 The Visual Basic Development Environment

• Main IDE components include (cont.)– The Project Explorer displays a hierarchical list of

the components of the current project

– The Properties window is used to assign and display the properties of the object that is currently selected in the Form window

• Docking– Allows the borders of windows to be attached, or

docked, to other windows

6 Creating a Visual Basic Database Application

• Data control– Form control that is attached to a database table– Used to sequentially step through the record to

view the items

6 Creating a Visual Basic Database Application

Figure 6-8: Clearwater Traders Inventory Maintenance application

6 Modifying Form Properties

• The properties of a VB object specify the object’s appearance and behavior

• Name property– Determines how objects are internally

identified within the project– Must begin with a letter and can contain

characters, numbers, or underscores

6 Modifying Form Properties

Table 6-1: VB object name prefixes

6 Modifying Form Properties

• To follow accepted Windows application design guidelines, you should always modify the following form properties:– Border Style, which determines whether the user can

resize the form

– Caption,which is the form description that is displayed in the form’s title bar

– MaxButton, which allows the user to maximize the form so that it is displayed full-screen

6 Modifying Form Properties

• To follow accepted Windows application design guidelines, you should always modify the following form properties (cont.):– MinButton, which allows the user to minimize

the form– StartUpPosition, which determines where the

form is displayed on the screen when the application is running

6 Saving Visual Basic Projects

• A VB project is saved in a file with a .vbp extension

• Forms are saved in files with an .frm extension

• When you create a new project, it is very important to always save the project (.vbp) file and the form (.frm) file in the same folder in your file system

6 Using Visual Basic with a Database

• ODBC driver– Low-level program that translates messages

from an application into the syntax expected by a specific database

– Each different type a database requires a different ODBC driver

– Always runs on the client workstation

6 Using Visual Basic with a Database

• Programs written in program development languages and applications that access databases, need to communicate with ODBC drivers

• To simplify this communication, Microsoft has developed a variety of different data access technologies: Data Access Objects (DAO), Remote Data Objects (RDO), and ActiveX Data Objects (ADO)

6 Using Visual Basic with a Database

• DAO– Available in most applications that support database

connectivity, but cannot support action queries that change content in some databases

• RDO– Full-featured, but only available in Microsoft’s high-end

enterprise-level programming environments

• ADO– Full-featured, is available in most Microsoft

environments, and supports most types of queries

6 Using Visual Basic with a Database

Figure 6-12: Microsoft data access architecture using ODBC

6 Using Visual Basic with a Database

Figure 6-13: Microsoft data access architecture using ADO and OLE DB

6 Creating an ActiveX Data Control

• ActiveX data control– A control that is displayed on a form – Different from an ordinary form control

because it is associated with a recordset• A specific set of database records based on a SQL

query

6 Creating an ActiveX Data Control

Figure 6-14: ActiveX data control

6 Creating an ActiveX Data Control

• To create an ADO data control, you must add a type library to your project

• A type library is a library of code that has programs, such as additional controls, that you can add to VB IDE and then use in your project

6 Using Program Code to Process Data

• With a programmatic data connection:– All of the commands to create the connections and

execute SQL commands are specified in a VB standard module

– User does not have to interact with a visible data control to perform database operations

6 Using Program Code to Process Data

Figure 6-17: Creating a data connection programmatically

6 Code in Visual Basic Programs

• A VB project consists of forms and standard code modules

• Every form and every code module has a section called the General Declarations section– Contains declarations for variables, and the code

for functions and procedures that are used in the form or code module

6 Variables in Visual Basic Programs

• Visual Basic code has variables and data types just like other programming languages

• It is a good programming practice to always declare variables before using them

• VB does not require you to declare variables, but allows you to assign a value to a variable without declaring it first. This can lead to errors

6 Variables in Visual Basic Programs

• Declaration keyword that is used to declare a variable depends on the desired scope of the variable

• Scope – Specifies how long the variable exists in memory

– Specifies what program components can assign and access the variable value

6 Variables in Visual Basic Programs

• Variable declaration keywords are used as follows:– The Public keyword is used to declare global

variables– These are variables that are visible to all forms

and modules within a project

6 Variables in Visual Basic Programs

• Variable declaration keywords are used as follows (cont.):– The Private keyword is used to declare form-

level and standard module-level variables

– These are variables that are visible only to procedures and functions written within the form or standard module where the variable is declared

6 Variables in Visual Basic Programs

• Variable declaration keywords are used as follows (cont.):– The Dim keyword is used to declare local

variables– These are variables that are visible in the

function or procedure where they are declared

6 Variables in Visual Basic Programs

• Variable declaration keywords are used as follows (cont.):– The Static keyword is used to declare local

variables whose values are only visible within the procedure where they are declared, but whose values still exist and can be accessed after the procedure is exited

6 Variables in Visual Basic Programs

• The Variant data type can store any type of data

Table 6-2: Visual Basic data types

6 Visual Basic Objects and Collections

• In object-oriented programming languages, an object is an item that has a specific structure and behavior

• Methods– Procedures that perform an operation on an object

• Collection– Contains a set of related objects

6 Running Programs in the Visual Basic IDE

• Source code– The program code that you write

• Interpreted– Means that the code is translated to machine language

and executed one line at a time

• Compiled– Means that all of the code is translated into machine

language, and the machine-language version is then stored as an executable file

6 Message Boxes

• A message box provides information to user regarding status of program or current operation

Figure 6-23: Message box

6 Message Boxes

• A return value is an integer value that corresponds to the button the user clicks in the response to the message box

• A prompt is a text string that specifies the message displayed in the message box

Table 6-3: Message box return values

6 Message Boxes

• Buttons – Defines the buttons that are displayed in the

message box– Commonly used values include:

• vbOKOnly

• vbOKCancel

• vbYesNo

• vbYesNoCancel

6 Message Boxes

• Title is a text string that defines the text that is displayed in the message box title bar

Table 6-4: Predefined message box definitions

6 Using a DataGrid Control

• To display database records on a Visual Basic form, you use a DataGrid– Always associated with an ADO control, which

specifies the database connection and the SQL query that returns the records that are displayed in the grid

6 Using a DataGrid Control

Figure 6-29: Inventory form

6 Displaying and Hiding Forms in a Project

• In Visual Basic, a form can be displayed as modal or modeless– Modeless

• User can interact with other project forms while the form is displayed

– Modal• User can interact with only the modal form, and

cannot interact with any other project forms until the modal form is unloaded

6 Displaying Visual Basic Programs Using Web Pages

• Visual Basic projects can be created to make standard executable files

• Also can be created using different ActiveX options to make programs that can be installed and/or run by clicking a hyperlink on a Web page

6 ActiveX Options Supported by Visual Basic

• Active documents– Have both a visual interface and code to process user

inputs and actions

• Active programs– Provide processing for other Web programs, but do not

have a visual interface component

• Active controls– Used to create custom controls that can be used in other

Web-based programming projects

6 Creating an ActiveX Document

• User document– Container object in an ActiveX document – Similar to a form in the sense that it can contain

controls and code, and has events similar to those in a form

• A user document must be associated with, or sited on, an external application, which is called a container

6 Creating an Installation Package for Your ActiveX Document

• Installation package - has the files needed to install an application on a user’s workstation

• Visual Basic provides a program called the Package and Development Wizard that leads developers through the steps needed to create an installation package

6 Displaying an ActiveX Document using a Hyperlink

• When creating a Internet package using Package and Development Wizard, the Wizard creates an HTML file containing the command for the hyperlink that references the ActiveX document

Figure 6-34: Link to ActiveX document file in HTML file created by Package and Development Wizard

6 ActiveX Documents and Security Considerations

• One approach to controlling and screening ActiveX programs is the use of verification certificates– These certify that software is from a

trustworthy source

• With verification certificates, users can be confident that the certified software will not harm their workstations

6 Chapter Summary

• Visual Basic is a development language and used to create Windows-based applications

• A VB project can have forms and code modules• Visual Basic has an integrated development

environment (IDE) with multiple windows for displaying and modifying project components

• Visual Basic forms and controls have properties, which determine how the forms and controls look and function

6 Chapter Summary

• An Open Database Connectivity (ODBC) driver is a program that allows an application to interact with a database

• A DataGrid is used to display multiple database records in a matrix format

• You can create a VB project that is an ActiveX document, which has both visual components and code, and can be displayed as a Web page using the Microsoft Internet Explorer browser

Recommended