You are on page 1of 62

Page 1 of 62

Visual basic
Unit 1:

Introduction:
Visual basic is a integrated development environment which is used to create an application as
an windows based one
Every visual basic has different type of application normally you can create standard.EXE
application to create a windows application.
You can also use ActiveX control to create based standard control. There are 2 different types of
standard.EXE available. They are 1. Single document interface (SDI), 2. Multiple document
interface (MDI).

Features in visual basic:


In every visual basic application you can find different types of windows inside the standard.EXE
Every visual basic application will be opened with a project Explorer, property window, form
layout window and initial form window. By default a VB application will be integrated with title
bat, menu bar, tool bar and tool box. These types of integration are called as integrated
development environment.

Title bar:
Title bar is a horizontal bar located at the top of every window. It gives the name of the
application in the windows application. EX. If you having the multi task between two windows
application like the Ms Paint & Ms Word file you can choose the application based on the title
of each application.

Menu bar:
Menu bars are used to interact with the particular windows application. If you need to create an
application you can choose file menu. It also used to save the file, close the file etc.

Tool bar:
Tool bars are used as a shortcut keys or direct usage keys for the menu bars. All the tool bars
will be displayed with their icons inclusive with home some icons are standard tool bar in VB.

B.Balaji (b.balag@gmail.com)
Page 2 of 62

ADD Standard.EXE:
Whenever you clock this icon, it will create a new executable visual basic file.

ADD form:
IT is used to add a new form to your application or project.

Open project:
It is used to open an existing visual basic project.

Start:
It is used to run the current application. You can also use run menu and start button.

Break:
It is used to pass the running project.

End:
It ends the running project.

Project Explorer:
It is used to visible the project explorer window inside the current application or if you are
pressing the project explorer the focus will be moved to project explorer window.

Property window:
It is used to visible the project explorer window.

Tool box:
It enables the tool box window inside the application.

Tool box control:


Tool box controls are used to display the standard controls which are available inside the
components in VB. BY default every tool box control as default control like labels, text box,
command button and etc. If you need to add extra controls into the tool box you need to enable
project components.

B.Balaji (b.balag@gmail.com)
Page 3 of 62

Project Explorer:
Project Explorer are used to display the available project(programs) and different types of
available forms in current application. Every project explorer is used to switch the control from
design window to code module window. Using the project explorer you can add new form to
the current object and you can also sort the forms by ascending or descending order. There are
3 different tools available in project explorer.
1. View code:

It is used to display or the code module.

2. View Object:
It is used to display the current design module.
3. Folders:
It is used to view or hide the available forms inside the project.
4. Project property:
It is used to setup the startup form and you can also rename the project names, project
description and make of the project.

Data types in Visual Basic:


There are 13 different types of data types available in visual basic.

1. Integer data types

2. Long integer data type

3. Boolean data type

4. Byte data type

5. Single precision

6. Double precision

7. String data type

8. Decimal data type

9. Date data type

10. Currency data type

11. Object data type

12. Variant data type

B.Balaji (b.balag@gmail.com)
Page 4 of 62

13. Custom or user defined data type

Integer data type:


Integer data type can accept any type of number with an arithmetic value from the range of
2power of 32(-32768 to +32767).

You can declare an integer value by ’%’ symbol at the end of the each variable names.

Eg:

Variable name %

A%

Long or long integer data type:


Long integer variable can hold the values from the range of 2 power 64 both positive and negative
values. You can declare the long integer variable using ‘&’ Symbol.

Eg:

Variable name&

A&

Date data type:


Date data type are used to store the both date and line information from the system. By declaring a ‘#’
symbol you can store the date variables. You can also use time inside the date data type

Eg.

DOB=#01dec2012#

Currency data type:


Currency data type is a special data type to store different type of currency symbol. You can
currency data type by using ‘@’ symbol at the end of variable name.

IND@=24.00

Variant data type:


The variant data type can hold different type of values in VB application. Every Variant data type can be
denoted by variant name at end of each variable.

B.Balaji (b.balag@gmail.com)
Page 5 of 62

You can also simply declare a variable without any data type and it will be known as variant data type.

SYNTAX:

Dim a as variant

(Or)

Dim a

String data type:


String data type is a collection of characters. It is used to save the variable values in character arrays.
Each string data type can be declared using ‘$’ symbol at the end of each variable name.

SYNTAX:

Dim a as string

(Or)

A&

Declaring variable:
All the variables must be declared with the keyword dim (dimensional) of the keyword. This indicates
each variable must be declared with this keyword. You can declare the variables anywhere inside the
code module in VB application.

Basic syntax for declaring variable:


Dim variable name as data type

Form properties, Methods and Events:


Definition for form:
Every VB applications have several different types of objects.

Eg:

Standard.EXE, Active.EXE, Active.DLL and DHTML file.

In every objects form is the basic window which is used to creating an application.

A form is a window that contains application code and objects which is used to create the user interface.

B.Balaji (b.balag@gmail.com)
Page 6 of 62

You can have n number of forms inside each application.

Every form cans different type of standard controls or dialog boxes which can change the form interface.

Properties in forms:
Generally forms have basic common properties and some special properties inside Visual basic. Each
and every property has the unique way of approach.

You can change the properties values in two ways.

1. Using design module


2. Using code module

Naming property:
Name property defines the current form name which is used to refer the form properties, events and
methods inside the application.

Caption property:
Caption property is used to enable the change inside the current form title.

Color property:
Color property consists of 2 types 1. Fore color, 2. Back color.

Fore color is nothing but the displaying the text color in the current window.
Back color is color displayed at the background of the current window.

Appearance property:
Determines whether the form needs to be in 3 dimensional look. The default value for appearance is 1.
If you change the value to 0 the form will appear as flat display.

Border style property:


Border style property consist of 5 different values.

1. Fixed style value is 1.


2. No border value is 0.
3. Fixed double value is 3.
4. Fixed tool window value is 4.
5. Fixed tool window value is 4.
6. Sizable tool window value is 5.

B.Balaji (b.balag@gmail.com)
Page 7 of 62

The default value for the border style is sizable value is 2.

Height and width property:


This property is used to change size of the current form.

Enable property:
Enable property is used to respect the events such as form click event, focus events, key events, mouse
events and etc.

If you set the property enable to false the form cannot respond to any about events.

Visible property:
Visible property is a dangerous property which is used to display the execution of each and every
position of the form. If you set the value of visible false the form will be not visible to the user during run
time.

You can able to see the form in the design window.

EXAMPLE FOR FORM EVENTS:

Private sub Command1_click()

Form1.width=1000

Form1.height=2000

End sub

Private sub Command2_click()

Form1.move 500000

End sub

Private sub Command3_click()

Form1.window state=2

End sub

Private sub form1_load()

Hscroll1.value=1000

Vscroll1.value=1000

B.Balaji (b.balag@gmail.com)
Page 8 of 62

End sub

Private sub Hscroll1_change()

Form1.move Hscroll1.value, Vscroll1.value

End sub

Private sub Command4_click()

End or load me

End sub

Window state property:


This property determines how the form will be displayed at run time. There are 3 possible settings in
window state.

If the value =1 the form will be displayed as icon.


If the value =2 the form will be displayed in maximum size.
If the property value =0 the form will be displayed in normal size(display same like design
window)

Scale Properties:
Scale properties are used to display the form with different size according to the monitor display.

There are 5 different types of scale properties.

