VB 6 Declaring A Subroutine

Advertisements
Advertisements

A subroutine is a block of code like function that do a specific task. The difference between subroutine and function is that a subroutine does not return a value. However, you may pass arguments to a subroutine in VB 6.

The syntax for subroutine is given below, the keywords within square bracket ([]) are optional.

[Private | Public | Friend] [Static] Sub-name [(argument list)]
...
[Statements]
...
[Exist Sub]
...
[Statements]
...
End Sub

Public keyword allows procedures from all forms and modules to access the public procedures.

Private keyword only allows access to procedures from forms and modules where private procedure is declared.

Friend keyword used in class modules (modules that implement classes) allows the procedure to be visible throughout the project except to the controller of the instance of an object.

Advertisements

Static keyword preserve the value of local variable (those declared inside the procedure) between calls. You can call a procedure several times, it would not lose the value of a static local variable.

Argument list is a list of variables that it passed to the procedure. You can have multiple arguments each separated by a comma.

Argument List: Sub Procedure

Argument list has following syntax:

[Optional] [ByVal | ByRef] [ParamArray] varname [()] [As type] = [default value]

Optional means that the argument is not required.

ByVal means the argument is passed to the procedure by value. ByRef means that the argument is passed as a reference rather than the value.

ParamArray is the last argument in the argument list and it indicates that the final argument is an array of Variant elements.

Varname is the name of the argument being passed to the procedure.

type is the data type of the argument.

defualt value is a constant or a constant expression set when you use the [Optional] keyword.

Exit Sub cause the sub procedure to immediately exit itself.

Advertisements

End sub is to end the sub procedure.

Example: Sub Procedure

Here is an example of sub procedure.

Sub Matrix (Optional MatValues as Variant)
     If IsMissing (MatValues)  Then
    
    ' If value is not passed

     MsgBox("Value not passed to the Matrix")

    Else
    ...
   End If

End Sub

In the next article, we will learn about VB 6 function which is similar to sub procedure.

Visual Basic 6 For Dummies 1st Edition

Create your own custom Visual Basic programs fast for Windows 95, Windows 98, and Windows NT!

If you want to develop business applications for Windows, then Microsoft’s Visual Basic 6 programming environment is for you. With its powerful and friendly object-oriented interface, Visual Basic 6 is the ideal tool for creating easy-to-use and attractive interfaces of your own. Whether you want to build original programs from the ground up or take advantage of existing wizards, Visual Basic 6 For Dummies will help you design and debug your work – and accomplish all your goals!

  • Covers using Visual Basic 6 with Windows 95, Windows 98, and Windows NT
  • Walks you through the ins and outs of object-oriented programming in Visual Basic and using the Visual Basic toolset
  • Shows you how to make use of menus and submenus, write subprograms that everyone can share, save and retrieve information in databases, integrate ActiveX controls into your applications, and build attractive user interfaces with buttons, boxes, and scroll bars

From the fundamentals of writing BASIC code to the thrill of distributing your very own custom applications, Visual Basic 6 For Dummies brings a fresh perspective to programming in Visual Basic 6 that everyone can understand.

Advertisements
Advertisements
Advertisements