COS 102 Introduction To PS Part 2
COS 102 Introduction To PS Part 2
6.3 Concept of VB
1 https://www.indeed.com/career-advice/career-development/how-to-learn-visual-basic
In 2002, Microsoft released Visual Basic.NET (VB.NET) to replace Visual Basic 6. Thereafter,
Microsoft declared VB6 a legacy programming language in 2008. Fortunately, Microsoft still
provides some form of support for VB6. VB.NET is a fully object-oriented programming
language implemented in the .NET Framework. It was created to cater for the development of
the web as well as mobile applications.
Options: choose to either start a new project, open an existing project, or select a list of recently
opened programs. A project is a collection of files that make up an application. There are
various types of applications that one could create, however, concentration is on creating
Standard EXE programs (EXE means executable). Next, click on the Standard EXE icon to go
into the actual Visual Basic 6 programming environment.
A new VB 6 Standard EXE project IDE is shown in figure 6.2. It consists of the toolbox, the
form, the project explorer, and the properties window.
The Form is the primary building block of a VB 6 application. Before proceeding to building
an application, it is a good practice to save the project first. Save the project by
selecting Save Project from the File menu, assign a name to your project and save it in a certain
folder.
Note about the beginning and the end statements (i.e. Private Sub Form_Load.......End Sub.);
Just key in the lines in between the above two statements exactly as are shown here. When
F5 is pressed to run the program, nothing showed up. In order to display the output of the
program, one needs to add the Form1.show statement like in Example or just
use Form_Activate ( ) event procedure as shown in example 2. The command Print does not
mean printing using a printer, but it means displaying the output on the computer screen. Now,
press F5 or click on the run button to run the program with the output as shown in Figure 6.6
Also perform arithmetic calculations as shown in Example 2. VB uses * to denote the
multiplication operator and / to denote the division operator. The output is shown in Figure 6.7,
where the results are arranged vertically.
Example 1
Private Sub Form_Load ( )
Form1.show
Print "Welcome to Visual Basic tutorial"
End Sub
Example 2
Private Sub Form_Activate ( )
Print 20 + 10
Print 20 - 10
Print 20 * 10
Print 20 / 10
End Sub
Figure 6.6: The output of Example 1
Figure 6.8
Figure 6.9
6.6. Working With Controls
6.6.1 The Control Properties
To effectively write an event procedure for a control to respond to an event, it is necessary to
configure specific properties that dictate its appearance and functionality. These properties can
be set either through the properties window or dynamically during runtime.
Figure 6.10 is a typical properties window for a form. In the properties window, the item
appears at the top part is the object currently selected. At the bottom part, the items listed in the
left column represent the names of various properties associated with the selected object while
the items listed in the right column represent the states of the properties. Properties can be set
by highlighting the items in the right column then change them by typing or selecting the
options available.
Figure 6.17
The structure of an event procedure is as follows:
Private Sub Command1_Click
VB Statements
End Sub
Enter the codes in the space between Private Sub Command1_Click............. End Sub. The
keyword Sub stands for a sub procedure that made up a part of all the procedures in a program
or a module. The program code is made up of several VB statements that set certain properties
or trigger some actions. The syntax of the Visual Basic’s program code is almost like the
normal English language, though not the same, so it is easy to learn.
The syntax to set the property of an object or to pass a certain value to it is:
Object.Property
where Object and Property are separated by a period (or dot). For example, the
statement Form1.Show means to show the form with the name
Form1, Iabel1.Visible=true means label1 is set to be visible, Text1.text=”VB” is to assign
the text VB to the text box with the name Text1, Text2.text=100 is to pass a value of 100 to
the text box with the name text2, Timer1.Enabled=False is to disable the timer with the name
Timer1 and so on. Let’s examine a few examples below:
Example 1
Private Sub Command1_click()
Label1.Visible=false
Label2.Visible=True
Text1.Text="You are correct!"
End sub
Example 2
Private Sub Command1_click()
Label1.Caption="Welcome"
Image1.visible=true
End sub
Suffixes for Literals: Literals are values that are assigned to data. In some cases, there is need
to add a suffix behind a literal so that VB can handle the calculation more accurately. For
example, use num=1.3089# for a Double type data. Some of the suffixes are displayed in Table
6.3.
& Long
! Single
# Double
@ Currency
In addition, we need to enclose string literals within two quotations and date and time literals
within two # sign. Strings can contain any characters, including numbers. The following are
few examples:
memberName="Turban,John."
TelNumber="1800-900-888-777"
LastDay=#31-Dec-00#
ExpTime=#12:00 am#
6.9 Managing Variables
Variables are like mailboxes in the post office. The contents of the variables change every now
and then, just like the mailboxes. In term of VB, variables are areas allocated by the computer
memory to hold data. Like the mailboxes, each variable must be given a name. To name a
variable in Visual Basic, there are a set of rules to follow.
6.9.1 Variable Names
The following are the rules when naming the variables in Visual Basic
i. It must be less than 255 characters.
ii. No spacing is allowed.
iii. It must not begin with a number.
iv. Period is not permitted.
v. Cannot use exclamation mark (!), or the characters @, &, $, #.
vi. Cannot repeat names within the same level of scope.
Examples of valid and invalid variable names are displayed in Table 6.4
My_Car My.Car
^ Exponential 2^4=16
* Multiplication 4*3=12,
/ Division 12/4=3
"Visual"&"Basic"="Visual
+ or & String concatenation
Basic"
Example 1
Private Sub Command1_Click()
Dim firstName As String
Dim secondName As String
Dim yourName As String
firstName = Text1.Text
secondName = Text2.Text
yourName = secondName +"" + firstName
Label1.Caption = yourName
End Sub
In this example, three variables are declared as string. For variables firstName and secondName
will receive their data from the user’s input into textbox1 and textbox2, and the variable
yourName will be assigned the data by combining the first two variables. Finally, yourName
is displayed on Label1.
Example 2
Dim number1, number2, number3 as Integer
Dim total, average as variant
Private sub Form_Click()
number1=val(Text1.Text)
number2=val(Text2.Text)
number3= val(Text3.Text)
Total=number1+number2+number3
Average=Total/5
Label1.Caption=Total
Label2.Caption=Average
End Sub
In the Example 2, three variables are declared as integer and two variables are declared as
variant. Variant means the variable can hold any data type. The program computes the total and
average of the three numbers that are entered into three text boxes.
6.11 If statement
This concerns decision-making in VB code, where one processes user input and direct the
program flow accordingly. The ability to make decisions is a crucial aspect of programming,
enabling intelligent problem-solving and generating meaningful output or feedback for the
user. For instance, one can construct a program that prompts the computer to execute specific
tasks until a predefined condition is satisfied.
6.11.1 Conditional Operators
To control the VB program flow, one can use various conditional operators. Basically, they
resemble mathematical operators. Conditional operators are very powerful tools, they let the
VB program compare data values and then decide what action to take, whether to execute a
program or terminate the program and more. These operators are shown in Table 6.6.
Table 6.6: Conditional Operators
Operator Meaning
= Equal to
Operator Description
<5000 0
5000-9999 5
1000-14999 10
15000-19999 15
In this example, we insert a textbox to accept sale volume input and a label to display
commission. Insert a command button to trigger the calculation.
The Code
2 https://www.codingninjas.com/studio/library/decision-tree-and-decision-table
In the table, there are multiple rules for a single decision. The rules from a decision table can
be made by just putting AND between conditions.
The major rules which can be extracted (taken out) from the table are:
• R1 = If (working day = Y) ^ (holiday = N) ^ (Rainy-day = Y) Then, Go to office.
• R2 = If (working day = N) ^ (holiday = N) ^ (Rainy-day = N) Then, Go to office.
• R3 = If (working day = N) ^ (holiday = Y) ^ (Rainy-day = Y) Then, Watch TV.
• R4 = If (working day = N) ^ (holiday = Y) ^ (Rainy-day = N) Then, Go to picnic.
Table 6.8
1 2 3 4
Working day Y N N N
Holiday N N Y Y
Rainy day Y N Y N
Go to office Y Y
Go to picnic Y
Watch TV Y
Evaluation
The final step of the problem-solving cycle is evaluation and learning. Evaluation refers to the
careful observation and assessment of the process and the effects. An evaluation should tell
whether a project is successfully completed, whether improvements need to be implemented
and what can be learnt for the future.
3
https://www.codingninjas.com/studio/library/decision-tree-and-decision-table
Evaluations can be performed with four objectives in mind. First, evaluations serve the current
problem-solving project by determining the results achieved and the improvements to be made.
This is evaluation in a strict sense. In a broader sense, for which the term ‘learning’ is used,
evaluation serves three further objectives. As a second objective, evaluation may also be
oriented towards learning for future problems in the same context. This use of evaluation and
learning is particularly stressed in the literature on organizational learning and knowledge
management. Third, evaluation and learning can be oriented towards advancing generic
scientific knowledge about this type of business process. Finally, evaluation and learning are
necessary for personal and professional learning and development.
Reference
1. https://www.vbtutor.net/lesson8.html
2. https://www.indeed.com/career-advice/career-development/how-to-learn-visual-basic
3. https://www.codingninjas.com/studio/library/decision-tree-and-decision-table
4. https://learnlearn.uk/alevelcs/stepwise-
refinement/#:~:text=Stepwise%20Refinement%20is%20the%20process,series%20of%20smal
ler%20sub%2Dsteps.
5. https://en.wikipedia.org/wiki/Decision_tree
6. https://www.codingninjas.com/studio/library/decision-tree-and-decision-table