Scale mode
Scale mode are used to change the units of internal coordinated of the forms inside the system.
This uses pixel centimeters or inches or multi meters unit of resolution to change the scale
mode in monitors.
Scale height and scale width:
This type of property is used to setup scale height and width of the forms from scale mode
property.
Scale left and scale top:
This type property describes the changes of scale left and scale top of the form inside the
monitor.
Font property:
This Property used to set the front text values. You can use font type, font style, font size,
underline and strike through.

B.Balaji (b.balag@gmail.com)
Page 9 of 62

Events in form:
Events are the procedures used execute code modules.

General syntax for Events procedure:

Private sub control name_event names()

Coding . . .

End sub

Load event:
This event will be fired when a form is loaded for first time.
Syntax:
Private sub form1_load()
Coding . . .
End sub
By default form loads are for initializing for the values, standard controls and events procedures
for every form load this event will be fired only once.
Resize() Events:
This event will be fired whenever a form is resizing. That means you can repaint the user
controls inside the forms.
Click() Event:
This event will be fired when the mouse pointer clicked inside the current form.
Double click() event:
This event will fired when the mouse button is clicked twice.
SYNTAX:
Private sub formname_dblclick()
Coding
End sub

Standard controls in vb:

Text box control


Definition:

Text box control are used to accept the values from the user as a input to any variable

Text box are used to display the values as a output to the user.

Generally text box accept the value from the user as a string data type. We need to change the string
data type we need to change the string value to a integer using val() function.

B.Balaji (b.balag@gmail.com)
Page 10 of 62

Standard properties in text box:


There are 50 properties available in text box you can set 39 properties at design time itself.

TEXT:
Text property are used to get the value from the user and assign to a variable as controls this
also used to set the value for the output to the user.
The default value for every text box is text number.

Alignment property:
This property controls how the text should be displayed. The default value is 0(left alignment). If you
change the value to 1, the text will be right aligned if the value is 2 the text is centered.

Multiline property:
This property determines whether a text box can accept more than one line of text. This control is a
Boolean control if you enable the multiline option VB automatically enables wordwrap. The multiline
property fo text box with accept 32000 characters.

Scroll bars:
This property enables when multilevel property is enabled. You can choose two type of scroll bars
horizontal scroll bar if the value is 1. The text box will have only horizontal scroll bar and it will limit the
character. If the value is 2 the text box has vertical scroll bar only.

Max length:
This property determines the maximum no. of character to the text box. The default value is 0. (i.e.)
32000 character any setting other than 0. Will limit the user to change no. of character.

Password character:
Password character property lets you to limit what the text box displays when the password is entered.
By default the password char users ‘#’ symbol.

Border style:
Border style are used to possible settings for the text box. The default value is 1 which gives you a single
width border. If you change the property to 0. The border will be disappeared.

Locked:
It is a Boolean property which is used to prevent users from changing the content of the text box.

B.Balaji (b.balag@gmail.com)
Page 11 of 62

Event procedures:
Text box can recognize 23 event procedures.

Got focus & lost focus:


This event will be fixed whenever a cursor is entered or exited from the text box.

Ex:

Private sub form1_load()

Text1.text=”welcome to VB”

Text2.text=”this is VB lab hour”

Text3.text=”thank you”

End sub

Private sub text1_gotfocus()

Text1.text=”setting got focus in text1”

End sub

Private sub text1_lostfocus()

Text1.text=”setting lost focus”

End sub

Private sub text3_gotfocus()

Text2.text=”we are 2nd b.c.a. students”

Text3.text=”got focus in this”

End sub

Private sub text3_lost focus()

Text2.text=”this is VB lab hour”

Text3.text=”lost focus used”

End sub

B.Balaji (b.balag@gmail.com)
Page 12 of 62

Change event:
Change event are used to executive a code module whenever text box need to change in run time.

SYNTAX:

Private sub text1_change()

Coding----

End sub

Key events:
There are 3 different types of key events in a text box 1. key down, 2. Key up, 3. Key press.

Key down:

This event will be executed whenever the user pressing any keys inside the keyboards.

Key up:

This is event will be fixed whenever the user releases the keys in a keyboard.

Key press:

This event has the similar features like key down event.

General syntax:

Private sub text1_keyevent()

{key press

Key down

Key up}

Coding---

End sub

Labels:
Labels are used to display information to the users at the runtime. The most common use of labels are
to identify a text box or any other standard controls. Labels have 34 possible properties. Yo u can use 30
properties at design window itself.

B.Balaji (b.balag@gmail.com)
Page 13 of 62

Caption property:
Caption property are used to display the text as a description as a label control.

Align property:
Alignment property for label has 3 possible settings. Default it will be in left alignment and the values is
0. If the value is 1 the alignment will be right justify. If the value is 2. The best will be centered.

Border style, back style:


Border style property has 2 possible settings. By default there will be no border and the value is 0. If you
change the value=1 you can set a border style to the label property.

Back style property determines whether the label is in transparent or not.

Event procedure for labels:


Labels respond to the 18 different event procedures. By default labels will respond to click event, doule
click events or change events.

Command button:
Command button are the most useful controls inside a VB application all the application must have
atleast one command button inside each form.

Properties in command button:


Command button uses more than 52 properties for executing it needs.

Caption:
Caption properties on a command properties determines the user what to be seen as a text in command
you can use any text for a caption property on a command button is automatically centered with in the
button.

By default command button shortcut with caption like command1, so on.

Enable:
This property determines whenever the button can respond to any events are not. If you set the
property to false no events will not respond to the button.

B.Balaji (b.balag@gmail.com)
Page 14 of 62

Visible:
Determines whether the command button is visible to user or not. If the property is false the command
button will not be displayed at run time.

Mouse pointer:
Mouse pointer are useful when the mouse cursor changes when you enter inside the command button.
You can change the mouse cursor icon using the mouse pointer property.

Picture property:
Picture property are used to set a picture inside a command button instead of caption property. To
display a picture you need to set a style property in a command button.

Style property:
Style property are used to set a special appearance to a command button by default style property will
be in 0 that means the command button is in standard format if you change the style property to 1. The
command button will be in graphical style.

Down picture:
Down pictures are used to set the pictures when an command button is fixed.

Disabled pictures:
Disabled pictures property are used when an command button is set to an enabled false.

Event procedures in command button:


Command button is the only control to use all different type of event procedures inside visual basic
application. Command button supports, basic event procedures like click() event, double event, change,
got focus(), lostfocus().

Command button supports special event procedures like mouse events and keyboard events.

Mouse events:
Command button supports 5 different types of mouse events.

1. Mouse down, 2. Mouse up, 3. In text box and 4. Label.

B.Balaji (b.balag@gmail.com)
Page 15 of 62

Example for event procedures in text box and label:


Dim a, b, c

Private sub form1_load()

Label1.caption=”2nd b.c.a. student”

Label2.caption=”example for text box and label events”

End sub

Private sub form1_click()

Label3.caption=”enter value for a”

Label4.caption=”enter value for b”

Label5.caption=”total of a*b”

Text1.text=””

Text2.text=””

Text3.text=””

End sub

Private sub text1_keyup()

A=val(text1.text)

End sub

Private sub text2_keyup()

B=val(text2.text)

End sub

Private sub label5_click()

C=a+b

Text3.text=c

End sub

B.Balaji (b.balag@gmail.com)
Page 16 of 62

Access keys:
Access keys are used to create a shortcut buttons for any objects inside the VB application. Generally
access keys are used for creating a short cut for menu bars, command buttons, check box, radio button
and etc.

Every access keys can be controlled using & symbol in the front of any character in your caption
property.

Many windows application allow to pressing alt key + some combination keys to activate general
windows shortcut. You can use a control keys and alter keys with addition of any other keys
combination to access the combination key.

