Modules (.BAS Files)

[ Using VB ]

    Modules can be a necessity to almost every advanced program you might make.  With a module, you can create global variables, make your own functions, and declare the Sub Main() which is what the program starts with.

Global Variables
Creating Your Own Functions
Sub Main()


Global Variables

    Global is a way of dimensioning variables.  Rather than using the Dim statement, you may want to occasionally use the Global statement.  When you declare a variable in a module using the Global statement, you can use that variable in every Sub procedure or function on any form.  To declare a variable, just do it the same way you would using Dim.  (ex:  Global strFirstName As String)  In this example, you could have the user type in his name and assign it to the variable strFirstName.  Then, in the rest of your project, you can use the user's name.


Creating Your Own Functions

    If you find yourself writing the same code in different parts of your program, you may want to create your own function.  To do this, just create a module....

wpe1B.jpg (6563 bytes)

....then click on Tools and Add Procedure.  When the dialog box appears, change the type to Function and leave the scope as public.

Modules.1.gif (15109 bytes)

The code window that pops up when you hit okay should say:    

                    Public Function PasswordCheck()

                    End Function

In this example, the code will check to see if a password that the user entered in a text box matches the correct password.  To make this work, just add the following code:

Public Function PasswordCheck(UserPassword As String, CorrectPassword As String)
If UCase(UserPassword) = UCase(CorrectPassword) Then
    blnPasswordOkay = True
Else

    blnPasswordOkay = False
End If
End Function

For this program to work, you need to have "Global blnPasswordOkay As Boolean" in the general, declarations area of your module.  Your form could look something like this:

Modules2.gif (4000 bytes)

If you wanted, you could use the input box function in order to get the password from the user.  The code for the "OK" button would be:

Private Sub cmdOK_Click()
Dim x As String
x = PasswordCheck(txtPassword, "Let me in")
If blnPasswordOkay = True Then
    frmSecretArea.Show  'This is the form that appears when the correct password is inputed
    Form1.Hide                  'Hides the password form
Else
    MsgBox "Password Incorrect"     'Gives a message box if the password is wrong.
End If
End Sub


Sub Main()

   The Sub Main() is what the program starts with.  If you want to start the program with an input box or a message box, you could use this function.  For example, in the above password example, you could start the program by asking for a password in an input box.  To do this, create a new procedure in your module (Tools - Add Procedure).  This one should be named "Main" and should be a sub, instead of a function. 

Modules3.gif (15050 bytes)

    The code in this sub that is created will be the first code that is run if you set it up that way.  To do this, go to the Project menu, then click on Project 1 Properties.  Under the default tab "General", find where it says "Startup Object:" and change that from Form1 to Sub Main.

Modules4.gif (34468 bytes)

    The code behind the Sub Main will be the code that asks for a password. 

                Public Sub Main()
                Dim strUserInput As String
                strUserInput = PasswordCheck(InputBox("Please Enter Your Password", "Password Check"), "Let me in")
                If blnPasswordOkay = True Then
                    frmSecretArea.Show
                End If
                End Sub

wpe4.jpg (7281 bytes)