“Option Explicit” Statement

“Option Explicit” Statement
• Variables must be declared before they
can be used
– Undeclared variables are Variants (slowest!)
– Avoid misspelling variables
Private Sub Command1_Click()
Dim sMississippi As String
sMississipi = "Hello" '<-- Note the missing "p"
MsgBox sMississippi
End Sub
http://www.rentron.com/intro.htm
“Option Explicit” Statement
• Place “Option Explicit” at the top of any code module
(“general declarations” in the case of forms)
• From VB’s menubar (Tools  Options)
Lab Sheet 3: Modular
Programming
Lab Sheet 3: Objectives
• In this lab, you will…
– Organise program code and data declarations
into modules
– Create code routines and variables with
different levels of scope
– Create code routines that accept parameters
– Create code routines that return a result
(Functions)
– Create and work with arrays
Exercise: Marks Calculator
•
•
•
Accept from a user
a list of students
names and marks
Display existing list
of students names
and marks
Calculate and
display the average
class mark
Assignments
• Assignment 1: Creating Modules
• Assignment 2: Adding Data Declarations
to a Module
• Assignment 3: Adding Subs to work with
the Data
• Assignment 4: Adding Functions to return
results from the data
Add a New Module
Arrays
• Simplest form of data structure
– A number of variables of the same type,
related in some way
• A List of names
• A Table of profit figures
– All elements share the same name
– Each element has an index indicating its
position in the array
Arrays
Dim Profits(1998 To 2000, 1 To 4) As Single
Dim Students(1 To 6) As String
Index
Students
1
2
3
4
5
6
John Smith
Jane Green
Mary Brown
Mike Stone
Ashok Din
Profits
Quarters
1998
1999
2000
1
1240.00
1450.25
1603.33
2
1775.50
1825.23
1733.24
3
1602.45
1743.10
1679.95
4
1100.70
1250.50
1432.55
1-Dimensional Array
Index
2-Dimensional Array
Element
Programming with Arrays
• Use For..Next construct to loop through an array
Dim ID As Integer, Year As Integer, Quarter As Integer
……
For ID = 1 To 6
Print Students(ID)
Next
For Year = 1998 To 2000
‘Note nested For loops
For Quarter = 1 To 4
TotalProfit = TotalProfit + Profits(Year, Quarter)
Next
Next
Programming with Arrays
• Use of a Control Array
• An array of Control Objects:
– Create the first Control Object and name it
– Create the second Control Object and give it
the same name. VB asks if this is an array.
– Reference by Name(0), Name(1), Name(2)
etc.
Use of Control Arrays
For i = 0 To 3
TextBox(i).Text = "Text Box " & i
Next
Menu Editor
The Menu Editor
• Tools  Menu Editor
http://www.samspublishing.com/library/content.asp?b=STY_VB6_24hours&seqNum=176&rl=1
Adding Menus
Sub-Menus
Connecting Menus to Events
The Print Method
[Printer.]Print [Spc(n) | Tab(n)] Expression
Printer.Print "The sales were"
Printer.Print 4345.67
Printer.Print lblComName.Caption
Printer.FontBold = True
Printer.FontItalic = True
Printer.FontSize = 60
Printer.Print "I'm learning Visual Basic!"
Printer.Print strFirstName; Spc(10), strLastName
Printer.Print Tab(5), dteDateGenerated
http://www.samspublishing.com/library/content.asp?b=STY_VB6_24hours&seqNum=169&rl=1
The Print Method
1: Taxl = TaxRate * HouseVal1
2: Tax2 = TaxRate * HouseVal2
3:
4: TotalVal = HouseVal1 + HouseVal2
5: TotTaxes = TaxRate * TotalVal
6:
7: Printer.Print "House Value"; Tab(20); "Tax"
8: Printer.Print Format(HouseVal1, "Currency");
9: Printer.Print Tab(20); Format(Taxl, "Currency")
10: Printer.Print Format(HouseVal2, "Currency");
11: Printer.Print Tab(20); Format(Tax2, "Currency")
12:
13: Printer.Print ' Prints a blank line
14: Printer.Print "Total tax:"; Spc(5); Format(TotTaxes, "Currency")
15: Printer.NewPage
16: Printer.EndDoc
http://www.samspublishing.com/library/content.asp?b=STY_VB6_24hours&seqNum=169&rl=1
PrintForm Method
• Sends an image of the form to a printer
– Not recommended: vary greatly depending on
the screen resolution and the resolution of the
printer
– No longer supported in VB 2005
' First turn of all the controls that do not have a Tag property of Print
Call PrintVisibility(False)
' Now print the objects that are still visible
PrintForm