HTML Applications Tutorial #7
When dealing with small programs, it is quite easy to see and understand it's structure. However, as programs get bigger, it becomes more difficult to update so a more modular approach is required. This is where subroutines come into their own. They enable the programmer to break up a program into more manageable chunks. This also have the advantage that they can be called anywhere in the program and indeed more than once. Here is an example of a routine that can be called when the program is first loaded and at other times, usually in response to a menu selection (the menu's have been omitted for the sake of clarity). So as usual, the program looks like this and must be cut and pasted into your template file.
' main program starts
here
dim Filename
Filename="c:\testfile.txt"
Get_Filename
' main program ends here
' sub starts here
sub Get_Filename()
dim TheInput
dim i
Dim msg
TheInput = InputBox("Enter a Filename", "Filename")
If TheInput = "" then
TheInput = Filename
end if
msg = Filename & " entered"
msgbox msg
end sub
' sub ends here
It is imporant to realise that up until now, your programs could be written without any real structure. Now it becomes important to split your programs into the main part and all the subroutines below it. I have shown this structure using comments which can be omitted but for the moment it might be best to keep them in until you get used to this new way of doing things. I'll go through it a line at a time.
' main program starts
here
dim Filename
The comment reminds us this is the start of the main
part of the program. The variable Filename is
defined here and it is important to realise that this is a global
variable. This kind of variable cab used both by the main program
and ALL of the subroutines defined in the
program. This is important. It will hold a value that can be
changed by any routine in the program and it will hold that value
until either the program is restarted or until something changes
it. This is useful for filenames as we want to be able to access
it from anywhere in the program.
Filename="c:\testfile.txt"
The global variable is now given a filename.
Get_Filename
' main program ends here
The subroutine is then called and we note the end of the main part of the program. This is where execution of the program also ends. However, when the subroutine is called, execution jumps down to the next line of code.
' sub starts here
sub Get_Filename()
After the comment, the name must
be preceded by the keyword sub (to denote a subroutine)
and suffixed by brackets as shown.
dim TheInput
dim i
Dim msg
Some more variables here. These are local variables
because they will only survive for the duration of the subroutine.
If it is called again, any changes to them will have been erased.
TheInput = InputBox("Enter
a Filename", "Filename")
If TheInput = "" then
TheInput = Filename
end if
The input box is used to get the new filename. If no
filename is entered then the local variable is assigned the
contents of the global variable above. If a
filename is entered then the global variable is assigned the
contents of the input box.
msg = Filename &
" entered"
msgbox msg
end sub
' sub ends here
The local variable msg is assigned the filename and displays it in a messagebox. The subroutine ends. You can try running this, first without entering anything in the messagebox and then entering a name and see what happens. See also what happens when the program is restarted.
One slight problem with the above program is that there are no checks to see if what is entered is a valid filename. The slighly modified program below rectifies this. Again, test it and see what it does.
dim Filename
Filename="c:\testfile.txt"
Get_Filename
sub Get_Filename()
dim TheInput
dim i
Dim fso, msg
TheInput = InputBox("Enter a Filename", "Filename")
If TheInput = "" then
TheInput = Filename
end if
if InStr(1, TheInput, ".") = 0 then
Filename = TheInput & ".txt"
else
Filename = TheInput
end if
msg = Filename & " entered"
msgbox msg
end sub
The code shown in bold has been added to the subroutine and
breaks down as follows.
if InStr(1, TheInput, ".") = 0 then
The Instr command will serach from the first charater (denoted by the 1) of the string contained in TheInput variable and searches for a dot (enclosed in the quotes).
Filename = TheInput
& ".txt"
If does NOT find it (if it finds a dot
then the result would be greater than zero) then it appends a .txt
to the end of the filename
else
Filename = TheInput
end if
otherwise take it just the way it is either because a filename wasn't entered by the user (so it remains with a .txt suffix) or the user added a suffix (which could be anything and might still be wrong so more error checking will be required at some point but it has been omitted here for the sake of clarity). And that's all there is to know about subroutines, for the moment at least. Until the next time we meet...