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.
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:

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.
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:


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

Visual Basic 2019 Made Easy
Unlock the power of Visual Basic 2019 with this comprehensive, easy-to-follow handbook written by Dr. Liew, renowned educator and founder of the popular programming tutorial website VBtutor.net. Whether you're new to programming or brushing up your skills, this book is your perfect companion to learn Visual Basic 2019 from the ground up.
What You'll Learn:
- Understand Core Programming Concepts: Grasp the foundational principles of Visual Basic 2019, including variables, data types, conditional logic, loops, and event-driven programming.
- Develop Real Windows Desktop Applications: Build fully functional and interactive Windows apps using Visual Studio 2019—guided through step-by-step tutorials.
- Apply Dozens of Ready-to-Use Examples: Explore a rich collection of practical sample programs, from basic calculators to image viewers and database applications.
- Adapt and Reuse Code for Your Own Projects: Customize professionally written code snippets to speed up your development process and bring your ideas to life.
- Package and Deploy Like a Pro: Learn how to compile, test, and distribute your Visual Basic applications seamlessly with built-in deployment tools.

Visual Basic Programming With Code Examples
Visual Basic Programming with Code Examples offers a unique dual-format approach, showcasing sample codes in both Visual Basic 6 (VB6) and VB.NET. This side-by-side presentation helps you understand the evolution of Visual Basic and empowers you to work confidently across both environments.
What You'll Learn:
- Core Concepts Made Easy: Explore data types, control structures, file handling, procedures, user interface design, and more.
- Hands-On Application Building: Design real-world applications, including financial calculators, educational tools, games, multimedia apps, and database systems.
- 48 Practical Code Examples: Study and customize fully explained programs that illustrate key programming techniques.
- Dual-Code Format: Learn to translate and adapt code between VB6 and VB.NET seamlessly.