Академический Документы
Профессиональный Документы
Культура Документы
NOTES FOR VISUAL BASIC PART 4
USING CONTROL ARRAY
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.
Note: all option button has name option1 and index ranging from 0 to 5. first
three are placed inside frame1 and last three are placed inside frame2.
Private Sub Command1_Click()
Dim ord$
Dim i%
For i = 0 To 2
If Option1(i).Value = True Then
ord = ord + Option1(i).Caption
End If
Next
For i = 3 To 5
If Option1(i).Value = True Then
ord = ord + Option1(i).Caption
End If
Next
MsgBox "Your Order is=" + ord
End Sub
Example-2
Note: all checkboxes have name check1 and index ranging from 0 to 5.
Private Sub Command1_Click()
Dim ord$
Dim i%
ord = "beverage="
For i = 0 To 2
If Check1(i).Value = 1 Then
ord = ord + Check1(i).Caption + ","
End If
Next
For i = 3 To 5
If Check1(i).Value = 1 Then
ord = ord + Check1(i).Caption + ","
End If
Next
LISTBOX
list box is useful to give several choices to the user and user can select one or
more choices.
following are the common properties of text box control:
name, height, width, left, top
list: it is an array. It contains name of the item in the list box.
listindex: it gives index of the item selected, if no item is selected then it’s value
is -1.
listcount: gives no. of items in list.
newindex: gives the index given to the item newly added to list box(useful if
sorted is true).
sorted: if true items are sorted alphabetically.
style:can be standard or checkbox.
multiselect: if true more than one items can be selected..
itemdata: it is an array. No. of elements is equal to no. of items in listbox. You
can store any integer value for each item.
selected: it is an array. It contains value true if an item is selected otherwise
contains false( useful if multiselect is true)
Methods:
additem : to add new item in list box
removeitem: to remove item from list whose index is given as argument.
clear: delete all items.
Event:
Click, dblclick.
Example-1
End Sub
Example-2
Case 1
If List2.ListIndex <> -1 Then
'add selected item from list2 to list1
List1.AddItem List2.List(List2.ListIndex)
'remove selected item from list2
List2.RemoveItem List2.ListIndex
End If
Case 2
For i = 0 To List2.ListCount - 1
List1.AddItem List2.List(i)
Next
List2.Clear
Case 3
For i = 0 To List1.ListCount - 1
List2.AddItem List1.List(i)
Next
List1.Clear
End Select
End Sub
End Sub
Example-3
Case 1
For i = List2.ListCount - 1 To 0 Step -1
'add selected item from list1 to list2
If List2.Selected(i) = True Then
List1.AddItem List2.List(i)
List2.RemoveItem i
End If
Next
Case 2
For i = 0 To List2.ListCount - 1
List1.AddItem List2.List(i)
Next
List2.Clear
Case 3
For i = 0 To List1.ListCount - 1
List2.AddItem List1.List(i)
Next
List1.Clear
End Select
End Sub
End Sub
COMBO BOX
combo box is useful to give several choices to the user and user can select only
one choice. Combo box is combination of textbox and drop down list.
following are the common properties of text box control:
name, height, width, left, top
list: it is an array. It contains name of the item in the list box.
listindex: it gives index of the item selected, if no item is selected then it’s value
is -1.
listcount: gives no. of items in list.
newindex: gives the index given to the item newly added to list box(useful if
sorted is true).
sorted: if true items are sorted alphabetically.
style: drop down combo, simple list and drop down list.
Example-1
End Sub
Creating Menu
End If
End Sub
Example-2
Scrollbar: this control is useful to let the user select value within a range.
Properties are as follows:
max: maximum value of the range
min: minimum value of the range
smallchange: when user clicks on either arrow value property will change by this
quantity.
largechange: when user clicks on either side of the thumb, value property
change by this quantity.
value: gives thumb position
Events:
change: when clicked on arrow or on either side of thumb.
scroll: when thumb is moved by mouse.
There are two types of scroll bars vertical and horizontal.
HScroll2.Min = 0
HScroll2.Max = 255
HScroll2.SmallChange = 5
HScroll2.LargeChange = 10
HScroll3.Min = 0
HScroll3.Max = 255
HScroll3.SmallChange = 5
HScroll3.LargeChange = 10
End Sub
End Sub
Private Sub HScroll1_scroll() 'moving thumb using mouse changes color
immediately
Text1.BackColor = RGB(HScroll1, HScroll2, HScroll3)
Label1.Caption = "r=" + CStr(HScroll1) + "," + "g=" + CStr(HScroll2) + "," + "b=" +
CStr(HScroll3)
End Sub
Private Sub HScroll2_scroll()
Text1.BackColor = RGB(HScroll1, HScroll2, HScroll3)
Label1.Caption = "r=" + CStr(HScroll1) + "," + "g=" + CStr(HScroll2) + "," + "b=" +
CStr(HScroll3)
End Sub
Private Sub HScroll3_scroll()
Text1.BackColor = RGB(HScroll1, HScroll2, HScroll3)
Label1.Caption = "r=" + CStr(HScroll1) + "," + "g=" + CStr(HScroll2) + "," + "b=" +
CStr(HScroll3)
End Sub
Timer control
Timer control is useful to execute some code at regular interval of time.
Properties:
interval: set duration in millisecond
enabled: if true timer control can fire event
event:
timer: this event is fired at regular interval as specified in interval property.
events:
change: for drive and directory
click: for file list box.
Shape control.
Shape control is useful to draw various figures on form. It doesn’t fire any event.
Properties:
1.backcolor
2. Borderstyle: you can have different line types.
3. borderwidth: if you set border width other than 1 then you can not have
different border style.
4. fillcolor: color for the pattern used for fill
5. fillstyle: you can choose different fill pattern.
Example-1
Combo2.AddItem "transparent"
Combo2.AddItem "solid"
Combo2.AddItem "dash"
Combo2.AddItem "dot"
Combo2.AddItem "dash-dot"
Combo2.AddItem "dash-dot-dot"
Combo2.AddItem "inside solid"
Combo3.AddItem "solid"
Combo3.AddItem "transparent"
Combo3.AddItem "horizontal line"
Combo3.AddItem "vertical line"
Combo3.AddItem "upward diagonal"
Combo3.AddItem "downward diagonal"
Combo3.AddItem "cross"
Combo3.AddItem "diagonal cross"
End Sub
Line Control
Useful to draw line on form
Image Control
This control is useful to display image. It has following properties
picture: set this property to any picture file.
stretch: if true picture is resized to fill in image control
Event
Click
place data control, labels and text boxes on form and set following properties for
data control
databasename:name of the database you want to work on e.g.
C:\Documents and Settings\COMPAQ\My Documents\emp.mdb
recordsource: select the table you want to work on e.g.
emplist
for each text box in form set datasource to data1 then set datafield to the field
whose data the textbox should display.
then set caption for labels.
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. RDO control is collection of connection and resultset
objects and it is based on Remote Data Access object library(RDO).
Following are the properties which are quite commonly used:
datasourcename: dsn contains the path and filename of the database file and
database driver to use for database access.
sql: determines the source of the set of records retrieved by the RDO data
control.
place RDO data control, labels and text boxes on form and set following
properties for data control
datasourcename:select the dsn created earlier
e.g. empdsn
sql: select the table using sql statement you want to work on
e.g. select * from emplist
for each text box in form set datasource to msrdc1 then set datafield to the field
whose data the textbox should display.
then set caption for labels.
place ADO data control, labels and text boxes on form and set following
properties for data control
connectionstring :select ODBC data source name radio button and select dsn
created earlier
e.g. empdsn
recordsource: select command type adcmdtable from first combo box and select
the name of table from second combo box then click on ok
e.g emplist
or select command type adcmdtext from first combo box and specify sql
statement in text box
for each text box in form set datasource to adodc1 then set datafield to the field
whose data the textbox should display.
then set caption for labels.
Data Form Wizard
Data form wizard is a tool using which we can make data entry form quickly.
Following are the steps required to build data form wizard:
Project->add form->vb data form wizard->Open->
To bring ADO control and data grid control in toolbox follow these steps:
Use project->components->tick mark Microsoft ADO Data Control 6.0(OleDB)
option then click on OK.
place ADO data control, and datagrid on form and set following properties for
data control
connectionstring :select ODBC data source name radio button and select dsn
created earlier
e.g. student
recordsource: select command type adcmdtable from first combo box and select
the name of table from second combo box then click on ok
e.g student
or select command type adcmdtext from first combo box and specify sql
statement in text box
Test Table
101 80 45 77
102 70 55 56
103 67 77 85
104 55 65 89
Useful properties:
datasource: set adodc1 control to this property
datafield: set adodc1 control to this property
rowsource: set adodc2 control to this property
listfield:set name of field you want to display from adodc2 control
boundcolumn: it should be same as name of datafield property value
To create DSN follow these steps:
Start->control Panel->administrative tools->data sources->click on system dns
tab ->click on add command button ->select Microsoft access driver(*.mdb and
click on finish command button )-> specify data sourcename e.g. student2 then
Click on select command button and select the database you want to work on
e.g.
C:\Documents and Settings\COMPAQ\My Documents\student2.mdb->ok->Ok
place ADO data control, labels, textboxes and datacombo on form and set
following properties for data control
connectionstring :select ODBC data source name radio button and select dsn
created earlier
e.g. student2
recordsource: select command type adcmdtable from first combo box and select
the name of table from second combo box then click on ok
e.g test
or select command type adcmdtext from first combo box and specify sql
statement in text box such as
select * from test
set datasource property of datacombo and other text boxes to adodc1 data
control and set datafield properties.
connectionstring :select ODBC data source name radio button and select dsn
created earlier
e.g. student2
recordsource: select command type adcmdtable from first combo box and select
the name of table from second combo box then click on ok
e.g student
or select command type adcmdtext from first combo box and specify sql
statement in text box such as
select * from student
set rowsource property of datacombo and to adodc2 data control and set
listfield propertiy to studentname and set boundcolumn to rollno
right click on connection1 and select properties. Under provider tab from the list
box select Microsoft oledb provider for odbc driver and click on next
specify data source name(dsn)
when clicked on test connection, connection succeeded message must appear if
it appears click on Ok
again right click on connection1 and select add command.
right click on connection1 and select properties. Under provider tab from the list
box select Microsoft oledb provider for odbc driver and click on next
specify data source name(dsn)
click on grouping tab: tick mark group command object and select field on which
grouping is required from first list box and move it to second list box. Click on OK
In this example we will drag a label control on to text box and text box will
acquire caption of label control in its text property.
Example-1
Example-1
.ToPage = 1
.Copies = 1
.ShowPrinter
End With
counter = counter + 1
f.Caption = "New Dcoument :" + CStr(counter)
f.Show
End Sub
Types of error
Error are of different types
logical error: logical error is an error that is difficult to find out we use debugging
tools to find out logical error. In logical error for a set of supposed input desired
output is not achieved.
Compile time error: this type of error is normally generated due to incorrect
syntax used in writing statement. Auto syntax check feature of vb checks syntax
myroutine:
If n = 0 Then
n=1
Resume ' execute the same statement caused error
End If
End Sub
m = Text1.Text
n = Text2.Text
p=m/n
Text3.Text = p
Exit Sub
myroutine:
If n = 0 Then
n=1
Resume next 'resume the statement appearing next to the statement caused
errorl
End If
End Sub
Using on error resume next and on error goto 0 (inline error handling)
n = Text2.Text
On Error Resume Next
p=m/n
If Err Then ‘err object will be true if previous statement caused error
Text3.Text = m ‘ you can use err.number to get error no. and to get error
description use
‘err.description
Else
Text3.Text = p
End If
End Sub
Differentiate binary and random file
Binary Random
It is to read or write data such as Random files are used to read write
data of ole controls, image files, text data in terms of fixed record
data field in a table of data type length. less commonly used to read/
blob(binary large object) etc. less write binary data in terms of fixed
commonly used to read text data. length record.
Binary can make use of input Random can not make use of input
function function.
Sequential Random
Sequential does not use record of Random uses record of fixed length.
fixed length.
Sequential is useful to read text data Random can be used to read text
in sequence. and binary data in terms of fixed
record length.
Sequential file can use following It can use get and put statement
statements which random file can which can not be used by sequential
not use: input, input#,line file.
input ,print #,write#
' if you open file in output mode if data exists in file, file's data is lost
' if you open file in append mode if data exists in file you can write new data at
the end of
' file. if file doesn't exist then new file is created
Open "data.txt" For Append As #1
Write #1, p
Write #1, q
Write #1, r
Close #1
End Sub
s.sname = Text1.Text
s.srollno = Text2.Text
s.sclass = Text3.Text
recordno = InputBox("enter record no. where you want to write record")
'size of record will be fetched using len(s)
Open "data.rnd" For Random As #1 Len = Len(s)
Put #1, recordno, s
Close #1
End Sub
Data bound control receives data from data control and have data source, data
member, data field properties. Data control has databasename, record source
and other properties. Data control connects with database and table and
supplies data to databound control.
Using validate event
Normally every control has validate property which is by default
true therefore control can generate validate event. Validation
event has an argument if this argument is assigned value true
then control can loose its focus if the argument is assigned value
false then focus remains in the control.