Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

VB 1-5 Unit - 240620 - 220336

Download as pdf or txt
Download as pdf or txt
You are on page 1of 48

1

UNIT 1

Getting Started withVB6

 Language developed in early 1960's at Dartmouth College:


B (eginner's) A (All-Purpose) S (Symbolic) I (Instruction) C (Code)
 Answer to complicated programming languages (FORTRAN, Algol, Cobol ...). First
timeshare language.
 In the mid-1970's, two college students write first Basic for a microcomputer (Altair) -
cost $350 on cassette tape. You may have heard of them: Bill Gates and Paul Allen!
 Every Basic since then essentially based on that early version. Examples include: GW-
Basic, QBasic, Quick Basic.
 Visual Basic was introduced in 1991.

What is Visual Basic?


 Visual Basic is a tool that allows you to develop Windows (Graphic User Interface -
GUI) applications. The applications have a familiar appearance to the user. • Visual
Basic is event-driven, meaning code remains idle until called upon to respond to some
event (button pressing, menu selection,). Visual Basic is governed by an event processor.
Nothing happens until an event is detected. Once an event is detected, the code
corresponding to that event (event procedure) is executed. Program control is then
returned to the event processor.

VB Programming Environment
To enter the VB environment, click the mouse on the VB icon which appears your
windows desktop. Enter the work space for a new project by selecting new/started or
selecting new project from the file menu.
2

Title bar:Top line is called the Title bar. Itincludes so

Menu bar:

The second line is called the menu bar consists of file, edit, view format, project, help etc.…

Tool bar:

The third line is called the statement tool bar but it contains open , save,cut,copy, past,
delete, undo, start& end program.
3

Working with forms


The Form Window is central to developing Visual Basic applications. It is where you
draw your application. The form design window iswhere the user interface is actually
design.This is accomplished by selecting the desired control icon form the tool box&
placing them in the form design window. Each control can be moved or resized. Its
properties can be reassigned as required.

Tool box:

Tool box contains icon that represent commonly used controls such as text boxes, label boxes,
frame check box etc.…can select a control frame the tools box& placed if in the currentform
design window.

The Toolbox is the selection menu for controls used in your application.
4

Properties window:

The Properties Window is used to establish initial property values for objects. The
drop-down box at the top of the window lists all objects in the current form. Two views are
available: Alphabetic and Categorized. Under this box are the available properties for the
currently selected object.

Project window:

Can display a form or module within the project container window. The Project Window
displays a list of all forms and modules making up your application. You can also obtain a view
of the Form or Code windows (window containing the actual Basic coding) from the Project
window.

 As mentioned, the user interface is ‘drawn’ in the form window. There are two ways to
place controls on a form:
5

1. Double-click the tool in the toolbox and it is created with a default size on the form. You can
then move it or resize it.

2. Click the tool in the toolbox, then move the mouse pointer to the form window. The cursor
changes to a crosshair. Place the crosshair at the upper left corner of where you want the control
to be, press the left mouse button and hold it down while dragging the cursor toward the lower
right corner. When you release the mouse button, the control is drawn.

 To move a control you have drawn, click the object in the form window and drag it to the
new location. Release the mouse button.
 To resize a control, click the object so that it is select and sizing handles appear. Use
these handles to resize the object.

Codeeditor window:

If you double click on a control icon with in the form design window the code edited window
will open.
6

Form layout window:

The Form Layout Window shows where (upon program execution) your form will be displayed
relative to your monitor’s screen.

Immediate window: -

It is very useful when debugging a project.


7

Developing an application
In these topic how use can develop an application or program.

Application (Project) is made up of:

 Forms - Windows that you create for user interface


 Controls - Graphical features drawn on forms to allow user interaction (text boxes, labels,
scroll bars, command buttons, etc.) (Forms and Controls are objects.)
 Properties - Every characteristic of a form or control is specified by a property. Example
properties include names, captions, size, color, position, and contents. Visual Basic
applies default properties. You can change properties at design time or run time.
 Methods - Built-in procedure that can be invoked to impart some action to a particular
object.
 Event Procedures - Code related to some object. This is the code that is executed when a
certain event occurs.
 General Procedures - Code not related to objects. This code must be invoked by the
application.
 Modules - Collection of general procedures, variable declarations, and constant
definitions used by application.
8

The process of writing visual basic program consistof several steps,

 Design
 Create
 Write
 Break
 Run

Design:
What the program is supposed to do.(Used to build application.)
Create:
User interface using visual basic program development tools. This generally involved two related
activities.
a) Draw the controls with their respective form.
b) Define the properties of each control.

Write:
The visual basic instructions to carried out the actions resulting from the various program
events.These can generally involves writing a group of commands called an event procedure.
Break:
Application halted and debugger is available.
Run:
Used to run the application. The program to verify that it executes correctly.

Variables, Data Types and Modules


 VB uses building blocks such as variables, data types, procedures, functions and control
structures in programming environment..

Modules
 Code in vb stored in the form of modules.
 3 kinds of modules are:
 Form modules
 Standard modules
 Class modules
 Large projects are much more manageable if they broke up in to modul. Each of which
contains portions of the cord comprising the entire project. Visual basic support several
types of modules,
 Each of which is stored as a separated file. Form modules are stored as a file identified by
the extension whenever added a new form to a project and then said the form a separated
9

form module is created module. A project may also include a standard module. A
standard module contains declaration and procedures.

Data Types

Variables
 We’re now ready to attach code to our application. As objects are added to the form,
Visual Basic automatically builds a framework of all event procedures. We simply add
code to the event procedures we want our application to respond to. But before we do
this, we need to discuss variables.

 Variables are used by Visual Basic to hold information needed by your application.
Rules used in naming variables:

 No more than 40 characters


 They may include letters, numbers, and underscore (_)
 The first character must be a letter
 You cannot use a reserved word (word needed by Visual Basic)