Message boxes:
Message boxes used to display a information in a dialog box on a form this message boxes will want for
the users to return the choice button before executing the application. The users cannot switch to any
other application without responding to the message box.

Default syntax for message box:

Msgbox(“message”, type of box, “title”)

Message box-different type of box control


SYMBOLIC CONSTANT VALUES DESCRIPTION

VB OK ONLY 0 It will display ok button only inside the message box

VB OK CANCEL 1 It will display ok and cancel buttons inside the message box

VB ABORT RETRY IGNORE 2 It will display abort, retry, ignore buttons inside message box

VB YES NO CANCEL 3 It will displays yes, no, cancel inside message box

VB YES NO 4 It will display yes and no button inside message box

VB RETRY CANCEL 5 It will display retry and cancel button inside message box

VB CRITICAL 16 It will displays a critical msg icon before the msg in msg box

VB QUESTION 32 It will displays a warning icon before the msg in msg box

VB EXCLAMATION 48 It will displays a warning msg icon before the msg in msg box

VB INFORMATION 64 It will displays a information msg icon before the msg in msgbox

VB DEFAULT BUTTON1 0 First button in the box will be default

B.Balaji (b.balag@gmail.com)
Page 17 of 62

VB DEFAULT BUTTON2 256 The second button in the msg box will be default

VB DEFAULT BUTTON3 512 Third button in the msg box will be default

EXAMPLE:

Msgbox ”welcome to VB”, VB OK CANCEL+VB INFORMATION+256,”EXAMPLE”

Input box:
Input box functions to get the value from the users at run time and it will be return to a variable
declared. Generally all input box will be displayed like a dialog box at run time.

General syntax for input box

Variable name=Input box(prompt, title, default, [xpos], [ypos], [helpfile])

Prompt:
Prompt parameters is a string variable 29 any text values which is to be displayed as a caption in input
box all the captions can be not exceeded than 1024 characters. in input box all the captions can be not
exceeded than 1024 characters.

Title:
Title parameter is a optional and it will gives the caption used for the title bar in the input box. By
default the application name or program name Is been used as a title bar.

Default parameter:
It is a optional parameter which is used to give a default value to the text box available inside the input
box.

[xpos], [ypos] parameter:


Xpos and ypos are a numeric expression which is used to set the input box scale positions.

Helpfile:
It is used to give each descriptions about the context used inside the input box.

Example for input box:


Dim a,b,c

B.Balaji (b.balag@gmail.com)
Page 18 of 62

Private sub form1_load()

A=val(input box(“enter a:”, “avalue”))

B=val(input box(“enter b:”, “bvalue”))

End sub

Private sub command1_click()

C=a+b;

End sub

Private sub text1_gotfocus()

Text1.text=””

Text1.text=c

End sube

Unit-2

Displaying information in a form:


VB can display a text on a form without any standard controls. i.e. you can display a text without adding
a label control or text box. There is a built in method which is used to display a text inside a form. Print
keyword is the method used to display a text inside a form.

DEFAULT SYNTAX:

Print values

Font method:
Font method are used to change the style of the standard controls inside any forms using the font
method all the changes will be occur at run time only.

SYNTAX:

Objname.font.size=value

Objname.font.style=type

B.Balaji (b.balag@gmail.com)
Page 19 of 62

Current x and current y method:


Current x method refers to horizontal position of the print method inside the form.

Current y refers to vertical position of the print method of the VB form.

SYNTAX:

Formname.currentx=value

Formname.currenty=value

Form methods & events:

There are 6 types of events which will be loaded whenever a form is been loaded for first time. This
events will be executed without the user interface.

1. Form_initialize()
2. Fomr_load()
3. Form-resize()
4. Form_activate()
5. Form_gotfocus()
6. Form_paint()

Form-initialize():

This event is triggered first and only once normally initialize event occurs before the load event is fired
by you cant to add or remove any control means you can place code in initiliaze event.

Form_load():

This event is triggered is load or enter the inputs into the user variable.

Form_resize():

Form resize event is fired whenever the user resizing a form or minimizing a form and then restarting
the form before starting the program. Generally form resize event are used to recalculation the size and

Positions of any object on your form.

Form_activate:
This event will be fired whenever a form is been loaded before anyother forms inside multidocument
interface. This type of event is used only in multiple form application.

B.Balaji (b.balag@gmail.com)
Page 20 of 62

Form_gotfocus:
Gotfocus event will be triggered whenever a form is been loaded and all standard controls are disable.

Form_paint:
Paint event are used to add a paint method a form needs to display a text you can use this event
procedures to redraw the informations on the form. To utilize this paint event you need to set the auto
redraw property to true. So this event will be automatically display the print method property.

Example for paint method:

Private sub form_load()

Print”hello”

End sub

Private sub form_resize()

Print”resize event”

End sub

Private sub form_gotfocus()

Print”got focus event:

Private sub form_ paint()

Print”paint event”

End sub

Private sub form_ activate()

Paint”activate event”

End sub

Private sub form_initialize()

Print”initialize event”

End sub

B.Balaji (b.balag@gmail.com)
Page 21 of 62

Private sub form_lostfocus()

Print”lost focus Event”

End sub

Private sub form_ click()

cls

End sub

Private sub form_dblclick()

end

End sub

Refresh method:
Refresh method are used to order forms available in multi document interface. You can also use refresh
method to redraw the print event procedures. To rewrite the print method.

Syntax:
Formname_refresh

Show method:
Show method are used to display the form in current window you can add the show method in any
standard control. Current form will be overloaded by the new form.

Syntax:

Formname.show

Hide method:
Hide method are used to display the visible of current window from the current application.

B.Balaji (b.balag@gmail.com)
Page 22 of 62

Syntax:

Formname.hide

Format method (or) functions:


It is a predefined function which is used to display the system date and times.

Syntax:
Print format(…)

Name Description
General date it displayed a date and time available inside the system. By default it will

display the date first and then time.

Long date it display the date format inside the windows include day, month and year.

Medium date it display the date format in windows.

Short date it displays only integer values as a date format.

Long time it displays the current time of the windows with inclusive of hours, minutes, seconds

in 12 hours format.

Medium time it displays the current time of the windows with hours and minutes in 12 hours format.

Short time it displays the current time using 24 hours format.

Example for format:


Private sub form_load()

Me.font.size=12

Me.print.format(now, “created date”)

Me.print.format(now,”long date”)

Me.print.format(now,”medium date”)

B.Balaji (b.balag@gmail.com)
Page 23 of 62

Me.print.fomrat(now,”short date”)

Me.print.format(now,”long time”)

me.print.format(now,”,medium time”)

me.print.format(now,”short time”)

end sub

design window:

demonstration of named date/time format

11/1/95 11:08:46pm

Wednesday November 01.1995

01-nov-95

11:08:47pm

11:08pm

23:08

Example for current x and current y:

Private sub form_resize()

Dim message as string

Dim width factor as integer

Dim height factor as integer

Dim left as integer

Dim top as interger

Cls

Message=”welcome to visual basic:”

Left=me.scale width/2

Top=me.scale height/2

Left=me. Scale left+left

B.Balaji (b.balag@gmail.com)
Page 24 of 62

Top=me.scale top+top

Width factor=me.text width(message)/2

Height factor=me.text height*(message)/2

Current x=left-width factor

Current y=top-height factor

Print message

End sub

Rich text box:


Rich text box central are used to display a text and get the value from the user same as text box control.

Rich text box control does not have any limits to the text.

Rich text box are the custom controls which you can import from the components window. To add a rich
text box you can use two different methods.

