Vous êtes sur la page 1sur 23

NOTES FOR VISUAL BASIC PART 3

Back to notes section :




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 lets 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 ODBCS and ODBC drivers
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 DBMSS API, or you had have to find
some other method to access the database though some middleware product,
such as Intersolvs 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
text2 name txtitemnumber
text3 text clear
text3 name txtsaleprice
text4 text clear
text4 name txtrateperitem
text5 text clear
text5 name txtrecord
text5 multiline true
text5 scrollbars both
command1 caption addrecord
command2 caption showrecord
command1 name cmdaddrecord
command2 name cmdshowrecord
write following code in code view window
Private Sub cmdaddrecord_Click()
Open "data" For Append As #1
Print #1, txtname
Print #1, txtitemnumber
Print #1, txtsaleprice
Print #1, txtrateperitem
Close #1
txtname = ""
txtitemnumber = ""
txtsaleprice = ""
txtrateperitem = ""
End Sub

Private Sub cmdshowrecord_Click()
Dim a$
Open "data" For Input As #1
txtrecord = "name,item number,sales price,rate per item" + vbCrLf

Do While Not EOF(1)
Input #1, a
txtrecord = txtrecord + a
Input #1, a
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.
Dynaset Tabletype Sanpshot

This type of recordset is
updateable
This type of record set is also
updateable
This type of recordset is
readonly and forward

We can use sql query for this
type of record
We can use table name and
we can not use sql query
We can use table name and
sql query

It may take less than or equal
to memory taken by table
type
It takes higher memory It may take less than or equal
to memory taken by table
type

It is slower It is slower in performance It is faster

We can use find command We can make use of index to We can use find command
and we can use index faster search the record and we can use index


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.

Write short notes on immediate window.
Immediate window is a window that normally appears in bottom.
We can use immediate window to know value of variable
Print x
We can use immediate window to assign new value to variable when program is
in break mode
X=10
We can use immediate window to check syntax of a function
Print len(rama)

Differentiate toolbar and menu bar.
Toolbar Menu bar

Toolbar contains icons which starts a
command
Menu bar contain menu pads which is made
of text, normally menu bar opens pull down
menu. Pull down menu may have item with
icons which may start other submenu or
command

Toolbar contains short cuts to start some
command
Menu bar doesnt contain short cut

Toolbar doesnt contain highlight character Menu bar contains highlight characters
Explain different types of data file in vb.
Binary:
It is to read or write data such as data of ole controls, image files, data field in a
table of data type blob(binary large object) etc. less commonly used to read text
data. It does not allow random access. Binary can make use of input function.
Type..end type statement is not useful.
Random:
Random files are used to read write text data in terms of fixed record length. less
commonly used to read/write binary data in terms of fixed length record. It
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.

Write short notes on Option button (Radio button)
Option button displays circle where user can click to put dot to select item. We
can have only one option button selected among set of option buttons. Option
button value property is true when dot is present in circle otherwise false. It is
compulsory to put option buttons in container control like frame or picture box
to create many sets. These controls are useful when we want user to select any
one out of more than one item.
Commonly use properties are as follows:
name: name properties is useful to identify one option button out of several
controls in code view.
caption: this is the text that appears along with control.
value: this property is true when option button is selected otherwise its value is
false.
style: this property changes the look and feel of the radio button

What is purpose of val function?
Val function extracts numeric value from string. If string starts with numeric
value it gives numeric value. If string starts with non-numeric value it gives 0.
Dim a#
a = Val("123.456")
Print a
print 123.456

a = Val("hello")
Print a
prints 0

a = Val("hello12")
Print a
prints 0

a = Val("hello 12")
Print a
prints 0

a = Val("12hello")
Print a
print 12

a = Val("12 34 .456hello")
Print a
prints 1234.456

Write short notes on Startup form
We can have many forms in our projects. When we run the project one of the
forms will be startup form. To change the startup form we can use project-
>properties->startup object and we can specify the startup form from the combo
box and we click on ok

Write short notes on visual data manager
Visual data manager is a tool that ships along with VB. As its name employs we
can use it to manage database.
Following are operations we can do in visual data manager: (Add ins-> visual
data manager)
creating new database
Opening existing database, redefining structure of table.
Creating tables, renaming tables, deleting tables, creating primary key, unique
key and defining fields.
Setting validation text, rule for fields.
compacting and repairing Microsoft access database
find and replace value in a field based on criteria
Data adding, editing and deleting in table.
Running sql query and saving the query.


Write short notes on data control.
VB provides a very useful control called data control that lets you access data
coming from a database. You can perform most data access operations using
data control without writing any code at all. The data control enables you to
move from record to record and to display and manipulate data from the records
coming from.
Following are the properties which are quite commonly used:
databasename: determines the path and filename of the database file.
exclusive: if this property is set to true, then when you open the database only
you will have exclusive access to the database and others cant access the
database.
options: determines access constraints for the tables you are working on.
readonly: when true, you can not update the database.
recordsource: determines the source of the set of records retrieved by the data
control.

What are the type of recordsets available in DAO
Differentiate different types of recordset in DAO
There are 5 types of recordsets available in DAO but 3 are available in design
time.
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 projects 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 labels 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.

What are err object and error function ?
err: err is an object if we use err.number then it gives error no. that occurred in
statement, if we use err.description it gives short description of the error
occurred, if we use err.raise we can raise an error intentionally.
error: it is a function if we give error no. as argument to it, it gives us error
description.

Write short notes on debugging tools in vb.
Visual basic offers a powerful suite of debugging options-notable the ability to
single step through your code as it is executing. The standard way to debug is
to place a breakpoint at a particular line in your code, and when execution
reaches that line, it will halt and visual basic will enter the debug state, give you
access to your code and variables. You can examine the contents of those
variables and work through your code line by line, watching program execution
behind the scenes.
To move through your program step-by-step you can select these stepping
options in the debug menu:
step into-single step though the code, entering called procedures if encountered.
step over- single-step through the code, stepping over procedure calls.
step out- step out of the current procedure.
Setting debugging breakpoints
Breakpoints are the foundation of vb debugging. When you set breakpoints in a
program and run that program, program execution continues until one of the
breakpoints is encountered and program execution stops, making vb enter the
debug state. You place a breakpoint in code by moving the text insertion caret to
that line and either selecting toggle breakpoint in the debug menu or pressing f9.
Examining variable and expression: just select the expression and press shift+f9
to evaluate variable and expression.
Adding debug watch windows: you can add a watch window to your debugging
session and display the values of selected variables or expressions in that
window continuously as you execute your program line by line.
Immediate window: when debugging, you can use the immediate window to
examine expressions or variables immediately, just by typing them in.


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


PROVIDED BY. MR NAGENDRA DEWANGAN

Vous aimerez peut-être aussi