Visual Basic notes & assignments
Visual Basic notes & assignments
PRACTICAL ASSIGNMENTS
BY: Edwin Eddy Mukenya E_MAIL: [email protected]
1. Create the interface below in VISUAL BASIC 6. Label the objects appropriately, and assign
the relevant characteristic adjusted from the property window.
Page 1 of 35
End Sub
Private Sub cmdunload_Click()
Unload MDIForm1 'removes the form from memory and display
End Sub
Private Sub Form_Load()
Frone.BackColor = vbGreen
End Sub
Private Sub Timer1_Timer()
Static t As Integer
frmtwo.Cls
frmtwo.Print Date$
frmtwo.Print Time$
frmtwo.Print "steven height changes by " & t & "inches"
pictone.Cls
pictone.CurrentY = 1000
pictone.Print Date$
pictone.Print Time$
pictone.Print "steven height changes by " & t & "inches"
t=t+1
End Sub
Page 2 of 35
SPLASH SCREEN
1. Create the splash screen below:
Page 3 of 35
DECLARATION OF VARIABLES IN VISUAL BASIC
DECLARATION OF ARRAYS
'syntax
'<keyword>{array name)[dimension] as <type>
'<keyword>{arrayname}() as <type>
Dim toll() As Double
'constant value
'syntax
'const <varname> = value
Const pie = 3.142
‘EXPLICIT DECLARATION
'syntax
'<keyword>{varname} as <type>
Dim sStr As String
Public nNum As Integer
Private vVal As Variant
' Static Ddate As Date
'initialization
sStr = ""
nNum = 0
vVal = ""
Ddate = #1/2/2004#
'IMPLICIT DECLARATION
Page 5 of 35
End Sub
For k% = 0 To UBound(Data)
Data(k) = 0
Next k
Data(0) = 12
Data(1) = 56 ‘Static or fixed values
Data(2) = 78
For j% = 0 To UBound(Data)
Data(j) = CLng(Rnd * 200) ‘Randomly assigned values
Next j
End Sub
kitoto:
MsgBox Err.Source & Space(3) & Err.Description & Space(3) & Err.Number
End Sub
Page 6 of 35
CONTROL STRUCTURES IN VISUAL BASIC
Introduction
'Data Types
'Variant ..... supports all the other variables
'integer......fixed or whole numbers
'long
'Byte
'double .....floating point numbers
'float
'string.......text or sequence of characters
'date.........calendar
'boolean......logic{true or false}
'Declaring Variable
'should not be a number
'should not be alphanumeric
'should not be keywords or symbols
'should not have spaces
'should not include math functions
'Syntax
'<keyword>[varname] as <datatype>.......explicitly
'[varname]<special character>=value.....implicitly
Implement the form interface below and type the following code in the form design view and
ensure that it meets the object procedure requirements
Page 7 of 35
Dim vVal As Variant, vNum As Variant (Declaring variant variables)
Form4.Cls
Dim sK As String
List1.Clear
While sK <> "N"
List1.AddItem sK
sK = InputBox("Enter Names", "WHILE")
Wend
End Sub
List1.Clear
Dim A(20) As Integer
List1.Clear
For Each vNum In vVal
List1.AddItem vNum '....extracting arguments from vval assigning then to
'......vnum one after the other then alocating the textbox
Next
End Sub
Private Sub Command4_Click()
'Syntax
'DO UNTIL <CONDITION=false> - loops only and only if the condition is not fulfilled(false)
' STATEMENT
'LOOP
Page 8 of 35
Dim n As Integer '.....declaring variable n as an integer
n=0 '.....initializing n to zero
List1.Clear
Do Until n = 100
List1.AddItem n
n = n + 1 '.....incrementing n by 1
Loop
End Sub
List1.Clear
Static l As Long
l = 100
Do While l > 0
List1.AddItem l
l = l - 1 '....decrementing l by 1
Loop
End Sub
'do
'statement
'loop while <condition=true>
List1.Clear
Static d As Double
d=0
Do
List1.AddItem d
d=d+1
Loop While d < 100
End Sub
Dim v As Variant
Page 9 of 35
v = InputBox("Enter values", "VALUES") ‘using input box to enter values
Select Case v
Case 0
Label1.Caption = "IT is azero/indian number"
Case 1 To 10000
Label1.Caption = "This is a numeric number"
Case "a" To "z"
Label1.Caption = "This is alower case alphabet"
Case "EDWINEDDYMUKENYA"
Label1.Caption = "I will never die so long as God allows who make me live, eddy"
Case Else
Label1.Caption = "go to hell"
End Select
End Sub
End Sub
Page 10 of 35
STRING FUNCTION AND USE
Math function Use
Abs(Number) Returns the absolute value of a number
Atn(Number As Double) Returns the arctangent of a number
Cos(Number As Double) Returns the cosine of an angle
Exp(Number As Double) Returns e (the base of natural logarithms) raised to a power
Log(Number As Double) Returns the natural logarithm of a number
Randomize([Number]) Initializes the random-number generator
Rnd([Number]) Returns a random number
Round(Number, [NumDigitsAfterDecimal As Long]) Round to a given no. of decimal places
Sgn(Number) Returns an integer indicating the sign of a number
Sin(Number As Double) Returns the sine of an angle
Sqr(Number As Double) Returns the square root of a number
Tan(Number As Double) Returns the tangent of an angle
Page 11 of 35
Convert the code below to fit your form design
End Sub
Page 12 of 35
End Sub
End Sub
End Sub
Page 14 of 35
CREATING AND USING PROCEDURES AND FUNCTIONS IN VB6
For i% = 0 To UBound(ali) - 1
Debug.Print "At Index " & Space(7) & ali(i)
Next i
End Sub
For i% = 0 To UBound(ali) - 1
ali(i) = CLng(Rnd * 200)
Debug.Print "At Index " & Space(7) & ali(i)
Next i
Debug.Print "________________________________________________"
ismail:
Me.Print Err.Description
End Sub
Page 15 of 35
viii.) Produres
Syntax
[public,private] sub {procedurename}([arguments])
statements
end sub
ix.) functions
[public,private][static] function functionname([arguments]) [as type]
statement
end function
Some procedures and functions created in modules and class modules
PROCEDURES
Public Const pie = 3.142
Public nft As Integer
Public vVal As Variant
Filling calculator interface button caption with values
Sub fillbuttons(obuttons As Object)
Dim vNum As Variant, i As Integer
vVal = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, ".", "clr", "*", "+", "-", "/", "^", "%", "=",
"back")
For Each vNum In vVal
obuttons(i).Caption = vNum 'assigning caption names to the command buttons
i=i+1
Next
End Sub
Enabling controls
Sub enable(frm As Form)
End Sub
Disabling controls
Sub disable(frm As Form)
End If
Next
End Sub
Page 16 of 35
Clearing control textbox
Sub clearing(frm As Form)
Dim mycontrol As Control
For Each mycontrol In frm.Controls
If TypeOf mycontrol Is TextBox Then
mycontrol.Text = ""
End If
Next
End Sub
OR
Sub cleartextbox(frm As Form)
Dim otext As TextBox
For Each otext In frm.Text1
otext.Text = ""
Next
End Sub
FUNCTIONS
j=0
p=0
End Function
The following three functions can be used to implement select sort algorithm
lk = A(ni)
nk = ni
For nj = ni To UBound(A) '- length of the Array.
If nj > ni Then
If A(nj) <= lk Then
lk = A(nj)
nk = nj
End If
End If
Next nj
smaller = nk
End Function
End Function
Page 20 of 35
Function selectsort(A() As Long)
Dim nj As Integer
For nj = LBound(A) To UBound(A)
swapping A, nj, smaller(A, nj)
Next nj
End Function
Create the interface above then Type the code below in the code editor of the above
interface to complete the calculator {Adjust the code appropriately to suit your
interface}
Page 21 of 35
Private Sub Command1_Click(Index As Integer)
' assigning values to the text box by click individual
'command boxes
For i = 0 To 10
If Command1(i).Value = True Then
If oprflag = True Then
Text1.Text = Command1(i).Caption
oprflag = False
Else
Text1.Text = Text1.Text & Command1(i).Caption
End If
End If
Next i
'Clearing the text box
If Command1(11).Value = True Then
Call clearing(frmcalculator)
oprsign = ""
num1 = 0
num2 = 0
End If
'math operations
For i = 12 To 17
If Command1(i).Value = True Then
oprsign = Command1(i).Caption
num1 = Val(Text1.Text)
oprflag = True
End If
Next i
'results
If Command1(18).Value = True Then
num2 = Val(Text1.Text)
Text1.Text = arith(oprsign, num1, num2)
End If
'back cancellation
If Command1(19).Value = True Then
Text1.Text = backing(Text1)
End If
End Sub
Private Sub Form_Load()
oprflag = False
fillbuttons Command1
disable frmcalculator
clearing frmcalculator
End Sub
Page 22 of 35
CREATE INTERFACE TO SHOW USE OF THE ABOVE FUNCTIONS WITHIN
THE FORM
type the following code in the code view of the above interface to complete the
implementation
{adjust the code to fit your application}
Dim arraypn() As Long
Dim arraypn1() As Long
Page 23 of 35
For t% = LBound(arraypn) To UBound(arraypn)
List3.AddItem arraypn(t)
Next t
End Sub
End Sub
Create the interface below then type the following code for the interface to be
operational.
Adjust the code appropriately to fit your interface design
Page 24 of 35
Dim Data(200) As Long, data1(20) As Long
Dim nj As Integer
Dim nm As Integer
nj = 0
List3.Clear
End Sub
Page 25 of 35
WRITING AND READING FROM RANDOM AND SEQUENTIAL FILES
Create the interface below that will be used for implementation of file access
Write the code below in the interface code view to complete the
implementation{Adjust the code appropriately to suit your self created interface}
nj = 0
List3.Clear
Page 26 of 35
End Sub
End Sub
Create the data base db4.mdb and db5.mdb in the same folder as the application and
created the tables applicable with the fields.
Give the labels names equivalent to there functionality, e.g. for command button addrecord the
name will be <cmdaddrecord> ,for label car id the names will be lblcarid and for first textbox
adjacent to label lblcarid will be txtcarid
After implementation of the above design you can type the code below adjusting the
code to fit into the procedures according to how you have captioned or named the design objects.
Select from project menu-reference menu item dialogue one of the Microsoft DAO
(2.5,2.5/3.51,3.51,3.60) object library
Page 27 of 35
DECLARATION
Page 28 of 35
rs.Update
End Sub
'Navigating data
End Sub
End Sub
Page 29 of 35
End If
End Sub
End Sub
Select from project menu-reference menu item dialogue one of the Microsoft Remote
Data Object (2.0) object library
Create the interface below then type the code that follows adjusting them appropriately to
fit your interface
CODES
Page 30 of 35
DBEngine.RegisterDatabase "mydsn", "Microsoft Access Driver (*.mdb)", True,
App.Path & "\db4.mdb"
Set rddata = rdoEngine.rdoEnvironments(0).OpenConnection("mydsn")
Set rdrecord = rddata.OpenResultset("select*from Car", rdOpenKeyset)
getvalues
End Sub
Select from project menu-reference menu item dialogue one of the Microsoft Activex
Data Object (2.0,2.1,2.3,2.5,2.6,2.7) object library
Create the interface below then type the code that follows adjusting them appropriately to
fit your interface
CODES
Dim cn As adodb.Connection
Dim rs As adodb.Recordset
Page 31 of 35
If Text1.Text <> "" Then
If MsgBox("Are you sure you want to delete therecord", vbCritical + vbYesNo,
"STUDENT") = vbYes Then
rs.Delete (adAffectCurrent)
MsgBox "The record has been deleted"
Else
MsgBox "The record was not deleted"
End If
rs.Requery
Else
MsgBox "No record to delete"
End If
clearing
disable
End Sub
Page 32 of 35
rs![Date of Birth] = Text3.Text
rs![Fee Payable] = Text3.Text
End Sub
Page 33 of 35
Private Sub Command6_Click()
If Not rs.EOF Then
rs.MoveLast
End If
get_values
End Sub
Select from project menu-component menu item dialogue one Microsoft ADO Data
Control 6.0 (sp6) OLEBD
Create the interface below and draw the adodc object on the form surface then right click
the control object then follow on the instruction that follows to connect the interface to the
database and then type the code that follows adjusting them appropriately to fit your interface
CODES
Private Sub cmdadd_Click()
'using the addnew method to create copy buffer space to allow editting of
'of data.
'Text box one inassigned values using the input box
'and focus is set to the next textbox after assigning the first textbox with items
Adodc1.Recordset.AddNew
txtName.Text = InputBox("Enter student name", "STUDENT")
txtRegistration.SetFocus
End Sub
Else
MsgBox "The record was not deleted"
End If
Adodc1.Recordset.Requery
End Sub
Page 35 of 35