1. Choose project_components
2. It will open a component dialog box
3. Then click Microsoft rich text box control 6.0
4. 2. Right click on the tool box.
5. Then click on components it will open the components dialog box or you can press ctrl+t for
components dialog box.

Properties:
Rich text box has the same properties like text box control. There are same different properties used to
select, used to cut, copy and paste the text.

Property name description


Seltext this property sets or return a string which is being selected inside the rich box.

Selstart it used to set or return the starting point of selected text in rich text box.

Sellength it is used to set or return the no. of characters selected in rich text box

Selbold it is used to set the selected text as bold.

Selitalic it is used to set the selected text as italic.

B.Balaji (b.balag@gmail.com)
Page 25 of 62

Selstrikethree it is used to set a strike three line for the selected text.

Selunderline it is used to set a underline for the selected text.

Selcolor this property sets the color for the currently selected text. You can use text

decimal coding or vb colors.

Set font name this property is used to change the font style for the slected text.

Setfont size this property is used to change the size of the font for currently selected text.

Maximum font size is 2160 paints.

Example for rich text box:

Private sub form_load()

Dim message as string

Message=”you can select text after you type in the box and”

Message=message&”then click on one of the buttons to see the changes go into effect”.

Msg box message

End sub

Private sub command1_click()

Rich text box1.sel italic=not(rich text box1.set italic)

End sub

Private sub cmdbold_click()

Rich text box1.sel bold=not(rich text box1.Sel bold)

End sub

Private sub cmditalic_click()

Rich text box1.sel italic=not(rich text box1.sel strike three)

End sub

Private sub ing font increase_click()

Rich text box1.sel font size=rich text box1.sel font size + 1

B.Balaji (b.balag@gmail.com)
Page 26 of 62

End sub

Private sub imgfont decrease_click()

Rich text box1.sel font size=rich text box.sel font size-1

End sub

DESIGN WINDOW:

CHAPTER-2
Determinate loops:
Determinate loops are used to define the flow of the control inside the procedures. This is type
of looping are used for testing the value for and users for some conditions. This type users for some
conditions. This type of loops are of three types.

1) For statement,
2) For next statement,
3) Nested for statement.

For statement:
This type of looping statements are used to generate control structure which used to operate a
block of statement. This determine the startin and ending value.

B.Balaji (b.balag@gmail.com)
Page 27 of 62

For next statement:


For next statement in the basic determine loop statement, there are 2 keywords which are used
inside the looping statements are 1) for keyword 2) next keyword

1) for keyword:
this are used to set the starttin value and the ending value for the variable.
2) Next keyword:

This are used to define the increment for variable by default, next keyword used as a increment
operator.

Basic syntax for next statement:


For variable name starting value to ending value

---------

next variable name

eg:

dim a as integer private sub form_load()

for i=1 to 10

print i

next i

for next step keyword:


step keyword are used to set the value for next keywords you can use both + and – value in step
keyword.

Steps for executing for next keyword:


1) For statement will be set the values to the variable using the starting value. This value will be
called as counter variable.
2) When the counter variable value is less than or equal to the ending value the for statement will
be proceeded for next step.
3) Of the counter variable is greater than ending value the loop will be terminated.
4) Once the loop has been started the for statement will be executed till it finds next keyword
inside the procedures.
5) The next keyword will be executed to the counter variable by one as statement.

B.Balaji (b.balag@gmail.com)
Page 28 of 62

6) If the step keyword have been included the next keyword look the step value whether to
increment or decrement counter variable.
7) The step keyword are used to change the counter variable by one variable by any number will
accept positive or negative.

Eg:

Dim I as integer

Private sub form_load()

For i=1 to 10 step -2

Print i

Next i>3

Nested for statement:


It is used to add more than one for statement inside another for statements. This type of statements can
be used for mathematical multiplication or matrix operations.

Basic syntax for the nested for statement:


For variable name1 starting value to the ending value step value

For variable name2 to end value step value

-----------

Next variable name2

Next variable name1

Example:

Private sub form_load()

Dim i,j,k

End sub

Private sub cmd1_click()

I=val(text1.text)

J=val(text2.text)

B.Balaji (b.balag@gmail.com)
Page 29 of 62

For k=I to j step 2

Print k

Next k

End sub

Private sub cmd2_click()

For i=1 to 5

For j=1 to step 2

Print I “*” j = i*j

Next j

Next i

End sub

Private sub cmd3_click()

For k=10 to 2 step -1

Print k

Next k

End sub

Indeterminate loop:
Interdeterminate loop consist of 3 types:

1) Do while loop
2) Do until loop
3) While/went statements

Do while loop:
This is executed with differen types of relational operators and logical operators. This type
statements will be executed once, the condition will be checked for the logical operators. If the
condition has been statisfied, the loop will be executed else the condition will be executed.

B.Balaji (b.balag@gmail.com)
Page 30 of 62

Default syntax:
Do

Statements

--------

-------

Loop while (conditions)

Eg:

Private sub form_load()

Dim x

Do

X=input ( “enter a value”)

Print x

Loop while (x>5)

End sub

Do until loop:
This are used to find the exact matching value for the given condition. You can use only assignment
operator inside the do until condition.

Syntax:

Do

statements

--------------

--------------

Loop until (condition)

Eg:

Private sub form_load()

Dim x

B.Balaji (b.balag@gmail.com)
Page 31 of 62

Do

X=input box (“enter a value”)

Print x

Loop until (x=5)

End sub

While wend statements:


This can be used to print bunch of statements after the condition is executed. If the conditions is not
satisfies, the following statement will be private, till it finds wend keyword.

Syntax:

Do

Statements

-----------

Loop while (conditions)

Statements

---------

Wend

Eg:

Private sub form_load()

Dim a

Do

A=val(inputbox(“enter value”)

Print=”only once”

Loop while (x=10)

Print a

Wend

Print “after wend”

B.Balaji (b.balag@gmail.com)
Page 32 of 62

End sub

Decision making statement or conditional statement:


This are used to check the condition, if the is true the following statement will be executed, else the
condition is false, types of decision making statements are available.

1) If then statements
2) If then else statements
3) Nested if statements
4) Select case or select statement

If then statement:
This are single line decision making statement you can use this type of statements without using end if
keyword.

Syntax :
If (condition) then statement

Or

If (condition) then statement

End if

If then else statement:


This are used to find out 2 different operations for the given if condition.

Syntax:
If (condition) then

Statement

Else

Statement

End if

B.Balaji (b.balag@gmail.com)
Page 33 of 62

Nested if statement:
This is also known as block if then statement. You can have n no. of if condition but you need to exactly
close the else part for each if then statement.

Syntax:
If (condition) then

If (condition) then

Statement

Else

Statement

Else

Statement

End if

End if

Eg:

Private sub cmd1_click()

If x>1 then

If x>5 then

Print “x is greater than 5”

Else

Print “x is between 1 to 5”

Else

Print “x is smaller than 1”

End if

End if

End sub

Dim x

B.Balaji (b.balag@gmail.com)
Page 34 of 62

Private sub form_load()

