Вы находитесь на странице: 1из 42

lOMoARcPSD|4339219

Unit-2- Notes FOR Visual Basic PART 2

Visual Programming (Bangalore University)

StuDocu is not sponsored or endorsed by any college or university


Downloaded by vipul kumar (vipulkumar7710@gmail.com)
lOMoARcPSD|4339219

 
NOTES FOR VISUAL BASIC PART 2
 
Write program using passing array to function to sort and find sum of element
of an array
'passing array to function
Private Sub Command1_Click()
Dim a%(4), x%
input_array a
sort_array a
print_array a
x = sum_array(a)
Print "sum=", x
End Sub
Private Sub input_array(a%())
'array is always passed by reference
Dim i%
For i = LBound(a) To UBound(a)
a(i) = InputBox("enter value for element")
Next
End Sub
Private Sub print_array(a%())
'array is always passed by reference
Dim i%
For i = LBound(a) To UBound(a)
Print a(i)
Next
End Sub

Private Sub sort_array(a%())


Dim i%, j%, t%

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

For i = LBound(a) To UBound(a)


For j = 0 To UBound(a) - 1 - i
If a(j) > a(j + 1) Then
t = a(j)
a(j) = a(j + 1)
a(j + 1) = t
End If
Next
Next
End Sub
Private Function sum_array(a%())
Dim s%
For i = LBound(a) To UBound(a)
s = s + a(i)
Next
sum_array = s
End Function
user interface:
command1: having caption set to input, print, sum of element

Write program to sum two matrix of order 3x3 using function.


Private Sub Command1_Click()
Dim a%(2, 2), b%(2, 2), c%(2, 2), i%, j%
MsgBox "enter value for elements of array a"
input_array a
MsgBox "enter value for elements of array b"
input_array b
sum_array a, b, c
print_array c
End Sub
Private Sub input_array(a%())
For i = 0 To 2
For j = 0 To 2

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

a(i, j) = InputBox("enter value")


Next
Next
End Sub

Private Sub sum_array(a%(), b%(), c%())


For i = 0 To 2
For j = 0 To 2
c(i, j) = a(i, j) + b(i, j)
Next
Next
End Sub
Private Sub print_array(c%())
For i = 0 To 2
For j = 0 To 2
Print c(i, j) & " ";
Next
Print
Next
End Sub
user interface:
command1: having caption input,add and print

Differentiate msgbox and inputbox

Inputbox Msgbox

Input box can be used to get user input of Msgbox can be used to get user input of
data and data can be numeric or string data in integer form only when used
function format of msgbox. when used
subroutine format of msgbox it can display
message only.

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

Input box has ok, and cancel button onlyMsgbox can have various button like, ok,
and text area to enter data. cancel, abort, retry, ignore etc. it can
display graphical symbol, we can set focus
to command button.

Input box has following syntax Input box has following syntax

Prompt: the text which will appear in client Prompt: the text which will appear in client
are are

Title:the text which will appear in title barbutton:button which will appear in client
area, the graphics which will appear and the
Default: the default value already will befocus.
shown in text area
title: the text which will appear in title bar
X,y : to position inputbox at particular
location Helpfile:specify help file which contents
help
Helpfile:specify help file which contents
help Context:specify help context id for input
box
Context:specify help context id for input
box

Write program to demonstrate various string functions.


Private Sub command1_click()
Dim d$()
a = "Hello Dear"
b = UCase(a)
Print "uppercase=" + b

b = LCase(a)

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

Print "lowercase=" + b

b = StrReverse(a)
Print "reverse string=" + b

a = "i am going"
b = StrConv(a, vbProperCase)
Print "proper case=" + b

a = "big mouse"
b = Mid(a, 2, 4)
Print "starting from position 2 and no. of character 4 of string 'big mouse' will
be=" + b

a = "big mouse"
b = Left(a, 3)
Print "leftmost 3 characters of string 'big mouse' is =" + b

a = "big mouse"
b = Right(a, 3)
Print "rightmost 3 characters of string 'big mouse' is =" + b

a = "big mouse"
b = Replace(a, "m", "h")
Print "when 'm' is replaced by 'h' string 'big mouse' becomes=" + b

a = " ranu "


b = LTrim(a)
Print "removing extra space of string ' ranu ' from left=" + b + "ok"

a = " ranu "


b = RTrim(a)
Print "removing extra space of string ' ranu ' from right=" + b + "ok"

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

a = " ranu "


b = Trim(a)
Print "removing space from both side of string ' ranu ' from right=" + b + "ok"

a = "i am beautiful"
d = Split(a, " ") ' each word will be stored in different
' element of dynamic string array d
Print d(0)
Print d(1)
Print d(2)
b = Join(d, ",")
Print " content of dynamic string array is joined by seperator comma:" + b

a = "i am beautiful"
n = InStr(a, "am")
Print " string 'am' appears in string 'i am beautiful' at position =" & n
End Sub

Explain date and time related functions


Private Sub Command1_Click()
‘differentiate now and date functions
Print "current date and time=" & Now
Print "shows or set current date only=" & Date

Print "shows or sets current time=" & Time


Print "date part of current date=" & Day(Now)
Print "month part of current date=" & Month(Now)
Print "year part of current date=" & Year(Now)

Print "hour part of current time=" & Hour(Now)


Print "minute part of current time=" & Minute(Now)
Print "second part of current time=" & Second(Now)

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

‘explain dateadd and datediff functions


