Creating Code modules in Visual Basic

Three VisualBasic(VB) Modules are:
  • Form module
  • Standard module
  • Class module
In Visual Basic, a module is a part or a piece of code which, used to, perform some specific task . Modules is a  separate code file. In VB, for application development programmer normally insert code for application by double clicking on controls in desing mode. This action transfer the programmer from design window mode to code-window. The code usually contains declarations of variables, constants, types, procedures, functions event procedures etc etc. Visual Basic usually handles this as module for the form. By using VB module one can create common code for an entire application. Modules are helpful when programmer developing a large application which possibly involve the usage of same code at different part such as procedure,function. For instance a larger application may contain multiple forms and each form use some common code for performing a particular task. So there occurs rewriting or duplication of codes for each form, controls. By using module, which contains sub-procedures, event procedures or sub-functions, which possibly uses the once written code and it would be very helpful over rewriting. By default every code in VB is stores in modules
A module consist declarations and procedures.

Declaration contains declaration of variables, types, constants, dynamic link libraries. Procedures contains sub-procedures, event procedures, sub-functions.

The three modules namely, form, class, standard got its own extensions. Every Form module has .frm extension, class module has .cls extension and standard module has .bas extension.

Creating module in VB is simple. Open the VB IDE and choose create standard exe. Click Right button of mouse in any blank area in project explorer window and select Add from the pop-up. In it there are items like Form, MDI Form, Module, Class module etc. The Module item can be used to create standard module(.bas extension). And Class Module is used to create class (.cls extension) module. If you want to create standard module select the Module from the list and if it is class module you want then hit the ClassModule.

When you make your choice then a new code window appears. In this code window you can put the necessary declarations and procedure whatever you want. For example
  Public Sub modSub()
MsgBox "Welcome to VB Module"
 MsgBox "This is From sub procedure in module"
End Sub
or you can define function
 Public Function modFun(str As String) As String
modFun = str + " button pressed.Standard module working"
End Function
Let's try an example by creating an application that have a form and two command buttons as in figure.
Now we can create module
In the above figure (the window named Add Module)  click the Open button.
Type above code( above given sub procedure and Function) into the newly created module .
Return to form and double click the button and type the code into procedure
Private Sub Command1_Click()
Call Module1.modSub
End Sub

Private Sub Command2_Click()
a = modFun(Command2.Caption)
Label1.Caption = a
End Sub
Module1 can be the property name of the module you created
form1 Code window (Opens on double clicking of command button control on form)
Now run the program and click the buttons
After these steps check the Project Explorer window(usually visible on right side of the IDE), see below figure
There are two items, Form and Modules contains the form module and standard module. The extension .frm in Form1.frm means form module and .bas in Module1.bas means standard module By selecting Project menu from menu bar shows list of items. There is also Add module and class module options available.
VB program code does not come line by line. Instead they are separated in code files called modules, each of them is separated into procedures. Thus, VB program becomes well structured which is the key part of developing Visual Basic applications easily.
Source:Books,Internet

No comments: