Modules in Visual Basic

In this tutorial on Visual Basic Modules, we will look at how to take our organisational skills to a whole new level with our already super-organised code split into nicely named Subroutines and Functions.  Typically we have been developing single form applications.  Our code has sat quite happily in this form but say we have a Subroutine to handle a click event.  How do we share this routine between multiple forms?  Modules.

Create a new project called Modules.  In the form Load event add the following code.


Dim radiusOne as Double
Dim areaOne as Double

Dim radiusTwo as Double
Dim areaTwo as Double

radiusOne = 3
radiusTwo = 2

areaOne = CalculateArea( radiusOne )
areaTwo = CalculateArea( radiusTwo )

Add this routine to the Form too

Private Function CalculateArea (ByRef radius as Double) As Double

     Dim pi as Double

     pi = 355/113

     Return pi * radius ^ 2

End Sub

Add another Form to your Project (Select the Project with your mouse, Right click, Click Add and Click Windows Form as shown below)

new form

On this Form add

  • a textbox called textRadius
  • A label witht he text “Enter the Radius to calculate the area of a circle”
  • a label called labelArea
  • a button called “buttonCalculateArea” with the caption/text “Calculate Area”

Design the form so it looks roughly as follows.

calculate area form

Add the following code to the button click event.

Dim radius as Double

Dim area as Double

radius = CDbl( textRadius.Text)

area = CalculateArea( radius )

labelArea.Text = “The area of the circle is “ & CStr(area)

What are these new functions CDbl and CStr I hear you alert readers asking?  Well, a Textbox.Text property returns a string.  However our Function is expecting a Double as the radius.  The CDbl (Convert to Double) function converts the textbox text into a Double datatype (if numeric, otherwise an error is thrown).  Once we have the area from the CalculateArea function, we need to convert this Double back to a String to add to the text field of labelArea.  We we use the CStr (Convert to String) Function for this.

We want this form to be the first form that is loaded when the application starts.  We want this form to be the Startup Form.  To do this, click on the Solution in the Solution Explorer, right click on the form and click Properties as shown below.visual basic project properties

You should now see the the Project Properties dialog.

Set a startup form

In the Startup Form dropdown select Form2.

Go back to your project and Add a New Module called ModuleFormula

visual basic project add module

Double click on your Module to add code.  Cut and paste the CalculateArea Function from Form1 and paste it into the module.

Module ModuleFormulas

     Private Function CalculateArea (ByRef radius as Double) As Double

          Dim pi as Double

          pi = 355/113

          Return pi * radius ^ 2

     End Function

End Module

Click F5 to run your project.  What happens?  You get an error.  The error says that the ‘CalculateArea’ is not delcared and may be unavailable due to its protection level.  Remember our exercise on Variable Scope which stated Variable scope defines when a variable can be accessed.?  Functions and Subroutines have scope too.  Scope in it’s wider context defines when a variable, subroutine or function can be accessed.

The first line of our CalculateArea function is defined as follows:

Private Function CalculateArea(ByRef radius As Double) As Double

Private in this case tells Visual Basic that the Function is only available within our module.  If we declared the Function in the Form, which we did originally, we informed Visual Basic that the Function was only accessible from code within the Form.  Essentially, Private is a means of declaring the variable local to its container.

Change the word Private to Public to make the Function available anywhere in our application.

Public Function CalculateArea(ByRef radius As Double) As Double

Press F5 and what happens?  It works! Yay!

Enter a Radius, click calculate and you’ll see the exact area (perhaps too exact) of the circle.

Now you can access this function from anywhere in your application and you’ve started to organise the contents of your project.  You can create Visual Basic Modules with Functions and Subroutines for specific tasks.  For example, we could re-name this module moduleTrigonometry and build up the routines we need for calculating areas, radii etc.  It all depends what our application does and how we want to organise it.

You’ll notice when you gain more experience in coding that some programmers are extremely neat in organising their code, some not so.  I recommend being disciplined at this stage until being organised becomes second nature. Your co-workers will love you for it.