Dim d As Date
d = #10/13/2004#
Print "three days added to date 10/13/2004 will be" & (d + 3)
Print "three days subtracted to date 10/13/2004 will be" & (d - 3)
Print "three months added to date 10/13/2004 will be" & DateAdd("m", 3, d)
Print "three days added to date 10/13/2004 will be" & DateAdd("d", 3, d)
Print "three year added to date 10/13/2004 will be" & DateAdd("yyyy", 3, d)

Print "10/13/2004 and 2/4/2003 has no. of days between=" & DateDiff("d",
#2/4/2003#, #10/13/2004#)
Print "10/13/2004 and 2/4/2003 has no. of months between=" & DateDiff("m",
#2/4/2003#, #10/13/2004#)
Print "10/13/2004 and 2/4/2003 has no. of years between=" & DateDiff("yyyy",
#2/4/2003#, #10/13/2004#)

End Sub

write program using format function to demonstrate use of format function


Private Sub Form_Click()
Dim d As Date, t As Single
d = #10/4/2004# 'date formatting
Print Format(d, "m/d/yy")
Print Format(d, "mm/dd/yyyy")
Print Format(d, "mmm/ddd/yyyy")
Print Format(d, "mmmmm,yyyy")
Print Format(d, "long date")
Print Format(d, "short date")

t = 23.45678 'number formatting


Print Format(t, "##.##")
Print Format(t, "####.##")

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

Print Format(t, "0,0.00")


Print Format(t, "000,0.00")
Print Format(t, "fixed")
Print Format(t, "scientific")

Print Format(t, "00,.00") 'divide no. by 1000 and show value


Print Format(t, "0000,.00")

u = "Raman" 'format string


Print Format(u, "<") 'in small letter
Print Format(u, ">") 'in capital letter

Dim v As Date 'formatting time


v = #10:12:20 AM#
Print Format(v, "hh:mm:ss AM/PM")
Print Format(v, "long time")
Print Format(v, "short time")
End Sub

Understanding User Interface:

What is form?
form is a container control it means that form can contain other control inside
itself. form is a control where programmer populates other control to construct
graphical user interface. a form is instance(object) of generic class ‘form’ and we
know object has certain properties or methods this is also true for form.

List out common properties of form/write any seven properties of form.


(i)Name: this is the property possessed by any control which lets the control
identified inside code.
(ii)Appearance: form’s look can be flat or 3d.
(iii)Autoredraw: if it is true control remembers what is drawn on surface of
control.

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

(iv)backcolor: controls background color of form.


(v)borderstyle: controls many settings related to border, sizing of
form,placement of buttons on title bar etc.
none:no title bar, no control box, no border
fixed: appears close button,title bar, border, form is not sizable.
sizable: this is default and appears titlebar, close, minimize, maximize,
controlbox all are visible.
fixedtoolwindow: appears close button and title bar with half height.
sizabletoolwindow: same as fiexedtoolwindow except it can be sized.

(vi) caption: returns or sets title bar text.


(vii) drawmode: drawmode controls behaviour of newly drawn pixel on form
with existing pixel.
(viii)drawstyle: controls line type of lines drawn e.g. dotted, dash dot.
(ix) drawwidth:outline width can be set or retrieve using this property.
(x)enabled: if enabled is true form can fire event as well as control placed on
form.
(xi)fillcolor: controls color of fill pattern.
(xii)fillstyle: allows to set fill pattern used to fill object which are closed.
(xiii)font: set font name which will be used by print statement.
(xiv)forecolor:color that controls outline color of drawing methods and print
statement.
(xv)height: gives height of form including title bar.
(xvi)icon: set icon which will appear in title bar.
(xvii)keypreview: keyboard event of object is invoked later on form’s keyevent.
(xviii)left: gives horizontal distance of form with respect to its container (screen
or mdiparent form)
(xix)maxbutton:set true if required maxbutton
(xx)mdichild:makes a form mdichild form if set true.
(xxi)minbutton: set true if required maxbutton
(xxii)mouseicon:set mousepointer property to custom and set mouseicon
property to your choice.
(xxiii)movable: if true form can be moved
(xxiv)picture: use this property to set form background picture.
(xxv)scaleheight: this property gives height of form without height of title bar.

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

you can use this property to divide overall height into a specified no. of units.
(xxv)scalewidth: this property gives width of form. you can use this property to
divide overall width into a specified no. of units.
(To be printed..)
Write any seven events of form.
(i)initialize: this event is called before load event. it is fired once.
(ii)load: this is an event which is fired automatically when a form is about to
show( due to call of method show) or load (due to call of method load).
(ii)resize: this event is fired once when form is about to show and fired next time
when size of form changes.
(iii)activate: this event is fired when form is about to show and fired next time
when current form becomes active.
(iv)deactivate: this event is fired when form becomes deactivate due to click on
title bar of other form.
(v)unload: this event is fired when form is about to close.
(vi)queryunload: this event is fired when form is about to close. this event has an
argument.
(vii)terminate: this is the last event fired when form is about to close.

describe methods of form.


method are as follows:
though there are many methods some are of related to graphics and some are
related to control working of form.
those which are related to working of form are as follows:
1. show: this method causes the form to show. if form is hidden it becomes
visible.
2.hide: this method hides the form but form still consumes memory in computer.
3.load:this method causes form to consume memory resources and whatever
statements are written in form_load and form_initialize are run but form is not
visible.
4.unload:this method is useful to free memory resources taken by form. if form
is visible it becomes invisible.

Write program using forms demonstrating use of multiple forms./ program to


add two nos. multiply to nos. etc. each applied on different form
start new standard exe project:

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

it will have a form already included, type following code in form1’s code view:

Private Sub Command1_Click()


Form2.Show
End Sub

Private Sub Command2_Click()


Form3.Show
End Sub

Private Sub Command3_Click()


Form4.Show
End Sub

Private Sub Command4_Click()


Form5.Show
End Sub

Private Sub Command5_Click()


End
End Sub

user interface for form1:


command1: set caption to ‘add’
command1: set caption to ‘sub’
command1: set caption to ‘divide’
command1: set caption to ‘multiply’
command1: set caption to ‘exit’

Private Sub Command1_Click()


Text3 = CSng(Text1) + CSng(Text2)
End Sub

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

Private Sub Command2_Click()


Unload Me 'me means current form
Form1.Show
End Sub

user interface for form2:


label1: set caption to ‘enter first value’
label2:set caption to ‘enter second value’
label3:set caption to ‘result’

command1: set caption to ‘add’


command2:set caption to ‘back’

similary design interface and code for form3, form4, form5 etc. but don’t forget
to change operator sign and caption of command button1.

Explain frame control. list the advantages of using a fame control in vb?
frame is a container control. it means that this control can be used to place other
controls inside it.
advantanges of frame control:
we can use frame control to place option buttons and create different set of
option buttons user can choose any one option button from each set. if we don’t
use frame we can not create different sets of option buttons and user can select
only on option button.
controls used on form if are logically related we can place them inside frame to
give use clue that those controls are interrelated thus increasing readablity.
frame control decorates form.
if we set enabled property to false of frame control all it child control will
become disabled and vice versa.
useful property: caption-the text which appear in frame upper left corner.
enabled-if set true child controls will be enabled otherwise disabled.

What are menus? explain the process of creating menus in vb:


menus are convenient means to place and organize commands that application

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

supports in structured manner. menu avoids putting of several command


buttons to initiate the same sequence of commands supported by application.
menu prevents overcrowding of form from command buttons.
menu is composed of following items:
menu bar: the place where first level menu items appear are known as menu
bar.the first level menu are placed on menu bar and provided hot keys and are
known as menu pads. menu pads can not have short-cut.
pulldown menu:when menu pad is activate pull down menu appears which is the
next level menu items.
submenu or cascading menu: second level menu item normally initiates a
command or another submenu( which is actually third level menu items).
Though we can have fourth level submenus yet these are not frequently used.
process of creating menu:
demo using menu to make text box text bold, italic, underline.
press control+e to bring menu editor dialog:
enter following information in menu editor dialog use left or right arrow, right
arrow key to change indentation level, next button to create new item as
necessary.

Caption Name Shortcut Indentation level

&Format MnuFormat First

&Bold Mnubold Ctrl+b Second

&Italic Mnuitalic Ctrl+i Second

&Underline Mnuunderline Ctrl+u Second

place a text box on form with name text1


and type following event procedure codes :
private sub mnubold_click()
text1.fontbold=not text1.fontbold
end sub
private sub mnuitalic_click()
text1.fontitalic=not text1.fontitalic

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

end sub
private sub mnuunderline_click()
text1.fontunderline=not text1.fontunderline
end sub
run the project and click on menu items.

describe options of menu editor.


options are as follows:
name:allows you to enter a control name for the menu item. a control nane is an
identifier used only to access the menu item in code; it doesn’t appear in a
menu.
index: allows you to assa numeric value that dermjines the control’s position
withing a control array. this position isn’t related to the screen position.
shortcut:allows you to select a shortcut key for each command.
helpcontextid:allows you to assign a unique numeric value for the conted id. this
value is ued to find the appropriate help topic in the help file indentified the
helpfile property.
negotiate position:allows you to select the menu’s negotiate position property.
the property determined whether and how the menu appears in a contain form.
checked:allows you to have a check mark appear initially at the left of a menu
item. it generally used to indicate whether a toggle option is turned on or off.
enabled:allows you to select wheterh you want eh menu item to respond to
events, or clear if you want the item to be unavailable and appear dimmed.
visible: allows you to have the menu item appear on the menu.
windowlist: determines if the menu control contains list of open mdi child forms
in an mdi application.
right, left, up, down arrows: allows menu item indent, outdent, move up, move
down.
menu list: a list box that displays a hierarchical list of menu items. submenu
items are indented to indicate their hierarchical position or level.
next: move selection to the next line.
insert, delete: insert inserts a new menu item and delete deletes a menu item.
ok: make menu item changes applied.
cancel: to abandon changes in menu items.

differentiate menu and submenu.

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

Menu Submenu

Menu contains submenu Submenu may contain other sub menu

Menu appear in menu bar Submenu appears in pull down menu when
menu pad is click

Menu is first level menu item in menu Submenu is second or further level menu
editor items

Menu cannot have shortcut keys Submenu can have short cut keys

Menu can have windowlist item turned onSubmenu does not uses this setting.
in case of mdi application

differentiate sub-procedure and function procedure.

Subroutine Function

Sub procedure is more commonly known asFuction procedure is more commonly


procedure only. known as function.

Subroutine does not return a value toFuction returns a value to calling routine
calling routine therefore a variable can not therefore a variable can appear in left side
appear in left side of assignment operator. of assignment operator.

Subroutine is used commonly when results Function is used commonly when a single
many result are being produced. result is being produced.

It uses sub..end sub keywords It uses function…end function keywords.

differentiate list box and combo box.

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

List box Combobox

List box contains list of items and user can Combo box contains list of items and user
select more than one items from list box can select any one item from list

List box does not have text area where userCombo box has text area and user can type
can type new item not in list portion. new item not in list

List box has two style settings: standard andCombo box has three style settings: simple,
check. check style puts check box beforedropdown and dropdown list.
each item and user can tick mark items to
select

It has properties : multiselect, columns,It has properties: sellength, seltext, selstart


selected, selcount which are not available in which are not available in list box.
combo box.

Height of list box can be changed Height of combo box is constant and can
not be changed.

It has itemcheck event. It does not fire itemcheck event.

Differentiate check box and option button.

Checkbox Option button

Check box control displays rectangle whereOption button displays circle where user
user can click to item select/unselect. can click to put dot to select item.

We can have multiple check boxes selected We can have only one option button
selected among set of option buttons

The value property of check box contains 1Option button value property is true when
if tick mark present or 0 if tick mark is

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

absent. dot is present in circle otherwise false.

It is not compulsory to put check boxes in It is compulsory to put option buttons in


container control like frame or picture boxcontainer control like frame or picture box
to create many sets. to create many sets

These controls are useful when we wantThese controls are useful when we want
user to select more than one items user to select any one out of more than one
items.

Differentiate binary and random file

Binary Random

It is to read or write data such as data of ole Random files are used to read write text
controls, image files, data field in a table ofdata in terms of fixed record length. less
data type blob(binary large object) etc. lesscommonly used to read/write binary data in
commonly used to read text data. terms of fixed length record.

It does not allow random access It allows random access of data.

Binary can make use of input function Random can not make use of input
function.

Type..end type statement is not useful. Type..end type statement is used to created
user defined data type which helps to
create fixed record length.

differentiate sequential and random access file.

Sequential Random

Sequential does not use record of fixedRandom uses record of fixed length.
length.

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

Sequential is useful to read text data in Random can be used to read text and binary
sequence. data in terms of fixed record length.

Sequential allows slow access of record Random allows fast access of record
randomly.

Sequential file can use following statementsIt can use get and put statement which can
which random file can not use: input,not be used by sequential file.
input#,line input ,print #,write#

differentiate image box and picture box.

Image box Picture box

Image box is designed specifically forPicture box is designed for displaying


displaying picture. picture as well as drawing graphics.

It consumes less system resources andIt consumes more system resources and
redraws picture fastly. redraws picture slowly.

Image box is not a contained control it Picture box is container control it means
means that other control can not be placedthat we can place other control inside it.
inside it.

If has properties and methods dis-similar toIt has properties and method similar to that
form. it has stretch property which whenof form therefore it is some times known as
set true picture is stretched to fit in imageform within form. it has autosize property
control. which when set true picture box resizes
itself to fit picture.

write short notes on keyboard events.


Private Sub Command1_Click()

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

MsgBox "name=" & Text1 & "age=" & Text2 & "salary=" & Text3
End Sub

Private Sub Text1_Change()


'this event is fired when textbox contain changes
End Sub

Private Sub text1_GotFocus()


'select text if text1 receives focus
Text1.SelStart = 0
Text1.SelLength = Len(Text1)
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)


'this event is fired when user presses a key reports ansi value of key pressed
If KeyAscii = 13 Then 'ascii value of enter key
SendKeys "{tab}"
End If
'avoid appearance of invalid keys
'keyascii for backspace is 8
If InStr("ABCDEFGHIJKLMNOPQRTSTUVWXYZ ,.", UCase(Chr(KeyAscii))) = 0 And
KeyAscii <> 8 Then
KeyAscii = 0
End If
'turn on letters to appear in caps
If KeyAscii >= 97 And KeyAscii <= 122 Then
KeyAscii = KeyAscii - 32
End If
End Sub

Private Sub Text1_Keydown(KeyCode As Integer, Shift As Integer)


'this event is fired after keypress , when key is released
'keycode reports code , shift reports control,shift,alter keys status

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

If Chr(KeyCode) = "A" And Shift = vbCtrlMask Then 'means control+A


Text1.SelText = "Mr."
ElseIf Chr(KeyCode) = "A" And Shift = vbAltMask Then 'means alter+A
Text1.SelText = "Mrs."
ElseIf Chr(KeyCode) = "A" And Shift = vbCtrlMask + vbShiftMask Then 'means
control+shift+A
Text1.SelText = "Miss"
ElseIf KeyCode = vbKeyF1 Then
Text1.SelText = "Welcome"
End If
End Sub

Private Sub Text1_Validate(Cancel As Boolean)


'this event is fired when text box is about to lose focus
'if cancel argument is assigned value true, text box can not lose focus
If Len(Trim(Text1)) = 0 Then
MsgBox "please type name"
Cancel = True
End If
End Sub
What is mdi form?/write short notes on mdi form/ what are benefits of mdi
form?.
mdi form is a form which can be used to create mdi application. only one mdi
form can be used in a project as soon as an mdi form is added the command
becomes unavailable. more than one mdi childs can be attatched to a single mdi-
parent form. some application like ms-word, ms-excel are using mdi concept. the
benefits of using mdi application are as follows:
user can open more than one document
data easily can be transferred from one document to other document.
we can view more than one file side by side using tile command of windows
menu.
we can not move document window( mdi child form) outside border of mdi
parent window.
when menu system is defined on both mdi-parent and mdi-child as soon as mdi

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

child window appear it replaces menu system defined in mdi-parent.


when menu system is defined only in mdi-parent appearance of mdi-child
window does not cause dis-appearance of mdi-parent menu.
closing of mdi-parent window causes closing of all mdi-child windows.

Write steps involved to create mdi form.


let us define steps involved in creating mdi forms:-
start a new standard exe project: project will contain a form name form1
from project menu select add mdi form : name of mdi form is mdiform1
select form1 and set its mdichild property to true
inside code view of mdiform1 :
declare an array by using statement
dim f(2) as new form1
in general declaration section
insert following statements in mdiform_load event of mdiform1
f(0).Caption = "document1"
f(0).Show
f(1).Caption = "document2"
f(1).Show
f(2).Caption = "document3"
f(2).Show
make the mdiform1 as startupobject : use project->properties->startup object-
>mdiform1
run the project using f5

What is short cut for running the project?


f5

Describe Select Case..End select/


select case.. end select statement
The control statement which allows us to make a decision from the number of
choices is called a select, or more correctly a select case – case else- end select.
Since these three keywords go together to make up the control statement. They
most often appear as follows:

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

select case(integer or string expression)


case is >= constant1:
do this
and this
case constant2
do this
and this
case constant 3 to constant 4
do this
do this
case constant5,constant6 , constant7
do this
do this
case else
do this
do this
end select

the integer expression following the keyword select-case is any ‘vb’ expression
that will generate an integer value or string constant. It could be an integer
constant like 1,2,3 or an expression that evaluates to an integer or any string
constant .
When a match is found, then program executes the statements following that
case, and all subsequent cases and default statements are not executedl. If no
match is found with any of the case statement, only the statements following
the case-else are executed. A few examples will show how this control works.

Write program to demonstrate mdi form arrange method, menu replacement


and opening and saving text file.
ans:
in form1 design following menu
mnufile &File
mnuopen &Open…
mnusave &Save…
mnusep -

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

mnuclose &Close
mnuwindow &Window (turn on window list check box)
mnuarrange &Arrange Icons
mnucascade &Cascade
mnutilehor Tile &Horizontal
mnutilever Tile &Vertical

in mdiform1 design following menu


mnufile &File
mnuopen &Open…
mnusep -
mnuexit &Exit

form form1 set mdichild property true


for mdiform1 set autoshowchildren property false
place text1 on form1 and set its multiline true and scrollbars to 3.
(A)type following code in form1:

Private Sub Form_Resize()


Text1.Move 0, 0, Form1.Width, Form1.ScaleHeight
End Sub

Private Sub mnuArrange_Click()


MDIForm1.Arrange vbArrangeIcons
End Sub

Private Sub mnucascade_Click()


MDIForm1.Arrange vbCascade
End Sub

Private Sub mnuClose_Click()


Unload MDIForm1.ActiveForm
End Sub

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

Private Sub mnuNew_Click()


MDIForm1.mnuNew_Click
End Sub

Private Sub mnuOpen_Click()


MDIForm1.mnuOpen_Click
End Sub

Private Sub mnuSave_Click()


Dim filename As String
If InStr(UCase(Me.Caption), "DOCUMENT") <> 0 Then
filename = InputBox("enter text file name to save with complete path")
Else
filename = Me.Caption
End If
Open filename For Output As #1
Me.Caption = filename
t = Text1
Print #1, t
Close #1
End Sub
Private Sub mnutilehor_Click()
MDIForm1.Arrange vbTileHorizontal
End Sub
Private Sub mnutilever_Click()
MDIForm1.Arrange vbTileVertical
End Sub

(B) put following coding in mdiform


Private Sub mnuExit_Click()
End
End Sub

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

Public Sub mnuNew_Click()


Dim f As New Form1
Static i%
f.Caption = "document:" & (i + 1)
i=i+1
f.Show
End Sub

Public Sub mnuOpen_Click()


'coding to open file
Dim filename As String
filename = InputBox("enter text file name with complete path")
Open filename For Input As #1
Form1.Text1 = Input(LOF(1), 1)
Form1.Caption = filename
Close #1
End Sub

What is meant by event driven programming in vb? Event driven programming


vs procedure oriented programming.
When a program consists of one or more than one event procedures and those
procedures accomplish the task to be done by program it is know as event driven
programming.
Event is any action performed by either performed by user or performed by
program itself. for example mouse-click, key press, window resize etc.

Structured/Procedure Oriented Event Driven Programming

Program consists of one or more procedure.Program consists of one or more event


procedure.

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

Programming task is completed byProgramming task is completed by event


procedure. procedure.

The order of execution of procedure is moreThe order of execution of event procedure


or less predictable. depends on user performed action.

Procedure oriented programming is notEvent driven programming is suitable for


suitable for window based application. window based application.

Write control instruction with example.


Control instruction decides the flow of control of the program. There are many
control instructions available in vb. Control instruction can be categorized as :
1. Conditional branching: conditional branching statement allows to execute
some statement under one condition and other statements under some other
condition. It is also known as selection statements.
it includes if..else..endif, select case… end select
2. Looping: if we want to run some set of statement again and again till the given
condition is true, we can use loop controls.
It includes do loop…while, do … while loop, while … wend, for…end for
For example of each control instruction please refer other pages in notes.

What are various type of operators in vb list them in each category.


arithmetic operator:
+ for addition,
- for subtraction.
* for multiplication
\ for integer division c=10\3 c will be 3\
/ for float division c= 10/3 c will be 3.333
^ for power e.g. c=10^3 therefore c will be 1000
mod for modulo division c=10 mod 3 c will be 1
relation operator
>,>=,<,<=,=, <>
logical operator

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

and, or, not


assignment
=

Write data types used in vb.


data type of variable defines range, memory and valid operation for a variable

Data type Range Memory

1.Byte 0 , 255 1

2.Integer % 32767 , 0 , -32768 2

3.Single ! +/- 3.4 e +34 4

4.Double # +/- 1.7 e+308 8

5.Date 1-1-100 to 31-12-9999 8

6.string(variable) $ 2 billion characters

6.string(fixed) $

7.boolean True, false 2

8.object Reference of any object

9. variant Range for number is same as


double

Range for string is same as 2


billion characters

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

What is mdi form, write five main differences between mdi and sdi application.
What is mdiform ? how will you create mdi parent and child form.
What is SDI? Explain it.
What do you understand by MDI? Write with example? How it is better than
SDI.
mdi form is a form which can be used to create mdi application. only one mdi
form can be used in a project as soon as an mdi form is added the command
becomes unavailable. more than one mdi childs can be attatched to a single mdi-
parent form. some application like ms-word, ms-excel are using mdi concept.
the benefits of using mdi application over sdi application are as follows:
user can open more than one document while in sdi only one document can be
opened at a time.
data easily can be transferred from one document to other document using drag
and drop and we can see content of more than one document at a time.
we can view more than one file side by side using tile command of windows
menu.
we can not move document window( mdi child form) outside border of mdi
parent window.
when menu system is defined on both mdi-parent and mdi-child as soon as mdi
child window appear it replaces menu system defined in mdi-parent.
when menu system is defined only in mdi-parent appearance of mdi-child
window does not cause dis-appearance of mdi-parent menu.
closing of mdi-parent window causes closing of all mdi-child windows.
Sdi application on the other hand allows to open only one document at a
time. If we have already open a document and try to open another document
the application will ask to close the current document only then we can open
other document.
Steps involved to create mdi form.
let us define steps involved in creating mdi forms:-
start a new standard exe project: project will contain a form name form1
from project menu select add mdi form : name of mdi form is mdiform1 by
default and this form will be mdiparent form
select form1 and set its mdichild property to true then this form will be mdichild
form.
inside code view of mdiform1 :
declare an array by using statement
dim f(2) as new form1

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

in general declaration section


insert following statements in mdiform_load event of mdiform1
f(0).Caption = "document1"
f(0).Show
f(1).Caption = "document2"
f(1).Show
f(2).Caption = "document3"
f(2).Show
make the mdiform1 as startupobject : use project->properties->startup object-
>mdiform1
run the project using f5

Describe scope of variable.


What do you mean by scope and duration of a variable.
Scope specifies the code range where value of variable is
accessible.
local variable: accessible inside a function or procedure. we use dim keyword to
declare local variable.
module level global variable: accessible inside a module but not outside from
other module. we use dim or private keyword to declare module level global
variable and declare it inside general declaration of module.
global variable : accessible from every where. we use public or global keyword
to declare global variable inside general declaration of module.
Duration of a variable is also known as life time of variable, it means how long
memory remain allotted to a variable?

Explain procedure in vb.


What is procedure?
a number of statements grouped into a single logical unit is
referred to as a procedure or subroutine which does not return
any value and which is made to complete a specific task. A
program can be made of many procedures.
Usages are as follows:
Function/procedure avoids code repetition:- once a function/procedure has
been made we can call the function/procedure to complete the task when and

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

where necessary.
Function/procedure used carefully makes the process of error removing easier
called debugging.
Once function/procedure is tested and satisfies the one’s needs, one can convert
it in to library, and can distribute it others for use.
Function/procedure allows breaking bigger task into smaller manageable
subtasks which is soul of modular programming.
Recursive function/procedure (function/procedure calling itself at least once)
solves some typical programming tasks easily and with few lines of coding which
otherwise would have taken several lines of code.
Example of procedure:
Demonstration of call by value and call by reference
'when called routine is not able to change value of actual argument
'through dummy argument it is known as call by value
Private Sub Command1_Click()
Dim a%, b%
a = Text1
b = Text2
swap a, b
'after call
Text5 = a
Text6 = b
End Sub
Private Sub swap(ByVal a%, ByVal b%)
'default is call by reference
Text3 = a
Text4 = b
Dim temp%
'interchange
temp = a
a=b
b = temp
End Sub

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

call by reference:
'when called routine is able to change value of actual argument
'through dummy argument it is known as call by reference
Private Sub Command1_Click()
Dim a%, b%
a = Text1
b = Text2
swap a, b
'after call
Text5 = a
Text6 = b
End Sub
Private Sub swap(Byref a%, Byref b%)
'default is call by reference
Text3 = a
Text4 = b
Dim temp%
'interchange
temp = a
a=b
b = temp
End Sub
user interface:
6 textboxes: having text property clear and having default name text1,text2…
6 labels: having caption enter value of a, enter value of b, within called
subroutine value of a, within called subroutine value of b, after call to swap value
of a, after call to swap value of b
1 command button: having caption swap and having default name command1

What is ide? Discuss any five components.


Ide stands for integrated development environment. it is
collection of program allowing to compose and edit the program, test the
program, run the program and debug the program.
Major components of vb Ide:

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

1. Project startup dialog box: when we start vb program we get a dialog box
which gives choice of project type to start.
2. Project explorer window: project explorer window allows browsing different
modules that make up our project. We can switch from one module to other
module. We can see the object view, code view of the module. We can make
save and save as the module, we can add new module from within the project
explorer window.
3. Form layout window: using form layout window we can set the startup
position of a form on screen.
4. Immediate window: using this window we can set new value for a variable
while the project in debug window. We can check syntax of statement and
function and we can use it as calculator also.
5. Toolbox: toolbox contains various controls which we can place on form.
6. Properties window: using this window we can set properties of a selected
control or module.
7. Form window: here we can place various control to design GUI for program.

Describe in brief all mouse events with example.


Explain mouse events in VB
Mouse events are those events which are generated by mouse actions such as
mousedown: this event is fired when mouse button is hold down. This event
reports mouse coordinate and button number which was press
mouseup: this event is fired when mouse button is left up. This event reports
mouse coordinate and button number which was press
mousemove: this event is fired when mouse is moved. This event reports mouse
coordinate.
click: this event is fired when mouse button is hold down and left up. This event
reports mouse coordinate and button number which was press
dblclick : this event is fired when mouse button is double clicked. This event
reports mouse coordinate and button number which was press
dragdrop: : this event is fired when control is dragged and dropped over other
control.
dragover: this event is fired when control is being dragged and enters the region
of control on to which it is to be dropped.
Start new standard exe project in vb and in code view window type following
code to observe mouse events:

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

Private Sub Form_Click()


Print "i am mouse click"
End Sub

Private Sub Form_DblClick()


Print "i am mouse dblclick"
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y


As Single)
Print "i am mouse down reporting mouse coordinate and button"
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y


As Single)
Print " i am mouse move"
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As