X=val(input box(“enter value”)

Print x

End sub

Select case or select statement:


Select case are used to verify a multiple conditional statements used by the counter variable. This type
of conditional statements, you can select a multiple options using the counter variable. Every conditions
in the select case can be have different values, according to the counter variable. If the case value is not
matching by default by the select case will more to case else choice.

Syntax:
Select case variable

Case value 1

Statements

Case value 2

Statements

Case else

Statements

End select

Eg:

Private Sub Command2_Click(Index As Integer)

t=0

If op = 1 Then

Text2.Text = Text1.Text

op = 0

Else

B.Balaji (b.balag@gmail.com)
Page 35 of 62

Select Case s

Case "+"

Text1.Text = Val(Text2.Text) + Val(Text1.Text)

Case "-"

Text1.Text = Val(Text2.Text) - Val(Text1.Text)

Case "*"

Text1.Text = Val(Text2.Text) * Val(Text1.Text)

Case "/"

Text1.Text = Val(Text2.Text) / Val(Text1.Text)

End Select

End If

s = Command2(Index).Caption

f=1

End Sub

Private Sub Command3_Click()

t=0

If s = "+" Then

Text1.Text = Val(Text2.Text) + Val(Text1.Text)

ElseIf s = "-" Then

Text1.Text = Val(Text2.Text) - Val(Text1.Text)

ElseIf s = "*" Then

Text1.Text = Val(Text2.Text) * Val(Text1.Text)

Else

Text1.Text = Val(Text2.Text) / Val(Text1.Text)

End If

op = 1

B.Balaji (b.balag@gmail.com)
Page 36 of 62

f=1

End Sub

Goto statement:
This are used to pause the current statements to any other states inside the form. This type of
statements are used to jump the control from one place to another place without executing the next
line statements.

Syntax:
Line variable:

---------

Goto line variable

--------

Line variable:

-----------

Eg:

Private Sub Command1_Click()

Dim i As Integer

Dim j As Integer

Dim k, l As Integer

first:

i = InputBox("enter a integer number to check:", check)

Text1 = i

If (i < 0) Then

MsgBox "enter only positive integers", vbInformation + vbOKOnly + vbDefaultButton1, "integers only"

GoTo first:

End If

Control arrays:

B.Balaji (b.balag@gmail.com)
Page 37 of 62

This are used as a special control which are designed in vb to used standard control as array. You can use
standard controls such as command button, check box, option box, list box etc..,

Every control has its own control array by pressing control + copy in design time & porting the same
control inside the design window. By default vb will appear a pop up message adding you need to have a
control array for the control or not.

Arrays:
The fundamental structure of organizing the information inside to a variables are known as arrays.
Generally, the arrays are have different types of dimensions.

1) Single dimensional arrays.


2) Fixed arrays
3) Dynamic arrays
4) Multi-dimensional arrays

Single dimensional arrays:


It can store n no. of items depending upon the data types of the variable. A special kind of parameters
which are used to find out each arrays variables values are known as index value.

Syntax:
Dim variable [index value] as data type

Int a[10]

Dim a[1 to 3] as integer

Eg:

Private sub cmd1_click()

Dim a[5], i

For i=0 to 5 step 2

Print a[i]

Next i

End sub

Private sub form_load()

a*0+=”2nd bca”

B.Balaji (b.balag@gmail.com)
Page 38 of 62

a*1+=”welcome”

a*2+=”vb lab”

a*3+=”array”

a*4+=”example”

a*5+=”thank u”

end sub

fixed arrays:
this are used to hold the memory space of the array values, throughout the application then memory
will be exited when program has closed.

Dynamic arrays:
This are used to change the size of index value while, executing each control. The memory allotment will
be changed according to the execution of the program.

Multi deminsional array:


This type of arrays are used to add more than one index value to an array varuable. You can use any no.
of index values for multi-dimensional arrays.

Syntax:
Dim variable[index1], [index2] as data type

Eg:

Private sub cmd1_click()

Static multitable (1 tp 12, 1 to 12) as integer

Dim i, j

For i=1 to 12

For j=1 to 12

Print multitable (I, j) = i*j

Next j

B.Balaji (b.balag@gmail.com)
Page 39 of 62

Next i

End sub

Functions & procedures:


Definition for functions:

Functions is a block of statement which can be defined by user. This set of code can able to function
throughout the application.

All the function can able to return value to a calling function name. the return type can be vary
depending upon the data type.

Default syntax for functions:


[private] [public] [static] function procedure name (argument) as data type

-----------

------------

End function

By default, there are 3 types of scope in function procedures.

1) Private
2) Public
3) Static

You can declare the function, anywhere inside the code module. You need to specify the return data
type, if you are position an arguments. The return type data types can differ for the function procedures.

You can add the function using built-In functions dialog box.

Adding a function or procedures to code:


You can add a function or procedures inside a code module by adding tools menu adding procedures.
This will open a add procedures dialog box.

You can also open this ass procedure by pressing a shortcut all+f or f>.

Procedures:

A procedures is a set of block which can execute the user defined coding, according to the calling

B.Balaji (b.balag@gmail.com)
Page 40 of 62

Function name. generally, procedures will not return any value for the calling procedure name.
procedures allows you to move program or will allow to change the logic from one place to another
procedures are used in one program can be used to a logic for another form.

Types of procedures:
1) Sub procedures
2) Function procedure
3) Property procedure

Sub procedures:
This are the difficult procedures type. This are by default has a specific module which are used to
execute and pass the value to entire application.

Syntax:
[private] [public] [static] sub procedures name (arguments)

----------------

------------------

End sub

Types of sub procedures:


1) General procedures
2) Event procedures

General procedures:

It is used to define your own sub procedures. It is used to perform different types fo task. Once it
procedure name is involed. By default, all general procedures can be written in any type of public forms.
You can also include the general procedures inside class module application. You can also use a general
procedure to pass the value to the local variable inside procedure name.

Syntax:
Syntax is as similar as sub-procedure.

B.Balaji (b.balag@gmail.com)
Page 41 of 62

Event procedure:
Every vb application, must have a single event procedures. All the application which are running while
the project is executing event procedures are called as an object in visual basic. All the standard controls
inside the vb, having the event procedures.

Syntax:
Private sub controlname_eventname()

----------------

----------------

End sub

Property procedure:
This are used like function procedures or sub-procedures. It is used to invoke the changes for the
current controls, properties at run time. you can change the different type of properties while running
the application, without changing in design time can also add the return type for the calling property
procedures.

Syntax:
[private] [public] [static] controlname_property name()arguments) as datatype

----------

----------

End sub

Eg:

Private sub form_load()

Dim s

S=caption

Print s

Command1.caption=s

Sub end

Private sub command1_caption() as variant

B.Balaji (b.balag@gmail.com)
Page 42 of 62

Command1.caption=”second bca”

End sub

Unit – 3

Menus and menu editor:


Definition:

It is a built-in pop up dialog box, which is used to enable different type of actions durinf the execution of
an application. You can add different type of events to a menu editors, such as click event, got focus
event etc..,.

A menu is a first and fore most ordinary pop-up window, which is used to respond,
mouse events and key board events. You can also use a access key to enable the short cut for the menus
inside the form.

To add a menu editor, you need to enable the menus, inside the form.

You can add a menu editor using a menus called toole-menu editor or you can use control+e as shortcut
key for menu editor.

After adding the menu editor, the menu editor is open with different properties like,
caption, name, index box, enable text, visible check box, arrow buttons, next button, insert and delete
button.

Caption property:
This are like caption text box property. It is used to add the caption for menus which will be
displayed as the default label for menu.

Name property:
This are like name text box property. It is used to name the current menu which can be used for any
event procedures with the control name. if you are using the same menu, more than once, you can
name the current name with index properties.

Index text property:


It is used to add a control array for the menu with different caption property.

B.Balaji (b.balag@gmail.com)
Page 43 of 62

Shortcut box:
It is used to add a shortcut for the current menus. By default of keys, which can be added as a list box
inside the shortcut box. You can cannot use a alt+f4 shortcut for the menus.

