Tag Archives: Visual Basic

Visual Basic Selection Using IF

Programs need to be able to branch and execute different instructions for different conditions. We can do this most simply using the IF statement.

We wanted to modify yesterday’s program so that we could turn it into a game. The user needs to guess the number that has been created by the dice roll. They have only one shot to get it right.

Some programmers noticed that the number the same the first few times they tried it, why do you think it was?

Continue reading Visual Basic Selection Using IF

Solution to 6.5.3 task 1

Ok so this is going to be a long post (I will need to check the blog options and shrink the post sizes). This is a solution to task 1 in 6.5.3 it is based on the design given in the answers. It is NOT the only solution and you might want to comment on ways the program could be improved (think about evaluation). I have included some screen shots.

NOTE the display of the data is optional.

Option Explicit

Private Sub Command1_Click()
'This program represents a solution to 6.5.3 task 1
'The Desgin was given in the answers section
'It is not the only solution
Dim HD_up As Integer 'HD Upgrade required
Dim RAM_up As Integer 'RAM Upgrade required
Dim Mon_up As Integer 'Monitor Upgrade required
Dim Cost1 As Single 'Price
Dim Num As Integer 'Quanity
Dim Basic_Cost As Single 'Cost for Order before deliver and discounts
Dim Dis As Single ' Discount on whole order
Dim Del As Single ' delivery charge per order
Dim VAT As Single 'VAT on Order
Dim Total As Single ' Order Total
Cost1 = 499 'Set the starting cost of a machine
Del = 50 ' £50 delivery per order
Form1.Print "The basic systems costs 499" 'optional
Call Get_Comp_Spec(HD_up, RAM_up, Mon_up)
Call Calc_Cost_Of_One(Cost1, HD_up, RAM_up, Mon_up)
Call Get_Number(Num)
Call Calc_Basic_Cost(Num, Cost1, Basic_Cost)
Call Calc_discount_delivery_VAT(Num, Basic_Cost, Dis, Del, VAT)
Call Calc_total_cost(Basic_Cost, Dis, Del, VAT, Total)
Call Display_total(Total)
End Sub

Private Sub Get_Comp_Spec(ByRef HD_up As Integer, ByRef RAM_up As Integer, ByRef Mon_up As Integer)
'Check what upgrades the user wishes
HD_up = MsgBox("Do you wish to upgrade to 60GB for £30?", vbYesNo)
RAM_up = MsgBox("Do you wish to upgrade to 1GB for £50?", vbYesNo)
Mon_up = MsgBox("Do you wish to upgrade to 21 inch for £199?", vbYesNo)
End Sub

Private Sub Calc_Cost_Of_One(ByRef Cost1 As Single, ByVal HD_up, ByVal RAM_up, ByVal Mon_up)
'calculate the cost of the upgrades
If HD_up = vbYes Then
Cost1 = Cost1 + 30
Form1.Print "Hard drive upgrade £30" 'optional
End If
If RAM_up = vbYes Then
Cost1 = Cost1 + 50
Form1.Print "RAM upgrade £50" 'optional
End If
If Mon_up = vbYes Then
Cost1 = Cost1 + 199
Form1.Print "Monitor upgrade £199" 'optional
End If
End Sub

Private Sub Get_Number(ByRef Num As Integer)
'Get the number of systems ordered
'As an extension task this number could be validated
Num = InputBox("How many machines do you want?")
Form1.Print Num & " machines ordered" 'optional
End Sub

Private Sub Calc_discount_delivery_VAT(ByVal Num, ByVal Basic_Cost, ByRef Dis As Single, ByRef Del As Single, ByRef VAT As Single)
'Calculate Discount
If Num > 10 And Num <= 20 Then
Dis = Basic_Cost * 0.05 '5% Form1.Print "A discount of 5% is £" & Dis 'optional
ElseIf Num > 20 Then
Dis = Basic_Cost * 0.1 '10%
Form1.Print "A discount of 10% is £" & Dis 'optional
Else
Dis = Basic_Cost * 0 '0%
Form1.Print "A discount of 0% is £" & Dis 'optional
End If
'Calculate VAT
Form1.Print "Delivery on the order is £" & Del 'optional
VAT = (Basic_Cost - Dis + Del) * 0.175
Form1.Print "The VAT on the order is £" & VAT 'optional
End Sub

Private Sub Calc_Basic_Cost(ByVal Num, ByVal Cost1, ByRef Basic_Cost As Single)
'Calculate the cost of the order before discount and VAT
Basic_Cost = Num * Cost1 ' Cost for whole order
Form1.Print "The cost of the order before discount,deliver & VAT is £" & Basic_Cost 'optional
End Sub

Private Sub Calc_total_cost(ByVal Basic_Cost, ByVal Dis, ByVal Del, ByVal VAT, ByRef Total As Single)
'Calculate the Total cost of the whole order
Total = Basic_Cost + Del + VAT - Dis
End Sub

Private Sub Display_total(ByVal Total)
'Display the cost for the whole order
Form1.Print "The total for the order was £" & Format(Total, "0.00")
End Sub

Program Code for Times Table

Change the program code below so that the program performs the calculation in a separate procedure from the display.

Option Explicit
Private Sub CmdStart_Click()
'Set up variables
Dim multiple As Integer
'Get Multiplier from user
Call GetValue(multiple)
'Display table
Call DisplayTable(multiple)
End Sub


Private Sub GetValue(ByRef intvalue As Integer)
intvalue = InputBox("Please enter the value")
End Sub


Private Sub DisplayTable(ByVal multiple)
Dim Counter As Integer
Dim Answer As Integer
For Counter = 1 To 12
Answer = multiple * Counter
lstOutput.AddItem multiple & " X " & Counter & " = " & Answer
Next
End Sub

Code for Corrie

Option Explicit

Private Sub Command1_Click()
‘Set up variables
Dim NumA As Single
Dim NumB As Single

Call Get2Numbers(NumA, NumB)
Call DisplaySum(NumA, NumB)

End Sub

Sub Get2Numbers(ByRef NumA, ByRef NumB)
NumA = InputBox(“What is the first number”)
NumB = InputBox(“What is the second number”)
End Sub

Sub DisplaySum(ByVal NumA, ByVal NumB)
MsgBox (NumA + NumB)
End Sub

Introduction to Arrays

Today’s lesson introduced 1-D arrays. The class started off by creating 3 programs.
1. Create a program to take in 3 numbers and display the sum and average.
2. modify the program to work with 5 numbers.
3. Modify the program to display the numbers after it displays the sum and average.

The class were given the main steps and asked to refine it, before starting the task. Each task was to be saved in separate folders.

The class was then asked to modify the program for 100 numbers. This led to discussion about the need for a new data type that could store a list of values, this data type is called Array.

Scratch was used to show how arrays can be used. The class then looked at the DIM command’s help page to discover how to set up an Array.

DIM IntNumber(50) as Integer

Where (50) is the number of items in the array.

The program was modified to allow entry of 100 values before the period finished.

HOMEWORK

Write the refinements for the main program given for 100 numbers.

  1. Set up Variables
  2. Get 100 values from user
  3. Calculate Average and Sum of values
  4. Display the Average, Sum and values
  5. End program