Single)
Print "i am mouse up reporting mouse coordinate and button"
End Sub

9.Report generation

Explain data report.


or
Write down the step of generating report in vb through data environment
or
What do you mean by data report and how will you create datareport? Explain
crystal report.
Data report
Data report is a reporting tool available in vb6.0. it is new feature of vb6.0. We
can create, maintain, and access databases from within a visual basic application.

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

The final piece of this set of building blocks is the capability to report on the data
stored in the database. With vb6.0 we have a built-in reporting designed that
works with the new data environment. Data report designer gives the capability
to create database reports without using any third-party tools such as crystal
report writer. It has following
Features of data report:
drag and drop field placement: we can drag and drop the fields needed in report
from data environment and vb automatically sets data member and data field
property.
Toolbox controls: the data report designer has its own set of control, which are
automatically added to the toolbox on their own tab named Data report
Print preview: we can print the report in code by using the printreport method
or clicking the print button on the toolbar when in preview mode.
File export: we can export the report in html, text or Unicode format using the
exportreport method.

Steps of creating Data report using data environment:


Suppose there is a ms-access database file having name : StudentInfo.mdb
Suppose it contains a table having name:student
Suppose the table contains following fields: rollno, name, class
It is required to prepare the report file then the steps required will be as follows:
(1) Settting the data environment:
a. project-> add dataenvironment
b. in project explorer window double click on dataenvironment1
c. right click on connection1 and select properties
d. double click on Microsoft jet 4.0 ole db provider
e. on select or enter database name click command button with caption …
f. select the database studentinfo from the location
g. click on ok to close data link properties dialog box.
h. right click on connection1 and select add command.
i right click on command1 and select properties
j on database object select table
k on object name select student
l click on ok
(2) preparing data report

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