Variable Declaration:
10

 There are three ways for a variable to be typed (declared):


1. Default
2. Implicit
3. Explicit

 If variables are not implicitly or explicitly typed, they are assigned the variant type by
default. The variant data type is a special type used by Visual Basic that can contain
numeric, string, or date data.
 To implicitly type a variable, use the corresponding suffix shown above in the data type
table. For example,
Text Value$ = "This is a string"
Creates a string variable, while
Amount% = 300
Creates an integer variable.
 There are many advantages to explicitly typing variables. Primarily, we ensure all
computations are properly done, mistyped variable names are easily spotted, and Visual
Basic will take care of insuring consistency in upper- and lower-case letters used in
variable names. Because of these advantages, and because it is good programming
practice, we will explicitly type all variables.
 To explicitly type a variable, you must first determine its scope. There are four levels of
scope:
⇒ Procedure level ⇒ Procedure level, static ⇒ Form and module level ⇒ Global
level
 Within a procedure, variables are declared using the Dim statement:
Dim MyInt as Integer Dim My Double as Double Dim My String, Your String
as String. Procedure level variables declared in this manner do not retain their
value once a procedure terminates.
 To make a procedure level variable retain its value upon exiting the procedure, replace
the Dim keyword with Static:
Static MyInt as Integer Static My Double as Double

Procedures and Control Structures


Procedures:
Procedure is a block of statements which performs a particular task, just that it does not return a
result
Syntax:

[Private/Public]SubProcedure Name (argument list)


11

VB Statements…
End Sub

Visual basic support ‘three’ types of procedures are,

1. Event procedure
2. Sub procedure
3. Function procedure

Event procedure: -

Event procedure is a procedure associated with the event of an object and are named
in a way that indicates both event and object clearly. It is accessed by specific action. Such as
clicking on an object name and the activating event collectively makeup the even procedures.

E.g.:

Private sub command1_click ()

And end sub in between instruction will also called as object or control.

Sub procedures: -

 Sub Procedures perform actions but do not return a value to the calling code.
 Event-handling procedures are sub procedures that execute in response to an event raised
by user action or by an occurrence in a program.
 Function Procedures return a value to the calling code.
 Event Procedure:
 E.g.: Private sub command one click. Private sub from load one the object and action
have been selected,

