Essential Visual Basic

(for Web Programming)
 

Visual Basic is the most common language used to program in Windows, and is a remarkable improvement over the initial Basic language introduced more than 20 years ago. But whomever learnt to program in Basic in 70's will find the syntax of Visual Basic very familiar. The addition of forms and controls to provide a Windows GUI made much simpler the job of I/O and event programming.

The basic source for Visual Basic references is the Microsoft VBasic page, but I also recommend James S. Laferriere VB 6 Tutorial Page.

This tutorial is a very simple review of the Visual Basic pre-requisite course, focusing on topics related to modules and files. Visual Basic 6.0 (VB 6.0) is installed in  the UB labs, but not in the MISLab.


Development Environment

VB 6.0 is a graphical development environment. An application, or project, is a collection of modules, including forms (.frm), Basic code (.bas and .cls), custom controls (.vbx or .ocx) and resources (.res). Each project requires either a Form of a Main procedure to run. You can debug your program by running it and seeing results in the Debug Window. Other Windows are also open to help you develop programs, like Forms, Project, Properties. A menu, toolbar and a toolbox also make the programming task much easier. 

Forms and Controls (very brief tour). You can see an example of a form and the code to process it. You draw forms using the Toolbox. You set the properties of the form by changing its properties.  You can download the form and  the project files for practice. We will not go in more details in this tutorial about forms and controls for they were covered in the VB pre-requisite course.

VB 6.0 syntax overview

Introduction

Comments in VB 6.0 are marked with the ' (single quote) sign and run to the end of the line. There are no statement terminators like in Perl or C.  More than one statement can be entered in a line separated by the colon (:). A statement can go in more than one line using  _ in the end of a line. There are many types of variables in VB 6.0, including integer, long, single, double, currency, string, boolean, date, variant, user define, etc.   VB 6.0 supports a variety of control structures -- block statements, if/then/else, do/loop, For/Next, Foreach/Next (no goto is really supported). VB 6.0 provides easy I/O statements using forms and controls. Also good file I/O commands for sequential, random and binary files. Finally, functions/subroutines are also easy to create and use. Some VB 6.0 libraries are available for simplifying many of the Web tasks.
 

Variables

You declare variables taking into consideration their scope: local or global. In general you declare local variables using DIM varname [As type]. As we saw before a project is comprised of modules and forms (in the case of Web programming, only modules). A module has one or more procedures. Global variables are declare as PUBLIC varname [As type] in the Declarations section of a module. Three other cases are important for Web programming:  (a) fixed-lenght strings, (b) defined user type and (c) arrays. You should use help in VB 6.0 to obtain more information in these variable types. For rules on naming variables read this.
 

Control structures

Note: To exit a loop use Exit . Exiting a Do loop: Exit Do , a For loop: Exit For.
 

Procedures

You create a procedure in a module or form. We will only discuss creating procedures in modules, given the purpose of this tutorial). Starting a new project, delete forms and controls, insert a Module using the Project pull-down menu, Add module, select the project properties at the end of the Project tab. In the property dialog choose as startup object Sub Main. In a new or existing process View code of the module (or click on the Code Window) and select Tools, Add a Procedure and add a procedure. You can also add a procedure by just typing in the module: Sub Procname, where Procname is the name of the procedure. Enter PUBLIC (global) variables in the Declarations of the module. Download an example of a simple project and module to display Hello World! Procedures can be of three types: (a) subroutine (does not return a value), (b) function (returns a value) and (c) property (returns and assigns values and set preferences to objects). Procedures are defined within modules, and when forms are not used (Web programming case) a Sub Main is required, as seen above.

Note: To exit a procedure use Exit . Exiting a Sub: Exit Sub , a Function: Exit Function.
 

File I/O Statements

There are three types of file access types in VB 6.0: (a) sequential, (b) random and (c) binary. The default access type is Random, that you should use whenever you read and write variables. Use sequential only to read and write lines. I will concentrate the file I/O explanation in this tutorial on the random file access.

In the random access type you need to define records, each with one or more fields (the same ones for all records). Then you open the file for random access, assigning a number to it and using the record layout you defined previously. Once a record is read you have access to all variables (fields) that comprise the record.

  1. Defining the records: use a user defined type (seen previously) to define the record. Lets use a simple example to illustrate it: the file Authorize comprised of usernames and passwords. in the Declarations section of the module we would create:

        Type Authorize
            uname As String * 30
            passwd As String * 30
        End Type

  2. Defining variables needed to open the file: again in the Declarations  section of the module we would define:

        Public Pos As Integer, Count As Integer, Fnum As Integer

        Where Pos would be the position of the record in the file (1 to the last).
                  Count would be the number of records in the file.
                  Fnum would be the file number.

    In a new procedure to read (open) the file we would also define:

        Dim Rlenght As Integer, Access As Authorize
               Rlenght = Len(Access)
               Fnum = FreeFile
     
        Where Rlenght would be the lenght of the record.
                   Access would be the file we want to open using the type Authorize.

         The record lenght can now be obtained using the Len ( ) function on the Access file.
         The file number (Fnum in the example) can be obtained using the function FreeFile.
             (FreeFile finds an available number for the file)

    (of course are many other ways of doing this).

  3. Opening the file: With all variables defined open the file as follows:

        Open "members1.dat" For Random As Fnum Len = Rlenght

        members1.dat is the name of the file, it could have been a full path name.
        I did not type For Random, VB 6.0 did it for me for it is the default.
     
    The Open syntax requires a fyle access type, a file number after the As keyword, and a record lenght after the Len = keyword.

  4. Processing the file: after the file is open we can read (Get) or write (Put) to the file. An example of reading the file follows:

        Count = LOF(Fnum) / Rlenght
     
        For Pos = 1 To Count

           Get Fnum, Pos, Access
           MsgBox Access.uname + "     " + Access.passwd

        Next

    Reading from a file requires the file number, position of the record and file name. An easy way to read all records is to obtain the leght of the file (using the LOF ( ) function) and divide it by the record lenght to know the number of records in the file. Then a loop can be used to read from record 1 to the last, as shown in the above example.

You can use the VB 6.0 Help to learn about the sequential and binary  access types. The sequential access is simpler than random, and binary is very similar.

See an example of a project opening, reading and adding records to the Authorize type of file for you to practice the file I/O concepts above. You can download the project  and the module  files here.

Creating an executable file

Last, but not least, after you debug your projects (don't forget to save them) you should select File and then Make EXE File to create an .exe file to run your application.


This page is maintained by Al Bento who can be reached at abento@ubalt.edu. This page was last updated on November 15, 2004. Although we will attempt to keep this information accurate, we can not guarantee the accuracy of the information provided.