a. project-> add data report


b. select the data report and set its data source property to dataenvironment1,
and data member property to command1
c. right click on data report and select retrieve structure and click on yes
d. drag the command1 from dataenvironment1 and drop it on detail section
properly adjust the fields on detail section
click on rptlabel on toolbox on data report tab
and draw it on report header section and set its caption property to “Student
List”
(3) set data report1 as startup object for this, use project->properties->start up
object-> datareport1
save and run the project

Crystal Report:
We can create, maintain, and access databases from within a
visual basic application. The final piece of this set of building
blocks is the capability to report on the data stored in the
database.
Crystal report is a third party report writing tool. It is created by Seagate
technology it ships with visual basic6.0. Newer version of crystal report writer
9.0 supports use of data environment as data source and variety of other data
sources that be used with crystal report 9.0. it takes approximately 300 mb of
Hard disk space to install
drag and drop field placement: we can drag and drop the fields needed in report
from data environment and crystal report automatically sets data member and
data field property.
Toolbox controls: the crystal report designer has its own set of control
Print preview: we can get print preview.
File export: we can export the report in html, text or Unicode format
Provides it own set of formulas for complex calculation.
Provides reporting based on condition which makes a single report to be used on
multiple situations.

What are dialog boxes? Explain different type of dialog boxes available in vb?
Dialog boxes are windows that are quite commonly used for displaying some
message or to collect some data from the user so as to perform some operation.
Dialog boxes have fixed border and cannot be resized. It has close button, title

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

