Visual Basic Code Examples for O-Level
Students
1. Basic Arithmetic Operations
Private Sub cmdCalculate_Click()
Dim num1 As Double
Dim num2 As Double
num1 = Val(txtNum1.Text)
num2 = Val(txtNum2.Text)
lblAdd.Caption = "Sum: " & (num1 + num2)
lblSub.Caption = "Difference: " & (num1 - num2)
lblMul.Caption = "Product: " & (num1 * num2)
lblDiv.Caption = "Quotient: " & (num1 / num2)
End Sub
2. Find the Largest of Two Numbers
Private Sub cmdCompare_Click()
Dim a As Integer
Dim b As Integer
a = Val(txtA.Text)
b = Val(txtB.Text)
If a > b Then
lblResult.Caption = "A is greater"
ElseIf b > a Then
lblResult.Caption = "B is greater"
Else
lblResult.Caption = "Both are equal"
End If
End Sub
3. Odd or Even Checker
Private Sub cmdCheck_Click()
Dim num As Integer
num = Val(txtNumber.Text)
If num Mod 2 = 0 Then
lblResult.Caption = "Even Number"
Else
lblResult.Caption = "Odd Number"
End If
End Sub
4. Simple Login System
Private Sub cmdLogin_Click()
Dim username As String
Dim password As String
username = txtUsername.Text
password = txtPassword.Text
If username = "admin" And password = "1234" Then
MsgBox "Login Successful"
Else
MsgBox "Invalid credentials"
End If
End Sub
5. Area of a Circle
Private Sub cmdArea_Click()
Dim radius As Double
Dim area As Double
radius = Val(txtRadius.Text)
area = 3.1416 * radius * radius
lblArea.Caption = "Area: " & area
End Sub
6. Temperature Converter (Celsius to Fahrenheit)
Private Sub cmdConvert_Click()
Dim celsius As Double
Dim fahrenheit As Double
celsius = Val(txtCelsius.Text)
fahrenheit = (celsius * 9 / 5) + 32
lblFahrenheit.Caption = "Fahrenheit: " & fahrenheit
End Sub
7. Currency Converter
Private Sub cmdConvertCurrency_Click()
Dim usd As Double
usd = Val(txtUSD.Text)
lblZWL.Caption = "ZWL: " & usd * 1000
End Sub
8. Voting Eligibility
Private Sub cmdCheckAge_Click()
Dim age As Integer
age = Val(txtAge.Text)
If age >= 18 Then
lblResult.Caption = "Eligible to vote"
Else
lblResult.Caption = "Not eligible"
End If
End Sub
9. Multiplication Table
Private Sub cmdTable_Click()
Dim num As Integer
Dim i As Integer
num = Val(txtNum.Text)
lstTable.Clear
For i = 1 To 10
lstTable.AddItem num & " x " & i & " = " & (num * i)
Next i
End Sub
10. Grade Checker
Private Sub cmdCheckGrade_Click()
Dim mark As Integer
mark = Val(txtMark.Text)
If mark >= 75 Then
lblGrade.Caption = "A"
ElseIf mark >= 60 Then
lblGrade.Caption = "B"
ElseIf mark >= 50 Then
lblGrade.Caption = "C"
ElseIf mark >= 40 Then
lblGrade.Caption = "D"
Else
lblGrade.Caption = "F"
End If
End Sub
11. Square Root Calculator
Private Sub cmdSqrt_Click()
Dim num As Double
num = Val(txtNumber.Text)
lblResult.Caption = "Square Root: " & Sqr(num)
End Sub
12. Count Characters in a Word
Private Sub cmdCountChar_Click()
Dim word As String
word = txtWord.Text
lblLength.Caption = "Characters: " & Len(word)
End Sub
13. Reverse Text
Private Sub cmdReverse_Click()
Dim txt As String, revTxt As String, i As Integer
txt = txtInput.Text
For i = Len(txt) To 1 Step -1
revTxt = revTxt & Mid(txt, i, 1)
Next i
lblReverse.Caption = revTxt
End Sub
14. Palindrome Checker
Private Sub cmdCheckPalindrome_Click()
Dim txt As String, revTxt As String, i As Integer
txt = txtInput.Text
For i = Len(txt) To 1 Step -1
revTxt = revTxt & Mid(txt, i, 1)
Next i
If txt = revTxt Then
lblResult.Caption = "Palindrome"
Else
lblResult.Caption = "Not a Palindrome"
End If
End Sub
15. Leap Year Checker
Private Sub cmdLeapYear_Click()
Dim year As Integer
year = Val(txtYear.Text)
If (year Mod 4 = 0 And year Mod 100 <> 0) Or (year Mod 400 = 0) Then
lblResult.Caption = "Leap Year"
Else
lblResult.Caption = "Not a Leap Year"
End If
End Sub
16. List Even Numbers (1 to 20)
Private Sub cmdListEven_Click()
Dim i As Integer
lstEven.Clear
For i = 2 To 20 Step 2
lstEven.AddItem i
Next i
End Sub
17. List Odd Numbers (1 to 20)
Private Sub cmdListOdd_Click()
Dim i As Integer
lstOdd.Clear
For i = 1 To 20 Step 2
lstOdd.AddItem i
Next i
End Sub
18. Display Today's Date
Private Sub cmdToday_Click()
lblDate.Caption = "Today is: " & Date
End Sub
19. Countdown (10 to 1)
Private Sub cmdCountdown_Click()
Dim i As Integer
lstCount.Clear
For i = 10 To 1 Step -1
lstCount.AddItem i
Next i
End Sub
20. Simple Calculator with Select Case
Private Sub cmdCalc_Click()
Dim a As Double, b As Double
Dim op As String
a = Val(txtA.Text)
b = Val(txtB.Text)
op = txtOp.Text
Select Case op
Case "+"
lblResult.Caption = "Sum: " & a + b
Case "-"
lblResult.Caption = "Difference: " & a - b
Case "*"
lblResult.Caption = "Product: " & a * b
Case "/"
If b <> 0 Then
lblResult.Caption = "Quotient: " & a / b
Else
lblResult.Caption = "Divide by zero error"
End If
Case Else
lblResult.Caption = "Invalid operator"
End Select
End Sub
21. Display First and Last Character
Private Sub cmdFirstLast_Click()
Dim text As String
text = txtInput.Text
lblFirst.Caption = "First: " & Left(text, 1)
lblLast.Caption = "Last: " & Right(text, 1)
End Sub
22. Age from Birth Year
Private Sub cmdCalculateAge_Click()
Dim birthYear As Integer
birthYear = Val(txtYear.Text)
lblAge.Caption = "Age: " & Year(Date) - birthYear
End Sub
23. Display Day of the Week
Private Sub cmdShowDay_Click()
lblDay.Caption = "Today is: " & WeekdayName(Weekday(Date))
End Sub
24. Swap Two Numbers
Private Sub cmdSwap_Click()
Dim a As Integer, b As Integer, temp As Integer
a = Val(txtA.Text)
b = Val(txtB.Text)
temp = a
a=b
b = temp
lblResult.Caption = "A: " & a & " B: " & b
End Sub
25. Login with Attempt Limit (3 tries)
Dim attempts As Integer
Private Sub cmdLogin_Click()
If attempts >= 3 Then
MsgBox "Too many attempts. Access blocked."
Exit Sub
End If
If txtUser.Text = "admin" And txtPass.Text = "1234" Then
MsgBox "Login successful"
attempts = 0
Else
attempts = attempts + 1
MsgBox "Wrong username or password. Attempt: " & attempts
End If
End Sub
26. FOR Loop – Squares of Numbers 1 to 5
Private Sub cmdSquare_Click()
Dim i As Integer
lstSquare.Clear
For i = 1 To 5
lstSquare.AddItem i & " squared = " & (i * i)
Next i
End Sub
27. WHILE Loop – Print 1 to 5
Private Sub cmdWhile_Click()
Dim i As Integer
i=1
lstWhile.Clear
While i <= 5
lstWhile.AddItem i
i=i+1
Wend
End Sub
28. DO UNTIL Loop – Countdown from 5 to 1
Private Sub cmdDoUntil_Click()
Dim i As Integer
i=5
lstDoUntil.Clear
Do Until i = 0
lstDoUntil.AddItem i
i=i-1
Loop
End Sub
29. SELECT CASE – Day of the Week
Private Sub cmdSelectCase_Click()
Dim dayNum As Integer
dayNum = Val(txtDayNumber.Text)
Select Case dayNum
Case 1: lblDay.Caption = "Sunday"
Case 2: lblDay.Caption = "Monday"
Case 3: lblDay.Caption = "Tuesday"
Case 4: lblDay.Caption = "Wednesday"
Case 5: lblDay.Caption = "Thursday"
Case 6: lblDay.Caption = "Friday"
Case 7: lblDay.Caption = "Saturday"
Case Else: lblDay.Caption = "Invalid input"
End Select
End Sub
30. NESTED IF – Determine Eligibility
Private Sub cmdEligibility_Click()
Dim age As Integer
age = Val(txtAge.Text)
If age >= 18 Then
If age >= 60 Then
lblStatus.Caption = "Eligible for senior benefits."
Else
lblStatus.Caption = "Eligible for adult services."
End If
Else
lblStatus.Caption = "Not eligible."
End If
End Sub
31. IF THEN ELSEIF – Grade Range Checker
Private Sub cmdCheckGradeRange_Click()
Dim grade As String
grade = UCase(txtGrade.Text)
If grade = "A" Then
lblGrade.Caption = "Excellent"
ElseIf grade = "B" Then
lblGrade.Caption = "Good"
ElseIf grade = "C" Then
lblGrade.Caption = "Average"
ElseIf grade = "D" Then
lblGrade.Caption = "Poor"
Else
lblGrade.Caption = "Fail"
End If
End Sub
32. FOR EACH Loop – Loop through array
Private Sub cmdForEach_Click()
Dim fruits() As String
Dim fruit As Variant
fruits = Split("apple,banana,orange", ",")
lstFruits.Clear
For Each fruit In fruits
lstFruits.AddItem fruit
Next
End Sub
33. SIMPLE FUNCTION CALL – Square of a number
Private Function GetSquare(ByVal x As Integer) As Integer
GetSquare = x * x
End Function
Private Sub cmdUseFunction_Click()
Dim num As Integer
num = Val(txtNum.Text)
lblResult.Caption = "Square: " & GetSquare(num)
End Sub
34. INPUT VALIDATION – Number Between 1 and 10
Private Sub cmdValidateRange_Click()
Dim num As Integer
num = Val(txtInput.Text)
If num >= 1 And num <= 10 Then
lblResult.Caption = "Valid input"
Else
lblResult.Caption = "Number must be between 1 and 10"
End If
End Sub
35. MULTIPLE CONDITIONS – Even and greater than 10
Private Sub cmdCheckMultiple_Click()
Dim num As Integer
num = Val(txtNumber.Text)
If num Mod 2 = 0 And num > 10 Then
lblResult.Caption = "Even and greater than 10"
Else
lblResult.Caption = "Does not meet condition"
End If
End Sub
36. Count from A to Z using ASCII
Private Sub cmdAlphabet_Click()
Dim i As Integer
lstLetters.Clear
For i = 65 To 90
lstLetters.AddItem Chr(i)
Next i
End Sub
37. Generate Multiples of a Number
Private Sub cmdMultiples_Click()
Dim num As Integer, i As Integer
num = Val(txtNumber.Text)
lstMultiples.Clear
For i = 1 To 10
lstMultiples.AddItem num * i
Next i
End Sub
38. Reverse an Array
Private Sub cmdReverseArray_Click()
Dim arr(1 To 5) As String
Dim i As Integer
arr(1) = "Apple"
arr(2) = "Banana"
arr(3) = "Cherry"
arr(4) = "Date"
arr(5) = "Elderberry"
lstReverse.Clear
For i = 5 To 1 Step -1
lstReverse.AddItem arr(i)
Next i
End Sub
39. Combine First and Last Name
Private Sub cmdCombine_Click()
Dim firstName As String
Dim lastName As String
firstName = txtFirst.Text
lastName = txtLast.Text
lblFullName.Caption = "Full Name: " & firstName & " " & lastName
End Sub
40. Password Strength Checker
Private Sub cmdCheckStrength_Click()
Dim pwd As String
pwd = txtPassword.Text
If Len(pwd) >= 8 And pwd Like "*#*" Then
lblStrength.Caption = "Strong Password"
Else
lblStrength.Caption = "Weak Password"
End If
End Sub
41. Find Average of 3 Numbers
Private Sub cmdAverage_Click()
Dim a As Double, b As Double, c As Double, avg As Double
a = Val(txtA.Text)
b = Val(txtB.Text)
c = Val(txtC.Text)
avg = (a + b + c) / 3
lblAverage.Caption = "Average: " & avg
End Sub
42. Toggle Label Visibility
Private Sub cmdToggle_Click()
lblHidden.Visible = Not lblHidden.Visible
End Sub
43. Convert Minutes to Hours and Minutes
Private Sub cmdConvertTime_Click()
Dim totalMinutes As Integer
Dim hours As Integer, minutes As Integer
totalMinutes = Val(txtMinutes.Text)
hours = totalMinutes \ 60
minutes = totalMinutes Mod 60
lblTime.Caption = hours & " hrs " & minutes & " mins"
End Sub
44. Change Background Color
Private Sub cmdChangeColor_Click()
Me.BackColor = RGB(255, 200, 150)
End Sub
45. Word Count in Sentence
Private Sub cmdWordCount_Click()
Dim sentence As String
Dim words() As String
sentence = Trim(txtSentence.Text)
words = Split(sentence)
lblWordCount.Caption = "Word count: " & UBound(words) + 1
End Sub
46. Capitalize First Letter of Each Word
Private Sub cmdCapitalize_Click()
Dim text As String
text = txtInput.Text
lblOutput.Caption = StrConv(text, vbProperCase)
End Sub
47. Validate Empty Input
Private Sub cmdValidate_Click()
If txtName.Text = "" Then
MsgBox "Please enter your name."
Else
lblResult.Caption = "Hello, " & txtName.Text
End If
End Sub
48. Length of a Word
Private Sub cmdLength_Click()
Dim word As String
word = txtWord.Text
lblLength.Caption = "Length: " & Len(word)
End Sub
49. Time-Based Greeting
Private Sub cmdGreet_Click()
Dim hour As Integer
hour = Hour(Now)
If hour < 12 Then
lblGreeting.Caption = "Good morning"
ElseIf hour < 18 Then
lblGreeting.Caption = "Good afternoon"
Else
lblGreeting.Caption = "Good evening"
End If
End Sub
50. Display Your Initials
Private Sub cmdInitials_Click()
Dim fname As String, lname As String
fname = txtFName.Text
lname = txtLName.Text
lblInitials.Caption = "Initials: " & Left(fname, 1) & Left(lname, 1)
End Sub
51. Average of Numbers in Array
Private Sub cmdArrayAvg_Click()
Dim arr(1 To 5) As Double
Dim i As Integer, total As Double
For i = 1 To 5
arr(i) = Val(InputBox("Enter number " & i))
total = total + arr(i)
Next i
lblAverage.Caption = "Average: " & total / 5
End Sub
52. Show Current Time
Private Sub cmdTime_Click()
lblTime.Caption = "Current time: " & Time
End Sub
53. Simple Interest Calculator
Private Sub cmdInterest_Click()
Dim p As Double, r As Double, t As Double
p = Val(txtPrincipal.Text)
r = Val(txtRate.Text)
t = Val(txtTime.Text)
lblInterest.Caption = "Simple Interest: " & (p * r * t) / 100
End Sub
54. Circle Circumference
Private Sub cmdCircumference_Click()
Dim r As Double
r = Val(txtRadius.Text)
lblCircumference.Caption = "Circumference: " & 2 * 3.1416 * r
End Sub
55. Countdown Timer (With Timer Control)
Private Sub Timer1_Timer()
If lblCount.Caption > 0 Then
lblCount.Caption = lblCount.Caption - 1
Else
Timer1.Enabled = False
MsgBox "Time's up!"
End If
End Sub