Академический Документы
Профессиональный Документы
Культура Документы
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
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
b = LCase(a)
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 = "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
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
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.
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.
it will have a form already included, type following code in form1’s code view:
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.
end sub
private sub mnuunderline_click()
text1.fontunderline=not text1.fontunderline
end sub
run the project and click on menu items.
Menu Submenu
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
Subroutine 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.
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
Height of list box can be changed Height of combo box is constant and can
not be changed.
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
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.
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.
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.
Sequential Random
Sequential does not use record of fixedRandom uses record of fixed length.
length.
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#
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.
MsgBox "name=" & Text1 & "age=" & Text2 & "salary=" & Text3
End Sub
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.
mnuclose &Close
mnuwindow &Window (turn on window list check box)
mnuarrange &Arrange Icons
mnucascade &Cascade
mnutilehor Tile &Horizontal
mnutilever Tile &Vertical
1.Byte 0 , 255 1
6.string(fixed) $
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
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
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
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.
9.Report generation
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.
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
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
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.
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
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.
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