bar and we can work on any other window in same application until we close it
normally.
VB offers two dialog boxes ready to use

Inputbox Msgbox

Input box can be used to get user input of Msgbox can be used to get user input of
data and data can be numeric or string data in integer form only when used
function format of msgbox. when used
subroutine format of msgbox it can display
message only.

Input box has ok, and cancel button onlyMsgbox can have various button like, ok,
and text area to enter data. cancel, abort, retry, ignore etc. it can
display graphical symbol, we can set focus
to command button.

Input box has following syntax Input box has following syntax

Prompt: the text which will appear in client Prompt: the text which will appear in client
are are

Title:the text which will appear in title barbutton:button which will appear in client
area, the graphics which will appear and the
Default: the default value already will befocus.
shown in text area
title: the text which will appear in title bar
X,y : to position inputbox at particular
location Helpfile:specify help file which contents
help
Helpfile:specify help file which contents
help Context:specify help context id for input
box
Context:specify help context id for input
box

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

What is property?
property of any object is actually member procedure which allows to set value in
data member ( working as mutator) or allows to retrieve value of data member
( working as accessor).
Properties of textbox for example are :
name: this is property by which a textbox can be differentiated from other
control
height: height of the text box control
width: width of the text box control
locked: controls whether content of text box can be edited(locked:false) or
cannot be edited(locked:true)
enabled: controls whether the text box can fire event or not.
text: controls the text that are to be shown in text box Etc.

