Search Tools Links Login

VB6 Tutorial 57: Sub Procedures


Before proceeding with this lesson, be sure to read the section covering Variable Scopes.

As we have discussed in the lesson about variable scoping, that procedures are of three types :

Sub Procedures are of two types :

Every procedure has a unique name, a scope and a list of arguments. The function procedure, in addition, has a return value. Sub procedure and property procedure does not return value.

Scope of a procedure

A procedure can be Private, Public or Friend. You can invoke a Private procedure only from within the module. A Public procedure can be called from anywhere of the current project -- from within the module, from other modules of your application project, and in some cases from outside the program using COM. And a friend procedure can be called from anywhere of the current project but not from outside.

Understanding the Event Procedure

An event procedure does not return any value and you can call it from other procedures of the module. The procedure is called using the Call command.

Example

 

Private Sub Command1_Click()
    Call Command2_Click
End Sub

Private Sub Command2_Click()
    Print "hello"
    MsgBox "hello"
End Sub

 

So if you click the Command1 button, the Command2_Click event procedure is called, and the lines of code inside it are executed.

You may omit the Call command while calling a procedure.

Example

 

Private Sub Command1_Click()
    Command2_Click
End Sub

Private Sub Command2_Click()
    MsgBox "hello"
End Sub

 

Public event procedure

Example

Place a CommandButton on Form1. Now add a new form from the menu : Project > Add Form > Form. Place a CommandButton on Form2.

 

'In Form1
Private Sub Command1_Click()
    Call Form2.Command1_Click
End Sub

 

 

'In form2
Public Sub Command1_Click()   'Scope is Public
    MsgBox "You have called a procedure of form2"
End Sub

 

When you click the CommandButton on Form1, Command1_Click procedure of the form2 module will be called. As the scope of the procedure in Form2 is Public, the procedure is accessible from anywhere of the current project, and if the procedure is Private, the procedure cannot be invoked from other modules. In the Form2 module, if you change the procedure scope to Private, it can then only be called from the Form2 module. This aspect will be clearer when you'll learn about working with multiple forms in the next lessons.

Understanding the general sub procedure

General sub procedure can be very useful when you want to use a block of code repeatedly in your program. Instead of writing the same lines of code again and again, define a General Sub Procedure, and call it where you need. These procedures do not return values.

Say you need to print some lines of text again and again. So define a general sub procedure xprint , and call it wherever in the program its necessary.

Note: Public is the default scope for procedures, so you may omit it.

Example

 

Sub txt()
    MsgBox "welcome"
End Sub

 

Example

 

Private Sub Command1_Click()
    Call xprint      'Function calling
End Sub

Private Sub xprint()   'Function Definition
    Print "Hello World"
    Print "";
    Print "*****"
    Print "New"
End Sub

Private Sub Command2_Click()
    Call xprint      'Function calling
End Sub

 

Using sub procedures in your code is a good programmer's habit. Because it reduces the number of lines of code. Besides, a large complex program becomes very easy and comprehensive when you use your own sub procedure in the code.

You can change the scope to Public when you want to access the procedure from outside the module.

Public general sub procedure

Add a new form from menu : Project > Add Form > Form. Public general sub procedures are useful when want to invoke your procedure from wherever you want, from any module.

Example

 

'In Form1
Private Sub Command1_Click()
    Call Form2.xprint
End Sub

 

 

'In form2
Public Sub xprint()
    Form2.Show
    Print "Hello World !"
    Print "";
    Print "***"
    Print "New"
End Sub

Private Sub Command1_Click()
    Call xprint
End Sub

 

The concept of Public general sub procedure will be clearer to you when you'll learn about working with multiple forms in the next chapters.

About this post

Posted: 2018-05-30
By: vb6boy
Viewed: 811 times

Categories

VB6 Tutorial

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.

ADODB.Connection error '800a0e79'

Operation is not allowed when the object is open.

/assets/inc/inc_footer.asp, line 37