Enable check box:


It is used for response the current menu item. If the check box is enable, the current menu item can be
used for event procedures, else your cannot use the enable for the current menu item.

Visible check box:


It determines the current menu item to be display at run time. If the visible check box is disable or
unchecked the current menu item will be invisible.

Arrow buttons:
This are used to highlight the menu items which menu and sub-menu. you can use four types of arrow
buttons to utilize the menu options hierarchy. You can also create ‘n’ no. of sub-menus, hierarchical to
the default menu items.

Next buttons:
Using this property, you can move the menu item to next main menu.

Insert buttons:
It is used to finalise the current menu item.

Delete item:
It is used to remove the currently highlighted menu items.

Multi-document interface:
It allows to create an application that can maintain multiple forms with a single contains forms. Best
example for multi-document interface is Microsoft excel.

Creating mdi forms and child forms:


To create an mdi form, you need to open the project menu and odd mdi for or you can also add
mdi form using project explorer. You can create only one mdi form for an entire application. You can
create n numbers of child forms inside the application. You can also create or open an existing forms for
the mdi forms.

B.Balaji (b.balag@gmail.com)
Page 44 of 62

Loading mdi and child forms:


Whenever you are creating a child forms parent mode property need to be changed for enabling
mdi forms. Every mdi form inside an application also need to be changed in auto show child property. by
default, mdi form will not be loaded automatically, you need to change starting form object or startup
object inside, the project properties.

Arranging child forms:


You can arrange the child forms inside an mdi forms in 2 different methods.

1) We can display the child forms by vertical coding.


2) We can display child form inside mdi form with horizontal coding.
3) We can arrange the child forms which refers to the project requirement while loading and
running your application.

Un Loading forms:
This will be works when you are closing the mdi form. If you are closing a child form, it does not
mean mdi form will be unloaded. If the user closes the application inside an mdi form, The entire
applications will be unloaded.
example: mdi coding

Private sub menuform1_click()

Form1.show

End sub

Private sub menuform2_click()

Form2.show

End sub

Private sub menuexit_click()

Unload mdi form

End sub

Form1:

Private sub menuexit_click()

End

End sub

B.Balaji (b.balag@gmail.com)
Page 45 of 62

Form2:

Private sub menuexit_click()

End

End sub

Error trapping and error tracing:

Error trapping:
Error trapping are used to prevent a error inside any applications. You can use a command, that
activates this type of errors, within a procedure or functions, it can be denoted by error go to label or
error line no. the label represents the current procedures or events which is having the error, will be
directly moved to the command, which throws the error message once you started error trapping within
the error go to command the run time error will not displayed for the application.

Syntax:
on error go to label(trap)

----------

----------

Error label=(trapping)

Err code

On error go to command can be added anywhere, inside on event, sub-procedures or functions


procedures. By default error trapping code will be retired inside the function procedures. The only
exceptions has been available inside any on errors statements, can be called any other function
procedures inside the application.

Resume statement:
For example, if an error occurs while running a program. On error go to command will be executed
automatically after display the error the program can resume using the resume statement inside the
function procedures.

Error tracing using error functions:


You can identify the problem of any error type by using err() method or err object. This wil give you the
built-in error, inside the vb application.

B.Balaji (b.balag@gmail.com)
Page 46 of 62

If you are using the error method, it will return a variable value, which can be used to display the error
methods or error names. By default, vb can identify 60 run time errors. You can able to find out capable
errors inside the vb using err object.

Example for run time errors:

Error code description

57 it is used to display the device i/o errors.

68 it is used to display the current availability of the device.

482 it is a general printer error, whenever the printer drive returns


some default, error messages.

483 it is used to display the error, when the printer device is not
supported.

Error functions and error objects:


You can use a error function, to display different type if built-in-error and user defined errors. To enable
this personality, you can use err method which is used to find out the errors occur inside the application.
To use this type of function, you need to create an object, which can assign the error messages to a
declared variable.

Syntax for enabling error object:


Err()

Dim variable name

Variable=err object

Eg:

Private sub cmd1_click()

Dim a

A=errorcode 57

Text1.text=a

End sub

B.Balaji (b.balag@gmail.com)
Page 47 of 62

Disabling error function and error object:


You can disable the error trapping either by error object or using an error function. If you are using an
error object, you need invoked a method called clear.

Syntax:
Err.clear

If you are using error go to statement, you need to reset the statement with 0. It can be denoted by an
error go to 0.

Example for error trapping:

Private sub cmd1_click()

Error number=err.number

Select case error number

Case 57

Msgbox”your printer may be offline”

Case 68

Msgbox”is there are a printer available 7”

Case 482.483

M$ =”please tell the operator (=program author?) that”

M$ = m$. & vb crlf = chr $ (10) + chr $ (13) new line

M$ =m$. & “a general printer error with” & error number & “occurred”

Msgbox m$

End

Case else

M$ =”please tell the operator (=program author?) that”

M$ = m$. & vb crlf = chr $ (10) + chr $ (13) new line

M$ = m$. & “error number” & error number & “occurred”

Msgbox m$

B.Balaji (b.balag@gmail.com)
Page 48 of 62

End

End select

End sub

Mouse monitoring activities:


Mouse monitoring activities in vb has basically has 2 events. This events are execute for all standard
control inside the vb application.

1) click event
2) double click event

there are five more special mouse events which are used in vb are:

1) mouse down event


2) mouse up event
3) mouse move event
4) mouse drag over event
5) mouse drag drop event

mouse down event:


this will be executed when the mouse has been clicked once inside any standard control. It is a similar
event like click event. It will be varied depending upon the button pressed while running the application.

Syntax:
Object name_mousedown (or) mouseup (or) mousemove( button as integer, shift as integer, x as single,
y as single)

-------------------

End sub

Mouse up event:
This event will be executed, whenever mouse button is released from the control or form. It has the
similar features for buttons.

B.Balaji (b.balag@gmail.com)
Page 49 of 62

Mouse move event:


This event is a special event in vb application. This event will be executed when the user use the mouse
pointer to any control, from a blank area inside a form (or) application. This event is more powerful in
any type of mouse events.

Since the user does not require to press any type of buttons in the mouse. This event will be fired with
the help of keyboard.

Mouse drag over event:


It will be fired, whenever any standard controls has been passed by pointer during the executed of vb
application.

Syntax:

Private sub objectname_dragover([index as integer]source as control, x as single, y as single)

-----------------

End sub

Mouse drag drop event:


It will be fired, whenever the user targets to any type of a standard controls from the source controls.
This event will be more powerful in run time application because the source control can be change the
desgined control. To enable this type of drag drop event, you need to change the drag mode property
for the source and designation control.

Arguments in mouse events:


All the five mouse events can use different types of buttons, available in mouse, you can able to use
different type of combination, according to the key events. All the button can able to utilize the different
type of events. The following are the button argument combination.

Buttons constrains value of a buttons argument

Left vb left 1

Right vb right 2

middle vb middle 4

Left and right vb left and vb right 3

Left and middle vb left and vb middle 5

B.Balaji (b.balag@gmail.com)
Page 50 of 62

Right and middle vb left and vb middle 6

Alt 3 vb right and vb left and vb middle 7

Usage of keyboard keys and mouse events:


In every vb application, you can able to add a keyboard keys and any mouse event you can able to see
the shift argument, which is used to detect, whether any keyboard keys have been added to the mouse
events. For shift arguments, you can use the control keys in the keyboard, such, ctrl, alt and shit keys.
The following table gives the parameter for shift arguments.

Action constrains value