What do you understand by front-end tool and back-end tool?


Front end-tool normally provide graphical user interface or collect data or
service request from the user. Front end tool require some data in order to fulfill
requirement of user service for which it depends on back-end.
Programming languages normally works as front end tool. For example visual
basic application for library management system is a front end tool. In order to
save, retrieve and report data it requires support of back end tool such as ms-
access, oracle etc.

What are the connectivity features?


or
What is DAO, ADO AND RDO?
or
Write the features of ado and difference between ADO, RDO and DAO
ADO(ActiveX data access Object):
It uses Universal data access specification that means ADO can use data from
variety of data sources. ADO is the application programming interface used to
access information. Using ADO we can perform the following sequence of
actions.

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

Connect to a data source. Optionally you can ensure that all changes to the data
source occur either successfully or not at all
Specify a command to gain access to the data source
Execute the command if the command causes data to be returned in the form of
rows in a table, store the rows in a cache that you can easily examine,
manipulate or change.
if appropriate, update the data source with changes from the cache of rows
provide a general means to detect errors.

Features of ADO
Typically we will use all theses steps in the programming model. However, ADO
is flexible enough that we can do useful work by executing just part of the
model.
ADO object hierarchy is less hierarchical than RDO. Size of ADO object library is
less than RDO and DAO. ADO is faster than RDO in performance. ADO provides
greater no. of events than RDO and DAO. It uses command, connection, error,
field, parameter, property, recordset objects. It uses errors, parameters, fields,
properties collections

