VB6 Tutorial 57: Sub Procedures

Posted On 2018-05-30 by VB6Boy
Keywords: visual,basic,vb6,visualbasic,tutorial,basic,beginner
Tags: VB6 Tutorials 
Views: 17


Looking for the more of the VB6 Tutorial? Here is the tutorial index.

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 Procedure
  • Function Procedure
  • Property Procedure

Sub Procedures are of two types :

  • Event Procedure
  • General Procedure

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.

Looking for the more of the VB6 Tutorial? Here is the tutorial index.


About the Author

has posted a total of 101 articles.


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.159.91.117

Before you can post, you need to prove you are human. If you log in, this test goes away.


Code Links