Shift key vb shift mark 1

Ctrl key vb ctrl mark 2

Alt key vb alt mark 4

Shift and ctrl and alt key vb shift and ctrl and alt mark 7

Mouse drag over and mouse drag drop properties:


In every vb application, except menu and time control, the move drag over and mouse drag drop event
will be executed.

For every mouse drag drop and drag over events will be executed using x and y axis parameters, this
tells the event where the control to be moved or drag from the form.

Property name description

Drag mode property this property enables whether the drag mode is automatic or manual. If
the value = 1, then the drag mode property is in automatic mode. If the
value = 0, it is in manual dragging mode.

Drag icon property it sets the mouse icon, to user defined icon, while you enable the drag
mode property.

Drag method it enables to execute the start and stop methods inside the drag mode

Property. If you enable drag mode property. If you enable drag mode
property to manual.

Drag drop property it is used to allow the controls to drop the source control.

Drag over property it is used to control the source, while passing other controls during drag
option.

B.Balaji (b.balag@gmail.com)
Page 51 of 62

Example for mouseup:


Private Sub Image1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

Image1.Picture = LoadPicture

End Sub

Example for drag drop event:


Private Sub Image1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

Image1.Picture = LoadPicture

End Sub

Dialog box:
These are used to interact between the users and the common system usage dialog box are used to
interact between the users as a user friendly tool kid (vtk). Vb allows to create 3 difference type of
dialog box.

1) Pre-defined dialog box


2) Common dialog box
3) Custom dialog box

Predefined dialog box:


Predefined dialog box, consist of built-in controls like msg box and input box. This type of box will
appear when the user creates a methods using msg box and input box. By adding this type of method
the application will be prompted to user whenever this method has been fired. You can also add a local
variable, to store values from the pre-defined dialog box. there is no limit, by adding or remiving the
dialog boxes inside the applications. This type of dialog box are known as model dialog box.

Common dialog box:


This controls are very powerful tools that provide a set of standard boxes which helps the user for
opening a file, saving a file, printing the files and adding different type of fonts and colors inside the
applications.

Every application can have only one common dialog box control, you can use different type of methods
to execute the common dialog box control.

Methods description

B.Balaji (b.balag@gmail.com)
Page 52 of 62

Show open() this method is used to open an existing file from the selected directory, inside
your application. It will use file name and file type property.

Show save() it is used to open a save dialog box inside you current directory of your
application. You can also use the file filter property to extend the save method.

Show print() this method is used to print the current opened file from the application.

Show font() it will open a font dialog box and you can select the font style or font file for a
select text is the application.

Show color() this method is used to set the color for the selected text inside the application.

User defined dialog box or custom dialog box:


This are used to define a custom control, which executes your forms inside any vb application. This are
created by using built-in-forms inside an interface according to the users choice. You can also create
different type of form logics according to the user names for example, you can create a standard exe
form, which looks like, new application window, inside your operating system. If you are creating a
active X control, it will look like a new window control, on your application. For example, you can create
a custom common dialog box control also.

Difference b/w model and modules dialog box:


Model dialog box:

This must be closed before or you need to response before, you can continue with the rest of your
application. A dialog box which opens when your child an application. You need to respond the
application either by using or by not saving or resuming the application by cancelling.

Modules dialog box:

This type of dialog box are used to shift the focus from the dialog box without closing and move the
cursor to the current running application.

eg: adding 2 find and replace dialog box inside your application. It helps the user to switch owns the
focus b/w the dialog box and application.

Do event and sub main:


Do events:

These are used to a return a value from a function to the standard forms. This type of functions are
known as property functions or property procedures when you start up a applications in visual basic. By
default, every application will be started from either code module of a normal form or mdi form. If you

B.Balaji (b.balag@gmail.com)
Page 53 of 62

are not having a default start up object, you can able to manage with a do event function to load the
application. This function can be done inside sub-procedures code module window.

Sub main:

Sub main functions are used to create a separate starting page inside the application. To enable the sub-
main function to choose the project menu and then select the project properties and select the startup
object to sub-main function. This will open a separate code module window which are used to write a
procedures for enabling or disabling available forms inside the current applications.

Syntax:
Sub main()

Form objects

End sub

Eg:

Sub main()

Form splash.refresh

Form splash.show

Form1.show

End sub

Form splash used to check vb application with different type of form availability and it will sort out the
availability forms in current applications.

B.Balaji (b.balag@gmail.com)
Page 54 of 62

Unit-04

File system objects and file system control:

File system control:


It is used to access your system drivers, directories or folders and file system inside your computer.
Every file system control have a hierarchical featured to display a system environment. There are 3
different types of controls used n file system control.

1) File list box


2) Directory list box
3) Drive list box

File list box:


This method enables to display the file in the current directory list out. You can able to change the
different type of property inside file list box. For example, you can change the display list inside the file
list box to read only files.

Directory list box:


It enables to display the current directories from the drive list box. If the user changes the drive list box
names or drives, the directory list box will rearranges the hierarchy of the drivers. You can also double-
click on the directory list box to list out the corresponding sub directories or sub-folders. If the directory
has any type of files, the corresponding files will be displayed in file list box.

Drive list box:


It enables to control different types of drivers available in the system. Drive list box has a type of drop
down list box. Every drive list box will be display each and every disk availability inside the system.

File system objects:


It refers to file programming, inside any visual basic application. You can refer file objects either in
standard exe, active exe etc., inside the vb. It is a separate library objects which includes to create a file,
opening a file, editing a file, etc., the following are the steps to include file system object library.

1) Add a reference to the scripting library.


2) Make a new file system object reference.
3) In the new file system object, can able to handle, available drivers inside the system.
4) Using the driver object collection, you can get a folder collection.

B.Balaji (b.balag@gmail.com)
Page 55 of 62

5) Using the folder collection, you can able to list out the sub folders and file collection.
6) Using file collection, you can execute the different type of methods for creating, deleting,
copying folders, sub-folders and files.

Methods in file system objects:


Methods description

Copy file source des [overwrite] this method is used to copy file from source path to destination
path overwrite is used to explicit the file.

Copy folder source des [overwrite] it copies a folder or directories including sub –folders and files.

Create folder [folder name] it will create a new folder under the current driver with the
specific folder name.

Delete file [file specified] it will delete the file which has been specify under, file name.

Delete folder [folder specify] it will delete the folder, including sub-folder and files.

Move file source des [overwrite] it will move the specified file from the source path to
destination path.

Move folder source, des [overwrite] it moves the folder including subfolders and files.

Get base name it will return a string, that displays the name of a file or folder

Get file, [file specified] it will return a file object corresponding to the specified file
name.

Example for file system object:


Private Sub Dir1_Change()

File1.Path = Dir1.Path

End Sub

Private Sub Drive1_Change()

Dir1.Path = Drive1.Drive

End Sub

B.Balaji (b.balag@gmail.com)
Page 56 of 62

Private Sub File1_Click()

Image1.Stretch = True

Image1.Picture = LoadPicture(Dir1.Path & "\" & File1.FileName)

End Sub

Searching and sorting in vb:


Searching are used to find out the different types of logics in a database. You can able to find out
different types of algorithms search in vb. The most common searching algorithms in vb is binary search.
The following are the steps to find out the binary search.

1) Divide the database list into half


2) Determine whether, you are searching the list in first half of the result
3) If the list has gone too for you can loose into the second half of the result.
4) If the result is executed in second half. Eliminate step, and rearrange database list which single
database.

You can also use built in functions to find out the binary search.

For example, you can use a string compare functions, string length function, vbound and lboung
methods to find out the limits

