Arcade Prehacks

Results 1 to 4 of 4
  1. #1
    variable's Avatar
    Join Date
    Dec 2010
    Location
    Neverland
    Posts
    91

    Working with modules - VB 6, Beginners

    In this tuturial I'm going to teach you how to work with modules.
    You're gonna need a lot of time to read this so I suggest saving this in a text file and reading later when you have some free time.

    --------------------------------
    What things you will learn:
    --------------------------------
    1. What is private and public Subs/Functions.
    2. What is the differences between subroutines and functions.
    2. How to work with subroutines (Subs are also known as subroutines) in modules.
    3. How to work with functions in modules.
    4. How to work with functions in modules (Much advanced).
    5. LOADS of Example on everything.

    This one is for the beginners though if you know the usage, you can still revise.
    Modules can be hard to understand for a beginner. I still remember when I used other people's module for my projects wondering how the hell they work.. Then I researched a lot on all modules and finally I understood it's functions (These sh*ts were hard to understand myself).
    The worst thing about modules is that NO one teaches you how to WORK with it. They just teach you in their tutorials what is a Public Sub and a Public Function which is of no use to the fellow beginners.
    Whatever, if you're facing the problems I did then this tutorial is just for you. Just hold tight and understand each line. I will make sure you are through with modules in this tut.

    You should have the BASIC OF THE BASIC knowledge on VB first though.

    So, What is a module?
    A module is a special kind of *place* (dunno what I should call it) where you can store your Subs and Functions which will ease up your life. Later you can call that function/sub EASILY from ANY Form.

    What kinda data does modules use?
    Modules store ALL the datas As Public (It can store private as well. Not necessary though. ALWAYS USE PUBLIC). So, what is the public crap? To understand public, open up VB and Create a new project. No need to save though. Now, make a command button on the form. Now, double click on the Command1 and a piece of code vb should automatically make up. That is the Command1_Click Event.
    Now, look at the code. It should be:

    Code:
    Private Sub Command1_Click()
    
    End Sub

    Notice the word "Private Sub"? It states that this control (Command1) can ONLY be used within Form1. That is, making another form (Form2), you won't be able to change the Command1's Caption using Form2. Problems? See the example below:

    Code:
    Private Sub Form_Load()	'This is Form2's code
    	Command1.Caption = "Crap"
    End Sub
    It would create an error. Whatever. Now, if you use Public Instead of Private then guess what? Any Form can use it (Only applicable for variables. NOT CONTROLS. though MODULES can still change control's properties in a different manner).

    That is for example:

    Code:
    Dim x as Integer
    
    Private Sub Command1_Click()
    	x = x + 1
    End Sub
    That code would increase x by 1 each time we click the button. But our variable x is not usable by any other form! Why? BECAUSE IT IS NOT PUBLIC!
    So, let's see what happens if we do this.
    We insert a new module (Project => Add Module) and type this:

    Code:
    Public x As Integer
    Guess what? Now we can use x in ANY Form! Form1 or Form2 or Form3 or Form(Whatever). For example, see this:

    Code:
    Private Sub Form_Load() 'Form2's code.
    	Label1.Caption = x
    End Sub
    Another example:

    Code:
    Private Sub Form_Load() 'Form3's code
    	x = 0
    End Sub
    See? Every form can access it! Though don't try this with controls. The controls cannot be made public this way.

    Wheeew! That concludes Public and Private Subs! Hope you understood cause' you're gonna need it to understand later parts.

    Now, Let's get on the fun part! Let's get to know how we would use modules (REMEMBER MODULES CAN ONLY SUPPORT PUBLIC).
    Modules can be off two types:
    1. Public Sub and 2. Public Function

    What is a public sub?
    It is best the same thing as a private Sub. Private Sub Example:

    Code:
    Private Sub Form_Load()
    
    End Sub
    Public Subs are almost the same except the fact that you cannot use the easy way to change control's properties. Dunno what crap I'm talking about?
    Okey then let's see an example (of course this goes in a module):

    Code:
    Public Sub ClickOnCommand1()	'Module Code
    	Command1.Caption = "Wheew!"
    End Sub
    Use that code and it wouldn't work.. Because, you cannot change these properties like normal sub routines..
    It would cause problems.. We can use this for correction:

    Code:
    Public Sub ClickOnCommand1()
    	Form1.Command1.Caption = "Wheew!"
    End Sub
    This would work fine when we would call this sub. How to USE this public sub? Use:

    Code:
    Private Sub Form_Load()
    	ClickOnCommand1
    End Sub
    Or,

    Code:
    Private Sub Form_Load()
    	Call ClickOnCommand1 'See the word Call? You can use the previous example too. Your choice!
    End Sub
    Both codes will work just fine. So, that concludes the Public Sub.. Now for the Public Function!

    Public Functions are one of the most usefull things to use. They can return a value for you to use (Read more to understand).
    We know we declare Public Subs like this:

    Code:
    Public Sub WhatEver()
    
    End Sub
    But how do we declare Public Functions?

    We declare them with a Variable attached to it at it's end (Which would return a value after it's execution)!
    This may sound a little complicated, but it's not! See the Example below:

    Code:
    Public Function WhatEver()As Interger
    
    End Function
    See? WhatEver would return an Integer value for us to use! To make WhatEver come to life, let's see an example:

    Code:
    Public Function WhatEver()As Integer
    Dim x As Integer, y As Integer, z As Integer 'Beginners: Declare multiple variables this way
    	x = 5
    	y = 5
    	z = 5
    	WhatEver = x + y + z	'Notice this code. It sets our Function WhatEver's value to the value of x + y + z (i.e 15)
    End Function
    Now to test, we can use:

    Code:
    Private Sub Form_Load()
    	MsgBox WhatEver
    End Sub
    That would show the result of x + y + z = 15 in a message box!
    Geez that worked!

    Now, I've shown you only a basic Function. Let's come to a little advanced type of Function.

    The above code is just a simple function. Instead of using x, y, z internally as we did in the previous Example we can send our function WhatEver the values of x, y and z! Yes it is possible!
    So, how can we do that? See the example below:

    Code:
    Public Function WhatEver(x As Integer, y As Integer, z As Integer)As Integer
    	WhatEver = x + y + z
    End Function
    See the new x, y and z? We need to give the values of x, y and z when we call the WhatEver Function (These are called ARGUMENTS).
    To test it, we can use this:

    Code:
    Private Sub Form_Load()	
    	MsgBox WhatEver(10,10,10)	'This would show 30 in a message box
    End Sub
    Or,

    Code:
    Private Sub Form_Load()	
    	MsgBox WhatEver(5,5,5)	'This would show 15 in a message box
    End Sub
    You can change the values OR for better, make three textboxes and use MsgBox WhatEver(Text1.Text, Text2.Text, Text3.Text) in a Command Button's click event. If you don't understand what the heck I'm talking about, see this example:
    Make three textboxes (Text1, Text2, Text3) and a command button (Command1).
    Use this code:

    Code:
    Private Sub Command1_Click()
    	MsgBox WhatEver(Text1.Text, Text2.Text, Text3.Text)
    End Sub
    When the Button is clicked, it will show the sum of the numbers of Text1, Text2 and Text3.

    Now we shall learn how to use optional arguments.
    Now, this is not really useful. But if you are thinking about releasing your code to the public this can be handy.

    Okay, What is an optional argument?
    Optional arguments are one kind of argument which can be used OR omitted when calling the procedure.

    To make you understand, here is an example:

    Code:
    Public Function WhatEver(x As Integer, y As Integer, z As Integer, Optional ShowResult As Boolean) As Integer
    
    End Function
    If you've understood the previous procedures then this should be a piece of cake. Now, if you look carefully, you can see that I have attached an argument called ShowResult which is a Boolean (returns true or false) and declared as optional.

    One thing to remember when using optional arguments is that they MUST BE SPECIFIED AFTER ALL OTHER ARGUMENTS. Which means it must be declared at the end.
    Okay, if you think you got that, let's see an example!

    Code:
    Public Function WhatEver(x As Integer, y As Integer, z As Integer, Optional ShowResult As Boolean)As Integer
    'We only check true, 'cause by default all boolean variables are false (note this)
    	If ShowResult = True Then
    		WhatEver = x + y + z
    	Else:
    		MsgBox "ShowResult was disabled!"
    	End If
    End Function
    Nice huh? If we set ShowResult's value to true when calling it, the message box will show the result. But if not, OR Omitted, another message would show up telling the sucker has done a mistake!
    To call it, we can use this:

    Code:
    Private Sub Form_Load()
    	MsgBox WhatEver(10,10,10,True)
    End Sub
    There. It should work. But if you use MsgBox WhatEver(10, 10, 10) Then the other message would still show up. Because ShowResult is Optional, you can ommit it as well. Which would cause ShowResult to be False.
    See this for reference:

    Code:
    Private Sub Form_Load()
    	MsgBox WhatEver(10,10,10)
    End Sub
    See? ShowResult was omitted. But still the function works and doesn't create an error. Optional arguments are not much useful by the way. Only use them when you are releasing your source code or making a function/subroutine which can use an argument and also can work without it. For Example: The 'Default' argument of the INI Read or Write procedure would set the default value the coder has asked IF the value was not found. Else it would return nothing (If you didn't understand this sh*t then not to worry! You don't have to! As long as you understood the previous examples). Another example is the built-in SaveSetting and GetSetting function.

    WHEEEEEEEEEEEEEEEEW! That should conclude my tutorial and I hope you got my point on how to use modules. They can be handy in all aspects. If you did not understand something or having trouble in anything, post a reply.

    -BladeX

  2. #2
    Senior Member
    Join Date
    Jul 2010
    Location
    The Netherlands
    Posts
    1,862

    Re: Working with modules - VB 6, Beginners

    Good tutorial, but I suggest to put all the code between tags
    I've been here before.

  3. #3
    variable's Avatar
    Join Date
    Dec 2010
    Location
    Neverland
    Posts
    91

    Re: Working with modules - VB 6, Beginners

    Thanks for the comment..
    Added the [code] tag, fixed grammar & spelling mistakes.
    Also provided more example and commented the codes better...

  4. #4
    Senior Member spiderkid12's Avatar
    Join Date
    Jun 2010
    Posts
    1,857

    Re: Working with modules - VB 6, Beginners

    And also please watch your language when you said
    Quote Originally Posted by BladeX
    (These ****s were hard to understand myself).
    Nice tut though. Very clear.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •