Unit 3 Notes For Visual Basic Part 3 PDF
Unit 3 Notes For Visual Basic Part 3 PDF
Unit 3 Notes For Visual Basic Part 3 PDF
NOTES FOR VISUAL BASIC PART 3
Write various data conversion function along with its syntax.
Functions whose name begins with c<type> convert an express from any data
type into the specified data type.
Sample syntax:
A=cbool(int) ‘convert from integer to Boolean
B=cbyte(int) ‘convert from integer to byte
C=ccur(dbl) ‘ convert from double to currency
D =cdate(dbl) ‘ convert from double to date
E=cdec(hex) ‘convert from hexadecimal to decimal
F=cdbl(vint) ‘ convert from variant to double
G=cint(long) ‘ convert from long to integer
H=clng(vint) ‘ convert from variant to log
I= csng(curr) ‘ convert from currency to single
J=cstr(dbl) ‘covert from double to string
K=cvar(lng) ‘convert from long to variant
5.Working with controls
Explain the use of progress bar in vb. Write the step to print a progress bar on
form.
or
Explain the use of progress bar and timer control in vb.
Progress bar control is a control to give the user feedback that some process is
going on and the user must wait for some time to finish the current on-going
process. Progress bar uses timer control which helps in fill in the progress bar.
Example: following example let’s the user wait for two seconds
Steps
start new standard exe project
project->components -> place tick mark before Microsoft windows common
control 6.0 (sp2)->ok
place progress bar, and timer control on form.
set timer inverval to 100 ( timer event of timer control will fire in every 1/10 th
second)
set max to 60 and min to 0 for progress bar control
right click on form and select code view window and type following code
Private Sub Timer1_Timer()
If ProgressBar1.Value < 60 Then
ProgressBar1.Value = ProgressBar1.Value + 6
End If
End Sub
What do you mean by ODBC.
ODBC stands for open database connectivity. It is set of application programming
interface written generally in c, c++ and assembly language to interact with wide
variety of database supplied by different database vendors.
ODBC provides a set of application programming interface that all developers
could use, to connect and use databases and it was ODBC’S and ODBC driver’s
responsibility to figure out how to talk to the database. As long as an ODBC
driver was available for the database management system you were using, you
only had to write your application to access the ODBC API. If an ODBC driver was
not available, you had have to write to that DBMS’S API, or you had have to find
some other method to access the database though some middleware product,
such as Intersolv’s Q+E data drivers.
Write a program for storing the item database with field item name, item
number, price, rate per item in sequential file and read data from the file.
Steps
start new visual basic standard exe project
place five labels, five text boxes and two command buttons
set following properties:
label1 caption enter name
label2 caption enter item number
label3 caption enter sale price
label4 caption rate per item
text1 text clear
text1 name txtname
text2 text clear
txtrecord = txtrecord + "," + a
Input #1, a
txtrecord = txtrecord + "," + a
Input #1, a
txtrecord = txtrecord + "," + a + vbCrLf
Loop
Close #1
End Sub
Differentiate different type of recordset.
Recordset is set of records in memory resulted due to execution of query.
We can use sql query for thisWe can use table name andWe can use table name and
type of record we can not use sql query sql query
It may take less than or equalIt takes higher memory It may take less than or equal
to memory taken by table to memory taken by table
type type
Write short notes on VBP.
or
What is project file? why it is needed?
project file contains information about all those file which make the project. it
stores location of individual file with path. when saving project it saves all files
one by one inside project. when we open project it automatically opens all files
which are part of project we need not to open each file one by one thus saving
time. project file has extension “vbp”.
Write short notes on list box and combo box
List box:
List box contains list of items and user can select more than one items from list
box
List box does not have text area where user can type new item not in list portion.
List box has two style settings: standard and check. check style puts check box
before each item and user can tick mark items to select.
It has properties: multiselect, columns, selected, selcount which are not available
in combo box. Height of list box can be changed. It has itemcheck event.
Combo box:
Combo box contains list of items and user can select any one item from list.
Combo box has text area and user can type new item not in list. Combo box has
three style settings: simple, dropdown and dropdown list. It has properties:
sellength, seltext, selstart which are not available in list box. Height of combo
box is constant and can not be changed.
It does not fire itemcheck event.
Both list box and combo box can be used to give user many choices and user can
select choice from the list.
Following are the common properties:
list : it is an array property which holds the item in the list portion.
listindex: it is a property that gives index of the item selected and we can make
an item selected. If no item is selected then it value is -1.
sorted: if true then items are arranged.
listcount: gives no. of items in list portion.
Following are the common methods:
clear: it is useful to remove all items from list portion.
additem: it is useful to add an item in list portion.
removeitem: it useful to remove an item from list portion.
Toolbar Menu bar
Toolbar doesn’t contain highlight character Menu bar contains highlight characters
allows random access of data. Random can not make use of input function.
Type...end type statement is used to created user defined data type which helps
to create fixed record length.
Sequential:
Sequential does not use record of fixed length. Sequential is useful to read text
data in sequence. Sequential allows slow access of record. Sequential file can use
following statements which random file can not use: input, input#, line
input ,print #,write#
Random:
Random uses record of fixed length. Random can be used to read text and binary
data in terms of fixed record length. Random allows fast access of record
randomly. It can use get and put statement which can not be used by sequential
file.
Write short notes on Form Layout Window.
Form layout window is useful to set startup position of the form on screen
graphically. To use form layout window we must run our project at least once
then icon of the form appears in form layout window. Using mouse we can
adjust position of form icon within form layout window. Now if we run the
project the form will be in new position according to setting done within form
layout window.
table-type: it represents the base table in a code form. It can be used to add,
change, or delete records from a single database table.
dynaset type: it represents the result of a query that can have updatable records.
It can be used to add, change, or delete records from one table. Fields can come
from one or more database tables.
snapshot type: it is a static copy of a set of records that you can use to find data
or generate reports. It can contain fields from one or more database tables. It is
not updateable.
forward only: this recordset is identical to a snapshot except that no cursor is
provided. You can only scroll forward through records.
dynamic type: it represents a query result set from one or more base table in
which you can add, change, or delete records from a row-returning query.
Further the records added, deleted or edited by other users, in the base tables
also appear in your recordset.
Write short notes on bound controls
A bound control is also called data-aware control is a control that can provide
access to a specific column or columns in a data source through a data control. A
data-aware control can be bound to a data control through its datasource and
datafield properties. When a data control move from one row to the next, all
bound controls connected to data control change to display data from columns
in the current row. If user changes in a bound control and then move to a
different row, the changes are automatically saved in the data source.
For e.g.
Textbox, checkbox, label etc.
What is control Array? What is its utility?
A control array is group of controls that share same name and type. A control
array has at least one element and can grow up to 32767 elements. The
elements of the same control array may have their own property settings
The control arrays are useful for following things:
control arrays use fewer resources than multiple controls of same type.
code can be shared among the elements of a control array.
Write short notes on activex control.
Or
.ocx
Activex controls which exist as separate files within .ocx file name extension. An
activex control is an interactive object that can reside on any form that supports
OCX( Ole controls) controls. An activex control is an extension to the visual basic
toolbox. You can use activex controls just as you would use any of the standard
built-in controls such as a scrollbar. Theses include controls that are available
only in the profession and enterprise editions. Many third-party activex controls
are also available.
Adding activex controls to the toolbox
from the project menu, choose components
select the check box next to the name of the desired .ocx control, then choose
ok
To remove an activex control
Remove all instances of the control from the forms in your project. Delete any
references to the control in the project’s code. If references to a deleted control
are left in your code, an error message will display when you compile the
application.
from the project menu, choose components. Clear the check box next to the
name of the .ocx control, and then choose ok.
Write short notes on Ole control.
The ole container control enables you to add insert able object to the forms in
visual basic project. With ole control we can:
Create a placeholder in our application for an insert able object. At run time we
can create the object that is displayed within the ole control or change an object
we placed within the ole container control at design time.
Create a linked object in application.
Bind the ole container control to a database using the data control.
Commonly used properties:
class: the property that returns the name of the application that produced the
object.
sourcedoc: the property that returns the data or a reference to the data.
sourceitem: the property that returns the image of the data. This applies to
linked objects only.
sizemode: the property controls how ole control is sized
oletypeallowed: this property determines whether source document is
embedded, linked or both
Commonly used method
createembed : useful to create embedded object.
createlink: usefult to create linked object.
What are the categories of controls?
There are three categories of controls in vb:
intrinsic controls: these controls are contained inside the visual basic .exe file.
Intrinsic controls are always included in the toolbox.
activex controls: it exists in separate files having .ocx extension.
insertable objects: some objects such as Microsoft excel sheet, word document
can be added into toolbox and can be used like control.
Write short notes on Drawing Methods.
Following are the graphics methods:
cls : clears drawing area
line: using this method line can be drawn, even rectangle can be created.
circle: using this method we can draw arc, circle, pie slice and ellipse
pset: it is used to draw a point
print: we can print some text on control
What is dll?
Dll stands for dynamic link library file. Dll file contains some functions and
subroutines written in any language that supports creation of dll files. More than
one application programs can call functions and subroutines in a dll file. More
than one copy of dll file is not needed if more than one application programs are
using same function or subroutine because dll file is used in shared environment.
So disk space is saved and memory space is also saved.
Write short notes on animation in vb
A common user for timer control is to create graphics animation, because the
way you create animation is by displaying successive frames of the animation
sequence at intervals. That is good job for the timer control.
To see how this works, we will create an example now. In this example, we will
just switch back and forth between two simple images, image1.bmp and
image2.bmp, which are simple strips of solid color, red and blue.
Step
start new standard exe project
place a picture box control and timer control
set interval property to 1000 for timer control and enabled property to true
add following code to code view window
private sub timer1_timer()
static blnimage1 as Boolean
if blnimage1 then
picture1.picture=loadpicture(“image1.bmp”)
else
picture1.picture=loadpicture(“image2.bmp”)
end if
blnimage1=not blnimage1
end sub
Write short notes on on error, resume statements in VB.
Vb provides many tools for debugging project
on error goto label/line no: when you execute an on error goto label/line
no statement in your code execution is transferred to the code starting at label if
a trappable error has occurred. The code following that label is your error
handler.
on error goto 0: if you want to turn off error trapping at some point in your
code, you can execute the statement on error goto 0 just before that piece of
code . then again you can give on error goto label statement for error trapping
after that piece of code.
on error resume next: on error resume next statement provides an easy
way to disregard errors, if you want to do so. Once you execute this statement,
execution continues with the next line of code if the current line generates an
error, and the error is disregarded.
resume: when you are writing code for an error handler, you can return
control to the main body of the procedure using the resume statement. Program
execution starts again with the line that caused the error, and this can be very
valuable if you are able to fix the error in the error handler.
resume label: when your are writing code for an error handler, you can
return control to a particular line in the main body of the procedure using the
resume label statement. To a label a line, you just place the label’s text directly
into the code, followed by a colon.
resume next: this statement resumes program execution in the
line after the one that caused the error.
TEXTBOX CONTROL
textbox control is useful to get data from the user and to display output of the
program.
Following are the common properties of text box control:
a. name: this is useful to identify one control among several controls inside code.
b. height: controls height of the control.
c. width: controls width of the control.
d. align: controls alignment of text inside text box.
e. multiline: if true then multiline text can be typed otherwise not.
f. scrollbars: you can set horizontal, vertical or both scrollbars this will work only
when multiline property is set true.
g.locked: if true user cannot change text in textbox.
h.enabled: if true textbox can generate event.
i. selstart: decides the position from which selection of text will start.
j.seltext: refers to the text selected in text box
k.sellength: no. of characters selected
Following are the useful events:
a. keypress: this event is fired when we press a key.
b. keydown: when key is hold down this event is fired. This event reports
extended key such as alter,control, shift combination which were used while
pressing standard key or not.
c . keyup: when key is released this event is fired. This event reports extended
key such as alter,control, shift combination which were used while pressing
standard key or not.
d. change: this event is fired whenever content of textbox alters.
e. gotfocus: when focus is received by textbox
f. lostfocus: when focus is lost by textbox.
Example-1 keypress and gotfocus,lostfocus
Private Sub Command1_Click()
Text3 = Val(Text1) + Val(Text2)
End Sub
Private Sub Form_Load()
Text3.Enabled = False
End Sub
Private Sub Text1_GotFocus()
Text1.BackColor = vbYellow
End Sub
Private Sub Text2_GotFocus()
Text2.BackColor = vbYellow
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Chr(KeyAscii) = vbBack Then 'allow backspace key
Exit Sub
End If
If KeyAscii = 13 Then
SendKeys "{tab}" 'Text2.SetFocus
End If
If InStr("+-0123456789.", Chr(KeyAscii)) = 0 Then 'allow nos. only
KeyAscii = 0
End If
End Sub
Private Sub Text2_LostFocus()
Text2.BackColor = vbWhite
End Sub
Private Sub Text1_LostFocus()
Text1.BackColor = vbWhite
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
If Chr(KeyAscii) = vbBack Then 'allow backspace key
Exit Sub
End If
If KeyAscii = 13 Then
Command1.SetFocus
End If
If InStr("+-0123456789.", Chr(KeyAscii)) = 0 Then 'allow nos. only
KeyAscii = 0
End If
End Sub
Example2-keyup
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If Chr(KeyCode) = "M" And Shift = vbCtrlMask Then
Text1.SelText = "Miss"
ElseIf Chr(KeyCode) = "M" And Shift = vbCtrlMask + vbShiftMask Then
Text1.SelText = "Mr."
ElseIf Chr(KeyCode) = "M" And Shift = vbCtrlMask + vbShiftMask + vbAltMask
Then
Text1.SelText = "Mrs."
End If
End Sub
Private sub command1_click()
Text2=len(text1)
End sub
LABEL CONTROL
label control is useful to display any message to the user and user cannot alter
the message at program run time.
Following are the common properties of text box control:
name, height, width, left, top
caption: text you want to display to the user
alignment
event which is useful:
1.click
COMMAND BUTTON/PUSH BUTTON
Command button is useful to perform some action after user has provided some
information.
following are the common properties of text box control:
name, height, width, left, top
caption: text you want to display to the user
style: if graphical it can display an image. It requires setting of picture property.
picture: picture you can display if graphical style is set for command button
downpicture: picture you can display if graphical style is set for command button
and mouse button is held down over command button.
event which is useful:
1.click
Example-1
Private Sub form_Click()
Dim a As Integer, b As Integer, c As Integer
a = Text1.Text
b = Text2.Text
c = a + b
Text3.Text = c
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Label1_Click()
Text1.SetFocus
End Sub
Private Sub Label2_Click()
Text2.SetFocus
End Sub
Private Sub Label3_Click()
Text3.SetFocus
End Sub
CHECK BOX
Check box is useful to give several choices to the user and user can select one,
more or all choices..
following are the common properties of text box control:
name, height, width, left, top
caption: text you want to display to the user
style: if graphical it can display an image. It requires setting of picture property.
picture: picture you can display if graphical style is set for command button
downpicture: picture you can display if graphical style is set for command button
and mouse button is held down over command button.
value: when check box is selected value property is 1 otherwise 0.
event which is useful:
1.click
Example-1
Private Sub Command1_Click()
Dim ordertext$
If Check1.Value = 1 Then
ordertext = ordertext + Check1.Caption + ","
End If
If Check2.Value = 1 Then
ordertext = ordertext + Check2.Caption + ","
End If
If Check3.Value = 1 Then
ordertext = ordertext + Check3.Caption + ","
End If
If Check4.Value = 1 Then
ordertext = ordertext + Check4.Caption + ","
End If
ordertext = Mid(ordertext, 1, Len(ordertext) - 1) ‘remove comma from the last
MsgBox "your order is " + ordertext
End Sub
RADIO BUTTON
radio button is useful to give several choices to the user and user can select any
one choice.
following are the common properties of text box control:
name, height, width, left, top
caption: text you want to display to the user
style: if graphical it can display an image. It requires setting of picture property.
picture: picture you can display if graphical style is set for command button
downpicture: picture you can display if graphical style is set for command button
and mouse button is held down over command button.
value: when radio button is selected value property is true otherwise false.
event which is useful:
1.click
Private Sub Command1_Click()
Dim ordertext$
If Option1.Value = True Then
ordertext = Option1.Caption
ElseIf Option2.Value = True Then
ordertext = Option2.Caption
Else
ordertext = Option3.Caption
End If
MsgBox "your order is " + ordertext
End Sub
Frame Control: frame control is a container control. Container control can be
used for placing controls inside it. If you want to make set of radio buttons in a
form you must use frame control. When frame control is disabled all controls
placed inside it become disabled.
Normally frame controls are used for grouping controls which are logically
related.
Property: caption
Private Sub Command1_Click()
Dim ord$
If Option1.Value = True Then
ord = ord + Option1.Caption
ElseIf Option2.Value = True Then
ord = ord + Option2.Caption
Else
ord = ord + Option3.Caption
End If
ord = ord + " & "
If Option4.Value = True Then
ord = ord + Option4.Caption
ElseIf Option5.Value = True Then
ord = ord + Option5.Caption
Else
ord = ord + Option6.Caption
End If
MsgBox "Your Order is=" + ord
End Sub