DAO(Data access Object):


DAO is the oldest data access technology available in vb. Primary it was
developed to write desktop database application with no client/server data base
application development in mind.
It can uses data through MS-jet database engine and ODBC. DAO object
hierarchy is hierarchical. Size of DAO object library is larger than RDO and ADO.
DAO is slower than ADO and RDO in performance. DAO provides least no. of
events than ADO and RDO.
It uses dbengine, workspace, error, database, tabledef, querydef, recordset,
field, index, parameter, user, group, relation, property, container and document
objects.
It uses databases, users, groups, properties, errors, querydefs, recordsets,
containers, relations, tabledefs, fields, indexes, parameters, connections,
workspaces, collections.

RDO( Remote Data access Object)


RDO was developed after DAO. It was primary aimed to develop client/server
database application development.
It can use data through ODBC. ODBC was created to access data from only

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

relational database. RDO object hierarchy is less hierarchical than DAO. Size of
RDO object library is less than DAO. RDO is slower than ADO in performance.
RDO provides greater no. of events than DAO.

Difference among ADO, DAO, RDO

ADO DAO RDO

It uses Universal data accessIt can uses data through MS-It can use data through
specification that means ADOjet database engine andODBC. ODBC was created to
can use data from variety ofODBC access data from only
data sources. ADO is the relational database.
application programming
interface used to access
information.

