Lesson 16: Sub Procedures in VB2019

Master modular programming with reusable code blocks

Key Takeaway

Sub procedures allow you to break your code into reusable, modular components that can be called multiple times from different parts of your application.

In this lesson, we'll explore sub procedures in Visual Basic 2019. Sub procedures (also called subroutines) are essential for organizing code, reducing redundancy, and creating modular applications that are easier to maintain and debug.

16.1 Understanding Sub Procedures

A sub procedure is a block of code that performs a specific task. Unlike functions, sub procedures do not return a value. They are typically used to:

  • Accept input from the user
  • Display information
  • Manipulate properties
  • Perform repetitive tasks
  • Organize complex operations

Sub Procedure Structure

Sub ProcedureName(parameters)
    ' Code to execute
End Sub

The parameters are optional data inputs that can be passed into the sub procedure to customize its behavior.

16.1(a) Creating a Simple Sub Procedure

This example demonstrates a basic sub procedure that displays a sum of two numbers passed as parameters.

Form1.vb
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Call the sub procedure with parameters
    sum(5, 6)
End Sub

' Define the sub procedure
Sub sum(a As Single, b As Single)
    ' Display the result
    MsgBox("sum=" & a + b)
End Sub

Output:

> Displays a message box with "sum=11"
Sub procedure output
Figure 16.1: Message box showing the sum result

16.2 Practical Application: Password Cracker

This example demonstrates a more complex use of sub procedures to create a password cracker simulation. The program generates random passwords and checks if they match the actual password.

Form1.vb
Public Class Form1
    Dim password As Integer
    Dim crackpass As Integer

    Private Sub BtnGenerate_Click(sender As Object, e As EventArgs) Handles BtnGenerate.Click
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        ' Call the generate sub procedure
        generate()
        
        If crackpass = password Then
            Timer1.Enabled = False
            LblResult.Text = crackpass
            MsgBox("Password Cracked! Login Successful!")
        Else
            LblResult.Text = crackpass
            LblStatus.Text = "Please wait..."
        End If
    End Sub

    ' Sub procedure to generate passwords
    Sub generate()
        crackpass = Int(Rnd() * 100) + 100
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        password = 123
    End Sub
End Class

Output:

> Starts generating passwords > Displays current password attempt > Shows success message when password is cracked
Password cracker interface
Figure 16.2: Password cracker during generation
Password cracked message
Figure 16.3: Success message after cracking password

16.3 Benefits of Using Sub Procedures

Code Reusability

Write once, use multiple times throughout your application without duplicating code

Modularity

Break complex problems into smaller, manageable pieces that are easier to understand

Easier Debugging

Isolate and test individual components of your application more effectively

Team Collaboration

Enable multiple developers to work on different parts of the application simultaneously

Lesson Summary

In this lesson, you've learned how to create and use sub procedures in Visual Basic 2019 to organize your code and create modular applications:

Sub Procedure Structure

Mastered the syntax for creating sub procedures with parameters

Calling Procedures

Learned how to invoke sub procedures from event handlers and other code

Practical Application

Implemented a password cracker simulation using sub procedures and timers

Code Organization

Understood the benefits of modular programming for maintainability

Sub procedures are fundamental for creating well-organized, maintainable applications. By breaking your code into logical units, you can create applications that are easier to understand, debug, and extend. In the next lesson, we'll explore functions that return values.

Next Lesson

Ready to learn about functions that return values? Continue to Lesson 17: Creating Functions.

Related Resources

VB6 Tutorial

Mastering VB6 Programming

Explore Tutorials

Visual Basic Examples

Practical VB code samples for real-world applications

View Examples

Excel VBA Tutorial

Learn how to automate Excel by creating VBA macros

Learn More

VB2019 Paperback

Comprehensive guide to Visual Basic 2019

Get on Amazon