Syntax:
Sub procedure name(arguments)
-------------------------------------
Statements
-------------------------------------
End sub
It subs procedure can be accessed from elsewhere within the model by call the
statements.
[The call statement is return as [call procedure name (arguments)]
12

Coding: -

Sub Smallest (a, b)


DIM min
If (a<b) Then
Min=a
MsgBox” a is Smaller (a=” &str(min)&”)”
Else if(a>b) Then
min=b
msgBox” b is Smaller (b=” &str(min)&”)”
Else
min=9
msgBox” Both values are equal (a, b=” &str(min)&”)”
End if
End sub
Private command 1-click ()
Dim x As Variant,Y as variant
X=Val(Text1.Text2)
Y=Val(Text2.Text)
Call smallest (x, y)
End sub
Private sub command2-click ()
End
End sub

Function procedure:

A Function procedure is a series of Visual Basic statements enclosed by the Function and
End Function statements. The Function procedure performs a task and then returns control to the
calling code. When it returns control, it also returns a value to the calling code.

Each time the procedure is called, its statements run, starting with the first executable
statement after the Function statement and ending with the first End Function, Exit Function, or
Return statement encountered.

You can define a Function procedure in a module, class, or structure. It is Public by


default, which means you can call it from anywhere in your application that has access to the
module, class, or structure in which you defined it.

A Function procedure can take arguments, such as constants, variables, or expressions,


which are passed to it by the calling code.
13

Control Structures

 Control Statements are used to control the flow of program's execution. Visual Basic supports
control structures such as if... Then, if...Then ...Else, Select...Case, and Loop structures such
as Do While...Loop, While...Wend, For...Next etc method.

Decision making statements

 If...Then selection structure


The If...Then selection structure performs an indicated action only when the condition is
True; otherwise the action is skipped.

Syntax of the If...Then selection


If <condition> Then
statement
End If

 If...Then...Else selection structure

The If...Then...Else selection structure allows the programmer to specify that a different action
is to be performed when the condition is True than when the condition is False.

Syntax of the If...Then...Else selection

If <condition > Then


statements
Else
statements
End If

Nested If...Then...Else selection structure

Nested If...Then...Else selection structures test for multiple cases by


placing If...Then...Else selection structure inside If...Then...Else structures.

Syntax of the Nested If...Then...Else selection structure

 Select...Case selection structure

Select...Case structure is an alternative to If...Then...ElseIf for selectively executing a single


block of statements from among multiple block of statements. Select...case is more convenient
to use than the If...Else...End If. The following program block illustrate the working
of Select...Case.

Syntax of the Select...Case selection structure


14

Select Case Index


Case 0
Statements
Case 1
Statements
End Select

Looping Statements
 A repetition structure allows the programmer to that an action is to be repeated until given
condition is true.
 Do While... Loop Statement

The Do While...Loop is used to execute statements until a certain condition is met. The
following Do Loop counts from 1 to 100.

Dim number As Integer


number = 1
Do While number <= 100
number = number + 1
Loop

 While... Wend Statement

A While...Wend statement behaves like the Do While...Loop statement. The


following While...Wend counts from 1 to 100

Dim number As Integer

number = 1
While number <=100
number = number + 1
Wend

 Do...Loop While Statement

The Do...Loop While statement first executes the statements and then test the condition after
each execution. The following program block illustrates the structure:
Dim number As Long
number = 0
Do
number = number + 1
Loop While number < 201

 Do Until...Loop Statement

Unlike the Do While...Loop and While...Wend repetition structures, the Do Until...


Loop structure tests a condition for falsity. Statements in the body of a Do Until...Loop are
executed repeatedly as long as the loop-continuation test evaluates to False.
15

 The For...Next Loop

The For...Next Loop is another way to make loops in Visual Basic. For...Next repetition
structure handles all the details of counter-controlled repetition. The following loop counts
the numbers from 1 to 100:
Dim x As Integer
For x = 1 To 50
Print x
Next

Arrays

 Up to now, we've only worked with regular variables, each having its own unique name.
Visual Basic has powerful facilities for handling multi-dimensional variables, or arrays.
For now, we'll only use single, fixed-dimension arrays.
 Arrays are declared in a manner identical to that used for regular variables. For example,
to declare an integer array named 'Items', with dimension 9, at the procedure level, we
use:
Dim Items (9) as Integer
If we want the array variables to retain their value upon leaving a procedure, we use the
keyword Static:
Static Items (9) as Integer
At the form or module level, in the general declarations area of the Code window, use:
Dim Items (9) as Integer
And, at the module level, for a global declaration, use:
Global Items (9) as Integer
 The index on an array variable begins at 0 and ends at the dimensioned value. For
example, the Items array in the above examples has ten elements, ranging from Items (0)
to Items (9). You use array variables just like any other variable - just remember to
include its name and its index. For example, to set Item ((5) equal to 7, you simply write:
Item (5) = 7

Working With Controls:


Common controls:
 Button
 Picture Box
 List Box
 Text Box
 Label
 Command
16

 Combo Box
 Check box
 Option button
 Shape control
 DrivelListBox
 Image Control

Text box

The text box is the standard control for accepting input from the user as well as to display the
output.It can handle string (text) and numeric data but not images or pictures.
Private Sub Command1_click ()
‘To add the values in TextBox1
Ana Textbox2
Sum =Val (Text1.Text) +Val (Text2.Text)
‘To display the answer on label 1
Label1.caption = Sum
End Sub

Label Box:

The label is a very useful control for visual basic, as it not only used to provide instructions and
guides to the users, it can be also be used to display out puts. One of its most important
properties is caption. Using syntax label. Caption, it can display text and numeric data.

Command Button: -

The command button is one of the most important controls as it is used to execute commands. It
displaysan illusion that the button is pressed when the userclicks on it.

Picture Box: -

The picture box is one of the most important control that is used to handle graphics. Picture click
in properties window and select the picture from the selector folder.
17

List box: -

The function of the list box is to present a list of items where the user can click and select the
items from the list. In order add item method .for example-add no of items list box following
statements,

Combo Box:-

The function combo Box is also to present a list of item where the user can click and select the
items from the list. In order to add items to the list, use the Add item method.

Check box:-

The check Box controls lets the user selects or unselects an option. When the check box is
checked, its value is set to 1 and when it is uncheck, the value is set to zero. Include the
statements check check1.value =0 to mark the check box and check1.value =0tounmark the
check box, as well as use them toinitiatesertion actions.
18

Option button:-

The option button control also lets the user selects one of the choices.Theothe option button is
unselected.it will be one option button can be selected at one time.

Custom Controls:-

 A custom control is an extension to the standard Visual Basic toolbox. You use custom
controls just as you would any other control.

Masked Edit Control:-

 The masked edit control is used to prompt users for data input using a mask pattern. The
mask allows you to specify exactly the desired input format. With a mask, the control
acts like a standard text box. This control is loaded by selecting the Microsoft Masked
Edit Control from the Components dialog box.
Chart Control:-

The chart control is an amazing tool. In fact, it’s like a complete program in itself. It allows
you to design all types of graphs interactively on your form. Then, at run-time, draw graphs,
print them, copy them, and change their styles. The control is loaded by selecting Microsoft

Multimedia Control:-
 The multimedia control allows you to manage Media Control Interface (MCI) devices.
These devices include: sound boards, MIDI sequencers, CD-ROM drives, audio players,
videodisc players, and videotape recorders and players. This control is loaded by
selecting the Microsoft Multimedia Control from the Components dialog box.
19

Working With Control Arrays:


 With some controls, it is very useful to define control arrays - it depends on the
application. For example, option buttons are almost always grouped in control arrays.
 Control arrays are a convenient way to handle groups of controls that perform a similar
function. All of the events available to the single control are still available to the array of
controls, the only difference being an argument indicating the index of the selected array
element is passed to the event. Hence, instead of writing individual procedures for each
control(i.e. not using control arrays), you only have to write one procedure for each array.
 Another advantage to control arrays is that you can add or delete array elements at run-
time. You cannot do that with controls (objects) not in arrays. Refer to the Load and
Unload statements in on-line help for the proper way to add and delete control array
elements at run-time.
 Two ways to create a control array:
1. Create an individual control and set desired properties. Copy the control using the
editor, then paste it on the form. Visual Basic will pop-up a dialog box that will ask you
if you wish to create a control array. Respond yes and the array is created.
2. Create all the controls you wish to have in the array. Assign the desired control array
name to the first control. Then, try to name the second control with the same name.
Visual Basic will prompt you, asking if you want to create a control array. Answer yes.
Once the array is created, rename all remaining controls with that name.
 Once a control array has been created and named, elements of the array are referred to by
their name and index. For example, to set the Caption property of element 6 of a label
box array named lblExample, we would use:
LblExample (6).Caption = “This is an example”

 Multiple controls of the same type can be grouped into an array.


 Such a grouping is known as control array.
20

 A control array can be created by placing a control within the form design
window. And assainging a value of zero of its index property.
 They copy and passed control.Resulting in a new control with the same name (but
an index value of one.)
Suppose we wish to create a control array. Whose elements are
check boxes and label boxes, left as being with an empty form and then one check
box is normal manner.

Review Questions
1. Discuss about variables in vb.
2. How procedures and functions are implemented?
3. What are the various control structures supported by vb.
4. Differentiate event procedure and property procedure.
5. What are different categories of controls in vb.
6. Define the salient features of control array.
21

UNIT-2
Menus
 Visual basic application can be adding menu is an easy way or use as to access them.
 Themenu bar appearbelow the title bar and it may contain one or more menu title.
 When a menu title is clicked it displays it set of menu item under that titles
 Eg:edit, save,open.
Using the menu editor

 A menu editor can be used to add new commands to the existing menu creating new menus,
menu boxes, change or deleting existing menu and menu bars.
 To display menu editor, menu editor command is choose from the tool menu or the menu
editor button is clicked in the tool bar.

Mouse events
 VB application respond to various mouse events are mouse down, mouse up and mouse
move.
 Mouse down occurs when the user process any mouse button.
 Mouse up occurs when the user release any mouse button.
 Mouse move occurs when ever the mouse pointer is moved to a new point on the screen.
 This events used the arguments button , shift button , X,Y and they contain information about
the mouse condition when the button is clicked.

Dialog box:
 Dialog boxes are used to display information and the user about the data.
 The user about the data need to continue an application 3 ways to adding dialog boxes to an
application.
Predefined dialog box:

 Create using the InputBox() and MsgBox() function.


Custom dialog boxes:

 Created by adding controls to the form.


Standard dialog boxes:

 Create using common dialog controls.


22

Modal and Modeless Dialog Boxes

 Dialog boxes are either modal or modeless.


 A modal dialog box does not allow the user to continue with other applications unless it is
closed or unloaded.
 For displaying a form as a modal dialog box we can use the show method with a style
argument 1 as given below
Form1.show 1
 A modeless dialog box allows shifting of focus between the dialog box and another form
without closing the dialog box.
 For displaying a form as a modeless dialog box we can use the show method without a style
argument.
Form1.show

Input box function:

 The input box function display a model dialog box that ask the user to enter some data.
 A dialog box contain a message an ok button and a cancel button.
 The user can type the text box and the close dialog box by clicking ok.
Rich text box function:

 The rich text box and control allows user to enter and edit text.
 While also providing more advanced formatting features than the normal text box control.
 Using properties text bold, italic, change its colour and create super script and subscript.
 We can also use adjust a paragraph formatting by selecting both left and right indents
 Eg-standard EXEprojectcomponentsMS.rich text box ctrl 6.0 select the box and then apply
ok.

MDI-Multiple Document Interface


 A multiple document interface used for opening many window at the same time.
 All the document window contains a parent window , which provide a work space in the
application.
 VB application can have only one form which contain all the child forms.
 A child form is an ordinary form that have his child property set to true it child property set
to true child forms are displayed within the area of an MDI form at run time.
 Eg
 standardEXEformprojectMDIform.
23

MDI FORM
PROPERTY

 Right click on the MDI form from the project explores windows add 3 or more forms.
 In property window set the child forms are true. in MDI form change the caption as parent.

Flex grid control:


 An MS Flex grid control in VB is used to create application that present information in rows
and columns.
 It displays information cell.
 A cell is a location in MS flux grid at which a row and a column.
 The MS flux grid control displayed operates on a tables data.
 It allows complete flexibility sort, merge, and format tables containing sting and pictures
 Standard EXE tool bar right click
Component select MS flux grid control 6.0 apply ok.

 Using property window to add number of rows and columns in a table.

Review Questions
1. Write short note on mouse events.
2. What is meant by an event? How are they handled?
3. Describe dialog box control in vb.
4. Explain the usage of multiple document interface.
5. Explain about flexgrid control.
24

UNIT III
ODBC and Data Access Objects:
ODBC

 An ODBC driver user the open data base connectivity interface by Microsoft that allows
application to access data in data base managementsystem, using sql as a standard for
accessing the data:
 ODBC was originally developed by Microsoft and samba technology is during 1990.
 It is computing, open data baseconnectivity (ODBC)is a standard application
programming interface (API)accessing databaseManagement system (DBMS).
 ODBC accomplishes DBMS independence by using an ODBC driver as a transition layer
between the application and the ODBC.

Users:

 An ODBC driver user the open DatabaseConnectivity (ODBC)interface by Microsoft that


allows application to access data in database management system (DBMS)usingsql as a
standard for accessing the data.
 ODBC permits maximum interoperability, which means a single application can access
differentDBMS.

ODBC connectivity database


25

Data Access Objects (DAO)


Understanding Data Access Objects

 “Working with Access Collections and Objects,” you’re already prepared for DAO and
any other object model that might come your way.
 All the concepts presented about properties, methods, and collections work the same way.
The only changes are the names of the objects and the types of members they contain.
 “Driving Office Applications with Automation,” focuses on programming Excel, Word,
and other VBA-supported applications through Automation.
 Again, using these object models with DAO just requires that you understand the
different types of objects and their spec- civic properties and methods; you don’t need to
learn any new basic concepts.
 DAO is one of Microsoft’s models for accessing databases. This object model coexists
with the Access programming model.
 However, the DAO feature is also a separate component available in all Microsoft
applications that support VBA.
 Therefore, you can use what you learn in this appendix for programming in Office 2002
as well as in Visual Basic 4 and greater.
 DAO provides a consistent object-oriented interface for performing all database-related
functions.
 Although this might sound intimidating, DAO is not only extremely powerful, but is also
very easy to use. Some of its features are as follows:
 The capability to create and edit databases, tables, queries, indexes, fields, referential
integrity rules, and security.
 The capability to access data by using SQL. Methods are also available for navigating
and searching through the data in tables and data resulting from queries.
 Support for beginning, committing, and canceling transactions. Transactions, which can
be nested, are useful for performing bulk operations on the database as a single action.
 The capability to create custom properties on any DAO.
 The capability to repair and compact databases from the programming language.
 Support for attaching remote tables to your database, as well as for managing the
connection.

When examining DAO, it helps to analyze the composition of your database. Databases in
Access are composed of the following pieces:
26

 Tables Reports
 Queries Code modules and macros
 Forms Security information
 Data Access Pages Relationships
 All these elements make up your database. DAO provides an interface to obtain a
tremendous amount of information about each element.
 You can obtain information about a table’s definition, all its fields, indexes, and
relationships.
 You can quickly and easily add or delete moretables, queries, or relationships.
 You can even add to these objects custom properties that are saved with the database.
 All this power is easily accessible through that consistent program- Ming paradigm
known as DAO.

Getting Started with DAO

 Because DAO consists of such a large number of objects, it helps to see a road map of
how all these objects relate.
 As with most object models, DAO has a distinct object hierarchy. In Access you start
with the Application object, whereas in DAO you start with DBEngine.
 In this section, you learn how to maneuver through the object hierarchy by manipulating
each object.
 Figure shows the diagram representing the DAO object hierarchy. This diagram
demonstrates the hierarchy or tree of objects that DAO exposes. From Figure, you can
see the numerous available objects and the path you must follow to reach them.
27

Data Access Options

Introduction:

Visual basic provides variety of options when it comes to accessing the data stored in the
database.Find all options bellow.

 Data access objects(DAO):-It communicates with the Microsoft access and other
ODBC compliant data sources through the JET engine.
 ODBC Direct:-It allows accessing ODBC data sources through the RDO (Remote
Data Objects) and DAO objects bypassing the JET database engine.

 Data control: - It binds data- aware controls to Microsoft access or other ODBC data
sources.

 Remote Data Objects(RDO):-It provides aframe work for using code to create and
manipulate components of a remote ODBC database system.

 Remote DataBase Control:-It binds to controls to an ODBC remote database.

Remote Data Objects:

Remote Data Objects (RDO): -

It provides aframe work for using code to create and manipulate components of a
remote ODBC database system.
28

Remote Data Objects.

Remote Data Objectsis an obsolete data access application programming interfaceprimarily used
in Microsoft Visual Basic Applications on windows 95 and later operating systems. This
includes database connection,queries,stored procedures, result manipulations,and change
commits.RDO (Remote Data Object) Application Program interface (API) from Microsoft that
lets programmers writing windows application get access and from both Microsoft and other
database provides.It provides aframe work for using code to create and manipulate components
of a remote ODBC database system.

ActiveX EXE and ActiveX DLL


 ActiveX EXE:
 Let us ActiveX EE, which is also called as out of process server.
 One of the major advantages of the out of process server is, it can be run on a different
machine from a client machine and this enabling you to develop application server in 3-
tier client/server architecture.
 ActiveX Exe cloud also be run as a separate program.
 It is suitable for cases where you need to create an application that may be invoked as
standalone application then users can interact with the application.
29

 The ActiveX EXE that we are creating contains the following features.
 It has single class Time.
 A form that is displayed only When ActiveX EXE is invoked as a standalone application.
 A form of Active EXE is used to display running digital clock using time class of the
ActiveX EXE.
 The clientapplication as usual users an object of time class to invoked methods such as
GetTime,SetTime.
 The startup object ofActiveX EXE is sub main which is procedures in code module.

Main procedures determines whether form is server is to be displayed or not depending upon
whether server is invoked as standalone application or invoked by client application.

ActiveX DLL

 An ActiveX DLL is a DLL that supports OLE Component object model (COM).
 A dynamic link library (DDL) is a file that contains compiled code……
 In VB, you create a DLL by selecting ActiveX DLL for the New Project dialogue box.
 The new project will be containing single class module, which illustrates how VB DLLs
work.
 DDL. Stands for “Dynamic Link Library”.
 A DLL (.dll)file contains a library of function and other information that can be accessed
by a window program.
 When program launched, links to ne the necessary.

Creating an ActiveX EXE Component & Creating an ActiveX DLL


Component
Creating ActiveX Component

In the professional and enterprise edition of visual basic, you can use
class modules to create ActiveX components and compile them as either ActiveX DLL or
ActiveX EXEs.The choice with are faced with,effectively, is whether you require an in process
or an out-of process component.

In-Process Components:

 An Active X DLL is an in-process component.But why is it called “in – process”….


 In 16- bit Windows, a DLL effectively become part of the operating system,and all
running process had access to the DLL.
 A handle to the DLL cloud be obtained centrally from the operating system, and the
operating system knew at any given time how many handles to a particular DLL had been
issued.
 In 32-bit Windows,a DLL doesn’t become part of the operating system.
30

 It become part of the process space of the application that calls the DLL.

Out-0f-Process Components: -

 When you create an ActiveX EXE, you are creating an out of -process components.
 An ActiveX EXE runs as a separate process with its own threads. This is ideal for cases
in which multiple applications.(or multiple clients)
 Are likely to require the functionality offered by the classes included in the ActiveX
EXE.
 These are the creating ActiveX EXE Components and Creating an ActiveX DLL
Components.

Review Questions

1. Explain about ODBC connectivity.


2. Write note on DAO and RDO.
3. Differentiate ActiveX EXE and ActiveX DLL.
4. Describe ActiveX EXE components.
5. Describe ActiveX DLL components.

UNIT IV
OBJECT LINKING AND EMBEDDING
Introduction

 An important feature of the Microsoft windows operating system its ability for sharing
information among applications.
 OLE is a means of communication which gives any application the power to directly use
and manipulate other windows application

OLE Fundamentals
 DDE is an acronym for Dynamic Data Exchange.it is the basic foundation for inter
process communication between applications.
 There is a functional similarities between OLE and DDE and there are few differences.
 OLE actually transfers control to the original applications.
 OLE is a technology that enables the programmer of a windows based application to
create an application that can display data from many different applications.
Object and classes
31

 An object is a combination of code and data that can be created as a unit.


 An application can also be an object.
 There are three ways of creating an object
1. The object is added to the toolbox using the custom control command.
2. The Create object or get object function can be used for creating the object in code
3. The object can be embedded or linked within OLE container control. the controls in the
VB represent a class.

OLE automation
 Some application provide object that support OLE automation.
 Some object that support OLE automation that also support linking and embedding.
Container application

An application that receives and displays an object’s data is a container application.

Linked objects

Data associated with a link object is stored by the application that supplied the object. this
application store only link references that displays a snap shot of the source data.

Embedded object

When an embedded object is created. all the data associated with that object is contained in
the object.

Using OLE Container Control


 The OLE container controls allows adding object from other applications.
 An OLE control can have only one object at a time. Using OLE control we can do the
following
 Create a placeholder in our application for an object.
 Create a linked object in our application.
 Bind the OLE container control to the database.
 Create objects from the data that was copied on to the clipboard.
 Display object as icon.
Creating object at design time

 When an object is inserted into the OLE control at design time, the class, Source Doc
and source item properties that identify the application that supplies the object, the
32

source file name and any specific file that is linked from within that file are
automatically set.
Creating object at run time

 To create a linked or embedded object at run time, various method and properties are
used in the code .
 We can create a linked object at runtime using the source Doc property and create
linked method.
 The following code fragment creates a linked object at run time
 OLE1.CreateLink “C:\OLE.x1s”
Moving or sizing the OLE Container

 The OLE container control has an Object move event, which is triggered when the
object associated with the control is moved or resized.

Using OLE Automation Objects


 OLE automation is an industry standard technology that applications used to expose
their OLE objects to development tools, macro languages and other applications that
support OLE automation.
 When an application support OLE automation, the object it exposes can be accessed
by visual basic.
 The following functions are used to access an OLE automation object.
 Create object creates a new object of a specified type
 Get object retrieves an object from a file.

Referencing an object using an object library

 The objects, functions, properties and methods supported by an application are


usually defined in the application’s object library.
 Every application that supports OLE automation provides atleast one type of object.
 Using an object’s methods and properties
 After declaring a variable that refers to the OLE automation object

OLE Drag and Drop


 It is a more versatile kind of Drag and Drop
 OLE Drag and Drop method is only used when we move file in explorer from one
directory to another.
 Involves several methods and events.

OLEDrag Method
33

 Initiate an OLE drag operation.


 It is called when data is copied between OLE containers.
 Syntax:
Object.OLEDrag
where, object is the OLE container object act as the source for the drag operation.

OLEDragMode Property

 Determine if the object act as an OLE drag source,and if the OLEDrag operation is
ndone manually or automatically
 The allowable property values are:
 vbOLEDragManual-0 – This is the default value.It is used when the user’s own
OLE drag handlers are wused in the application.
 vbOLEDragAutomatic-1 – Used when application has to handle the drag and
drop operation.
OLEDropMode Property

 Determine the methodology of processing OLE drop events in the application.


 It can take any one of the following values:
 vbOLEDropNone-0 – Default value,prevents the OLE container from allowing
OLE drop events.
 vbOLEDropAutomatic-1 – Set when user entrusts visual basic with handling
of the OLE drag routines.

OLEDropAllowed Property
 Determine whether the OLE drop operations are allowed or not
 If set to true, allows OLE drop operation on the container, otherwise operation are
prohibited.

OLEDragDrop() event

 Event fired whenever an OLE drop operation is performed on an OLE container


which allows OLE drop operation.
 Syntax:
Private Sub object_OLEDragDrop(data As DataObject, effect As Long, button As
integer, shift As integer, x As Single, y As Single)
OLE DataObject can be referenced using the GetData method to retrieve the data
being dropped in this event. Effect parameter is used to communicate to the target
component the action that is to be performed on the data. Button parameter is used to
34

identify the button on the mouse that was clicked during OLE drag operation. The x
and y indicates the current position of the mouse pointer.

File and File system control

File system control

 VB6 provided three native toolbox controls for working with the file system:
the DriveListBox, DirectoryListBox, and FileListBox.
 These controls could be used independently, or in concert with one another to navigate
the file system.
 We can select the desired file by selecting a drive from the DriveListBox, a directory
from the DirectoryListBox and a file from the FileListBox.

Accessing Files

 A file consist of a series of related bytes located on adisk.


 When an application access a file, it must assume what the bytes are supposed to
represent.
 Depending on the kind of data the file contain, we can use the appropriate file type.
 There are three ways of accessing files in VB.
1. Random access
2. Sequential access
3. Binary access
Random access files

 A random access file is like a database. It is made up of record of identical size.


 Each record is made up of data of identical size.

Sequential access file

 Sequential access files are accessed line by line and are ideal for applications that
manipulate text files.
 Sequential access file is opened in one of the three ways- output, Append, or Input.

Binary access files

 Binary access files are accessed byte by once a file is opened for binary access we can
read from and write to any byte location in the file.
 The ability to access any desired by in the file makes it the most flexible one.
 Before accessing a file in binary mode the file should be opened first for binary access

Review Questions
35

1. Describe in detail about OLE container objects and automation objects.


2. Explain OLE Drag and Drop methods.
3. How is data accessed from a random access file?
4. Explain the file handling and file system controls with example.

UNIT V

Additional controls in VB

SSTab control
 The SSTab control provides an easy way of presenting several dialogs or screens of
information on a single form.
 Only one tab is active in the control at a time.
Working with SSTab

 Start a new project


 The SSTab control is not a part of the standard controls on the tool box.to add the SSTab
control
 Right click on the toolbox
36

 Select component from the popup menu


 From the component select Microsoft tabbed control6.0
 You will see the control on your tool box
 Draw the control on your form.
 To set the properties for each of the tabs
 Add controls to each of the tabs
 Change the caption for each of the tabs.
 Write code for the controls when necessary
You can change the font ,the colour of the text or the background by selecting the option from
the property page

Setting properties at run time


 Add the following code to the form _load event
SSTab1.Tab=12

SSTab1.TabPerRow=4

 By default the number of tab is 3.


 Run the program
 Tabs are indexed beginning at zero(0)

Adding controls to tabs


 Add control to a particular tab’s ‘client area’. just as you add controls to a form.
 Once you have add the control you can write code for each of them.
Enabling and disabling tabs at runtime

 You can use the Tab Enabled property to enable.


 The Tab Enabled property specifies the tab number, then disable it by setting the value to
false.Eg:SSTab.TabEnabled(2)=False
The TabOrientation property

 The tab orientation property allows you to locate the tabs of your tabbed dialog box on
either of the four sides:
 SSTab1.TabOrientaion=SSTabOrientation Left(or)
 SSTab1.TabOrientaion=SSTabOrientation Right
 SSTab1.TabOrientaion=SSTabOrientation Top (or)
 SSTab1.TabOrientaion=SSTabOrientation Bottom (or)

List control tab


37

 The image list control acts like a repository of images for the other control.
 An image list control contains a collection of images that can be used by the other
windows common controls.
 The control uses bitmap(.bmp), cursor(.cur), icon(.ico), JPEG(.jpeg),or GIF(.gif) files in a
collection of list image object.
 You can add and delete images at design time or runtime.
 Working with the image list control
 Start a new project. You can add the image List control.
 From the list of component select Microsoft window common control6.0.
 The image list control and the other controls will get added to the toolbox.
Adding images to the image list

 To add an image to a control at design time use the image list control property pages
dialog box.
 To add list image at design time
 Right click the image list control and click properties to bring up the property pages.
 Click the images tab to display the image list controls property pages.
 Click insert picture to display the selected picture dialog box.
 Click on the key box and enter a string that will uniquely identify that image.
 We can add and remove images to control at design
 Time or run time.

TabStrip control
 The function of a Tab strip control is very similar to the SSTab.
 It is used for a tabbed dialog box to allow user to set various attributes.
 The control consist of one or more tab object in a tabs collection.
 You can affect the tab object appearance by setting properties at runtime and design time.
Creating tabs at design time and runtime

To create tab object at design time

1. Right click the tab strip control and click the properties to display the properties pages
dialog box.
2. Click the tab to display the tab page and make the display.You can add a tab at runtime
with code like this:
TabStrip1.Tabs.Add,”find”,”Find”,”Fbook”

MS Flex grid control


 The MS Flex grid control displays and operates on data in a table form.
 The flex grid is designed to only display the data and not allow the user to enter data in it.
38

 The user can sort, merge and format tables containing string and pictures.
 MS Flex grid control allow read only data.
 We can allow the user to enter data in the Flex Grid using a text box.
 The user can resize the cell width or height in design time or runtime.

Why ADO
 Active x data object is the new data access technology offered by Microsoft.
 ADO mean to replace DAO, RDO, and ODBC.
 We can access any type of data and store it locally in a new type of database and tackle
with various type of data

application

ADO

database

 ADO enabled your client application to access and manipulate data in a database server
through any of the OLE DB providers.
 ADO is primary benefits are ease of use , high speed, low memory overhead and a small
disk footprint.
 The ADO acts like a intermediate between the application and OLE DB.

connection

error

command

parameter

Recordset
39

field
Connection

 The ADO Connection Object is used to create an open connection to a data


source. Through this connection, you can access and manipulate a database.
Error

 The ADO Error object contains details about data access errors that have been generated
during a single operation.
Command

 Once a connection has beenestablished with the data source the data has to be extracted
.this is done using the command object.
Parameter

 parameter are arguments to a command that alter the result of the execution of the
command
Record set

 The command object when executed will return a set of rows from one or more table.
This set of record is called record set.
Field

 A row of a record set has one or more field. To change the data in the data source you
have to modify the value of the field.

Establishing a reference
 Open a new project. To use ADO in your project,you have to make a reference to it.
 This is done just as we made the reference to DAO
 Click on the project, and from the menu select references.
 From the list displayed in the references dialog boxes select the Microsoft Active X Data
objects 2.0.now you can use ADO in your project.
 In order to achieve our object of accessing a data source, extracting a set records from it
and manipulating or editing the record set and finally updating the data source.
 The data source we have to the following steps:
 Make a connection to a data source
 Create a command to specify the records to be extracted.
40

 Execute the command


 Navigate and edit the data in the record set.
 Update the data source with changes made to the data in the record set .

Crystal and Data Reports

Crystal Report
 Crystal Report Writer is a report generation tool.
 It is used to generate simple to slightly complex reports. However, with Visual Basic 6.0
onwards you also get Data Reports. So you can generate a report either with CRW
(Crystal Report Writer) or with Data Report.
 CRW is not a product from Microsoft. It is from Segate Software.
 A report created using Crystal Reports is accessed from a Visual Basic project using an
ActiveX Control - Crystal Report Control.
 The best part of Crystal Reports is its user friendliness. Even an end-user will be able to
generate a report in a few minutes. It is also powerful enough to enable application
developer to generate moderate to complex reports.

Starting CRW(Crystal Report Writer) :First make sure CRW is installed in your machine.
If it is not installed, install it from Visual Studio CDs. Note that by default CRW is not
installed. So install it from Common\tools\Visual Basic\ crysrept directory of the Visual
studio CD.If it is installed, then you start CRW as follows:
1. Click on Start button
2. Select Programs-> Microsoft Visual Studio 6.0 -> Crystal Reports. In which group crystal
reports is placed depends upon the group selection at the time of installation.
3. If you are prompted to enter registration details, cancel it or register and then click on Ok.
4. The initial screen of Crystal Reports will be displayed.

 Crystal Reports for enterprise provides an interface that enables you to quickly and easily
create, format, and publish effective reports.The menu bar provides full range of features
available in Crystal Reports for Enterprise as shown in the following image.
41

The standard toolbar as shown in the following image allows you to access common Report
functions such as: Open an existing report, create a new report, save a report, print a report,
cut, paste, export and undo.
42

The Insert tab allows you to insert objects into you report, such as inserting a text, line, box,
groups, sections, pictures, calculations and/or charts, as shown in the following image.

The Format tab as shown in the following image, allows you to use functions for formatting
the selected field such as: changing the font size or font color, background color, alignment
of text to center, left, right, etc.It also allows you to apply conditional formatting, such as
highlighting values above or below a specific threshold value in the report.

When you click on conditional formatting option at top right corner, the formatting box open.
In this box, you define the condition under which you want conditional formatting to appear.
In setting area, specify the formatting to appear when condition is met, like changing font
style or color of text.The Data tab as shown in the following figure, enables you to work with
data-editing queries, creating groups and sorts, applying filters to limit data in the report
and creating formulas to add custom calculations to reports.

When you click on Query filter option or on Edit data sources, as shown in the following image,
a query panel opens. In the Query panel, you can select objects that you want to see in the report.
In the filter option, you can apply filters to restrict the data returned by the report.
43

When you click on Formula button, as shown in the following image, the Formula workshop
opens. This allow you to use custom calculations in the report. You can apply formulas by typing
or by clicking on objects, functions and operators in the data explorer.
44

The main working area in Crystal Reports is known as Report Design Canvas and is divided
into structure tab and page tab. Crystal Report is divided into five different parts by default and
additional sections are added if you apply grouping to the report.
45

Using the Structure tab, as shown in the above image, you can create the overall structure by
placing items in various sections of the report. You can also apply any required sorting,
grouping, etc. Here, you work with placeholders for data and not data itself.

The Page tab, as shown in the following image, displays the report data on the basis of the
structure you created in the structure tab. Here, you can evaluate formatting and layout of the
report design for distribution.

Data Report
 Helps you design a report that displays fields and records from the underlying table or
query.
 A report is an effective way to present data in a printed format. You can display the
information the way you want to see it
 You create the report using graphical object called Data report designer controls. Data
report designer controls. Include: a data-bound Textbox control, a function control which
displays calculated figures, and Image control for inserting graphics, labels that display
captions, and a line and a shape control that graphically organizes the data.
 The Data Report generates reports using records from database. To use it.
1. Configure a data source, such as the Microsoft data environment, to access a
database.
2. set the Data member property of the data report object to a data member.
3. set the data member property of the Data Report.
4. Right click the designer and click retrieve structure,
5. Add appropriate controls to the appropriate sections.
6. Set the data member and Data field properties for each control.
46

7. At run time, use the show method to display the data report.

 Use the data report object to programmatically change the appearance and behaviour of
the data report by changing the layout of each section object.
 The data report designer also features the ability to export reports using the export report
method. This method allows you specify an export format object, from the export formats
collection, to use as a template \for the report.

Part of the Data Report

The Data Reporter designer consists of the following objects:

1. Data Report Object – Similar to a Visual Basic form, the Data Report object has a
visual designer. It is used to create the layout of a report.
2. Section object – The Data report designer contains sections which helps to configure the
report more elegantly. The sections of the Data report designer is explained in the next
section.
3. Data report controls – special controls that only work on the data report designer are
included with it. These controls are found in the Visual Basic toolbox, but they are placed
on a separate tab named “Data Report’
4. Report Designer – User the report Designer to create and modify reports. When the
report designer window is active, Visual Basic display report controls toolbar.

 Data report designer features – the data report designer has several features:

1. Drag and drop functionality for fields - When fields are dragged from the
Microsoft data environment designer to the data report designer, Visual Basic
automatically create a text box control on the data report and sets the Data
member and Data Field Properties of the dropped field. A command object can
also be dragged from the Data environment designer to the data report designer.
In that case, for each of the fields contained by the command object, a text box
control with be created on the date report; the Data member and Data field
property for each text box will be set to the appropriate values
47

2. Toolbox controls – The data report designer features its own set of controls.
When a Data report designer is added to a project, the controls are automatically
crated on a new toolbox tab named Data report. Most of the controls are
functionally identical to Visual Basic intrinsic controls, and include a label, shape,
image, textbox, and line control. The sixth control, the function
control,automatically generates one of four kinds of information: sum, average,
minimum, of maximum.
3. Print Report – When a report is generated, it can be printed by clicking the
printer icon on the toolbar.
4. File export – in addition to creating printable reports, it can be exported to text
files, by clicking the export icon and specifying text file name.

 Section of the Data Report Designer- The Data report designer contains the following
sections:

1. Report Header – Contains the text that appears at the very beginning of a report,
such as the report title, author, or database name.
2. Page Header – Contains information that goes at the top of every page, such as
the report’s title.
3. Group Header/Footer – contains a “repeating” section of the data report. Each
group header is matched with group footer. The header and footer pair are
associated with a single command object in the data environment designer.
4. Details- Contains the innermost “repeating” part (the records) of the report. The
details section is associate with the lowest level command object in a data
environment hierarchy.
5. Page Footer – Contains the information that goes at the bottom of every page,
such as the page number.
6. Report Footer – Contains the text that appears at the very end of the report, such
as summary information, or an address of contact name.

 Printing a Data Report - Printing a data report can be accomplished in one of two ways.
The user can click the print button that appears on the data report in print preview mode
(using the show method), or you can programmatically enable printing using the print
report method.
 Choosing to display a print dialog box - When printing a report programmatically, you
have two choices:

 Print by displaying the print dialog box- Add a command button to a form.In the
button’s click event, place the following code:

data report1. print report true.

 Printing without displaying the dialog box- Add a command button to a fromIn
the button’s click event, place the following code:
48

data report1. print report false

 Print Report Method - At run time, prints the data report created with the data report
designer.Syntax

Object. Print report (Show Dialog, Range, and Page From, Page To)

Review Questions

1. Write note on crystal and data report in vb.


2. Explain tabstrip control.
3. Describe sstab control and the working of sstab control.

You might also like