ADO object hierarchy is lessDAO object hierarchy isRDO object hierarchy is less
hierarchical than RDO hierarchical hierarchical than DAO

Size of ADO object library isSize of DAO object library isSize of RDO object library is
less large less than DAO

ADO is faster than RDO inDAO is slower than RDO inRDO is slower than ADO in
performance performance performance

ADO provides greater no. ofDAO provides least no. ofRDO provides greater no. of
events than RDO events events than DAO

It uses command, connectionIt uses dbengine, workspace,


, error, field, parameter,error, database, tabledef,
property, recordset objects. querydef, recordset, field,
index, parameter, user,
group, relation, property,
container and document
objects.

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

It uses errors, parameters,It uses databases, users,


fields, properties collections groups, properties, errors,
querydefs, recordsets,
containers, relations,
tabledefs, fields, indexes,
parameters,connections,
workspaces, collections.

What is dynamic array? what are the advantages of Dynamic Array?


dynamic array is an array whose no. of elements are not specified earlier. no. of
elements can be requested from user at program run time. redim keyword can
be used to allocate memory for such no. of elements. after allocating memory
we can store value in elements of array.
dynamic array saves memory because we have to allocate memory for such
many elements which we want to use.
Example : program to sort an array using dynamic array
Dim a%(), n 'note element is not specified
Private Sub Command1_Click()
Dim i%
' take no. of elements required
n = InputBox("enter no. of elements")
'allocate memory
ReDim a(n - 1) ' workout
For i = LBound(a) To UBound(a)
a(i) = InputBox("enter value", "data entry", 1)
Next
End Sub

Private Sub Command2_Click()


Dim i%, j%
For i = LBound(a) To UBound(a) - 1
For j = LBound(a) To UBound(a) - 1 - i

Downloaded by vipul kumar (vipulkumar7710@gmail.com)


lOMoARcPSD|4339219

If a(j) > a(j + 1) Then


t = a(j)
a(j) = a(j + 1)
a(j + 1) = t
End If
Next
Next
End Sub

Private Sub Command3_Click()


Dim i%
For i = LBound(a) To UBound(a)
Print a(i)
Next
End Sub
place command1:having caption set to input
place command1:having caption set to sort
place command1:having caption set to print

Downloaded by vipul kumar (vipulkumar7710@gmail.com)

Вам также может понравиться