Sorting:

Sorting a data is one of the most common jobs for a huge database, by using a sorting algorithms, you
can arrange the database orders and fetch the database values according to the users needs.

In vb there are four types of methods can be used for sorting.

1) Ripple sort.
2) Insertion sort.
3) Bubble sort.
4) Shell sort.

You can able to ass extra sorting algorithms inside vb application. Unfortunately some of the sorting
methodologies cant be proceed and difficult to write inside vb application.

Connecting with a database:


In every vb application, you can connect a database and fetch a database using different
standard controls. Every standard controls has application Programming interface (API) for connecting
with database program or database servers. Each control has their own programming interface for

B.Balaji (b.balag@gmail.com)
Page 57 of 62

connecting with database. Each and every control has different type of property which is used to
interact with database server.

1) Connection objects.
2) Data providers.

Connection objects:
This is used to maintain a connection to a database table. Every connection object will create a
command object and record set objects using a single connection object you can able to connect
multiple access to multiple database servers mainly, connection object is used to selecting the values,
inserting the values, updating the values and deleting the values from the table.

Properties in connection object:


Connection string:

It is a string data type containing the information which is used to connect the data source.

Providers:

It is used to decide the name of database server.

Command time out:

It is used to close the current connection object from the database server.

Default database:

It is used to select the default database server.

Connection object methods:

This is used to save the changes to the database table.

Open:

This is used to establish the connection to the data provider.

Close:

This is used to close the connection from the data provider. It also close all the transaction without
updating or saving the data tables.

Cancel method:

It is used to terminate the transaction without saving the values in data table.

B.Balaji (b.balag@gmail.com)
Page 58 of 62

Data providers:

Data providers are use to connect database server and vb application. For example data providers such
as, ms-access, oracle, sql server, etc.,

By default, vb as a database provider within the addins, named as visual data manager.

Types of databases:
Ms database programming has 3 different types of database object.

1) Data access object (DAO)


2) Remote data object (RDO)
3) Active X data object (ADO)

Data access object:


This controls to move around database records and display the record using different types of queries.
Dao uses data control which is used to move around the records in a API (application programming
interface).

Usage of dao object:

It is a standard control within the vb application x used to serve default database links. The default
database in dao object is ms jet database. Jet database uses link between the vb application and ms
access database.

Properties in dao control.

1) Database engine:

It is used to set the database provider name to the vb application. You can find different type of
available database serves inside the system. For eg, if the system has database server, link access,
sql server, oracle etc.,.

2) Work space:
It is used to add or more database table from the database engine.
3) Record set:
It is used to set the result. Query from the data table.
4) This property is used to return the columns from the selected record set table.

Remote data object:


This is used to connect the database using ODBC connection (open database connectivity).

B.Balaji (b.balag@gmail.com)
Page 59 of 62

Using rdo connection, you can link the database as a third party database to the vb application. Every vb
application can built rdo connection for client server application. Using ODBC active interface, you can
connect both local database Server X REMOTE DATABASE SERVERS.

Once you create a object in ODBC you can link the reference to rdo connection. The one main feature of
having a rdo object is you can create a database by defining the object of odbc interface.

Active x data object:


Active x data object model was created to replace both dao control model and rdo object model. It uses
a new ole db api(application program interface). Ms latest set of data object is known as active x object.
Adp object is used to work with variety of database such as oracle, sqp server, my sqp and all relational
database server. The following are the data providers which used in active x data object.

1) Ms jet 4.0 ole db provider.


2) Ms ole db provider for oracle server.
3) Ms ole db provider for sqp server.
4) Ms ole db provider for odbc driver.

Ado control:
Adodc is designed to create an connection to a database server form ms vb. This control is used to
create a dynamic connection to various database server using ole db provider. You can create a set of
record source and can create custom sqp statements to using the data control.

Properties in adodc:

1) Recordset:

It is used to set the database table from the ole db provider. It also used to move different type of
methods inside the adodc.

2) Fields:

It is an object reference, which used to select the columns from data table.

3) EOF (end of file) :


It is a Boolean value which is used to check the current record position in database.
4) Bof:
It is a Boolean value which is used to check the current record position, befor e the first record
in the record set property.
5) Data source:
It is an object reference, that indicates the data source owner, you are using any bound controls.
6) It is a string that contains the data source property which is used find the record set to the
bound control.

B.Balaji (b.balag@gmail.com)
Page 60 of 62

Methods in adodc control:


1) New:

It is used to add a new record into the recordset property.

2) Update:

It is used to update a record into the recordset property.

3) Delete:

It is used to delete a record into the recordset property.

4) Move first:

It is used to move the current record pointer to the first record into the recordset property.

5) Move last:

It is used to move the current record pointer to the last record into the recordset property

6) Move next:

It is used to move the current record pointer to the next record into the recordset property.

7) Move previous:

It is used to move the current record pointer to the presvious record into the recordset property.

Visual data manager:


This is used to create a built-in-database without vb. Visual data manager is a add-in which user jet
database engine to built database. It uses dao control as a default data base object, to run a visual data
manager, you need to choose add-ins menu x visual data manager in vb. It uses dao 4.6 to create the
database.

constructing database and table in visual data manager:


The most common uses of visual data manager utility is to create jet database object without using any
controls.

Creating a new database:


1) Start the visual data manager and choose file>view>ms access>version 7.0. this will display the
ms access database to create database name.
2) Using the access database you need to create database name and save it inside the appropriate
folders.

B.Balaji (b.balag@gmail.com)
Page 61 of 62

Creating a table:
1) After creating the database, right click anywhere inside the windows of database. This will
display a table structure for creating the table.
2) Enter the name of the table in table name field you should not add duplicate names.
3) To add column or fields click on add field dialog box, it will open a dialog window for entering
new columns.
4) You can choose different types of data types for each and every columns. You can alos use size
of the data type, default value for data type and validation for the variables.
5) After creating the columns, press on the built the table, to create the table.

Data environment:
This designer is a tool in vb to create a database application more faster than, any other database
controls. The data environment designer will work with ado object model technology dao and rdo object
model are not supported in data environment designer. To ass a data environment designer choose on
the project properties. Add data environment in designer tab and apply in to the vb application.

Connecting the data base:

To connect is database within data environment designer, you need to add the following commands.

Add connection:

It is used to add a connection object environment, which will be used to access a database server.

Add command:

It creates a new command object to the create environment which can use to execute an sql command.

Design:

It is used to access the sql query designer when you specify the sql statement data source for command
object.

View code:

It displays the code module for data environment.

Delete:

It removes a object from data environment.

Refresh:

It is used to refresh the selected object inside data environment.

B.Balaji (b.balag@gmail.com)
Page 62 of 62

Data report designer:


Ms data report is used to generate a report page for an application and database. It uses default control
to draw a report page.

Build-in data report structure:


To create data report, you need to add a data report designer from project properties designer tab.
After adding a data report you need to insert the connection object name from data environment
designer. After adding the connection object you need to link the command object from data
environment to insert the column in data report. There are seven types of templates structure in data
reports.

1) Reports header:
It is used to print only once within bigger font size of the report.
2) Page header:
It is used to print different type of titles of top of each page of the report.
3) Group header:
It is used to print different type of text for each and every command object in record set.
4) Detail:
It is used to print different type of each and every records retrieved from data type.
5) Group footer:
It is used to print at end of corresponding group header section.
6) Page footer:
It is used to print at the bottom of each page of reports.
7) Report footer:

It is used to print only once at the end of each report.

B.Balaji (b.balag@gmail.com)

You might also like