What is the difference between module and form in vb

Joel Siii
Joel Siii used Ask the Experts™
on
Hi everyone, I need a bit of clarity when it comes to the difference between module and forms in vb
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Database Expert
Awarded 2016
Top Expert 2016
Commented:
Form - GUI ( graphical user interface ) It is something that the the user will have interaction with. It can and usually does contain code specific to the controls that exist on the form. These might be buttons, listboxes, etc.

A module in vb is used to encapsulate code. At its purest form a module would contain code used for specific tasks and a new module would be used for other specific tasks.

In the past, I have created modules that contain utility routines that I find I use frequently. I can then include the module of my utility routines into any new project I have and reuse that code.

Code or functions/subs in a VB form can be addressed using the Me prefix. A module does not exist within a form, so that context is not available.

A module would be a part of the application.

From old EE database.
Martin Liss"Life would be infinitely happier if we could only be born at the age of eighty and gradually approach eighteen." - Mark Twain
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
A module does not exist within a form, so that context is not available.
That's not true. The code behind forms/userforms is referred to as the "form module".
Commented:
Forms are modules, but they are also a class.  Apart from the obvious (A form has a UI), this is the main difference.  

A module is not a class.  

With a class you can create an instance, which you can't do with a module.  You can also called static methods which is like calling a sub or function from a module.
Máté FarkasDatabase Developer and Administrator
Commented:
Form: this is an object which copies a build in class UserForm in VB. Your Form object has some initial capabilities:
      - has a window with common window controls (close, minimize, maximize, resize with mouse)
      - can draw itself on the screen (with Show method) or hide (with Hide method)
      - can contain other control like buttons (CommandButton), text fields (TextBox)
      - can manage keyboard and mouse events
      - etc. you can extend the capabilities with your methods and controls
Form is similar to Class.

Module: this is only a code file which contains some functions, procedures, variables which are independent for any other Forms or Classes. You can call your functions from your module in any part of your program.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial