GENERAL PROCEDURES  Often you will encounter programming situations in which multiple procedures perform the same operation. This condition can occur when the user can select either a command button or a menu option to do the same thing. Rather than retyping the code, you can write reusable code in a general procedure and call it from both event procedures.
 General procedures include subroutines, also called sub procedures, and functions.
GENERAL PROCEDURES The difference between these two types of procedures is:
A sub procedure is a procedure that performs actions. A function procedure may perform an action, but it also returns a value to the point from which it was called.
GENERAL PROCEDURES  Sub Procedures A Sub procedure is a series of Visual Basic statements enclosed by the Sub and End Sub statements. Each time the procedure is called, its statements are executed, starting with the first executable statement after the Sub statement and ending with the first End Sub, Exit Sub, or Return statement encountered.
GENERAL PROCEDURES The syntax for declaring a Sub procedure is as follows:
Public Sub subname[(argumentlist)] ' Statements of the Sub procedure go here. End Sub
The syntax for a call a Sub procedure is as follows: Call subname[(argumentlist)]
GENERAL PROCEDURES  Functions A Function procedure is a series of Visual Basic statements enclosed by the Function and End Function statements. Each time the procedure is called, its statements are executed, starting with the first executable statement after the Function statement and ending with the first End Function, Exit Function, or Return statement encountered.
GENERAL PROCEDURES The syntax for declaring a Function procedure is as follows:
Public Function functionname[(argumentlist)] As datatype ' Statements of the Function procedure. End Function
The syntax for a call a Function is as follows: Value = functionname[(argumentlist)]
WORKING WITH FILES  Until the last lesson, we are only creating programs that could accept data at runtime, when a program is terminated, the data also disappear. Is it possible to save data accepted by a VB program into a storage device, such as a hard disk or diskette, or even CDRW? The answer is possible. Is this chapter, we will learn how to create files by writing them into a storage device and then retrieve the data by reading the contents of the files using customized VB programs.
WORKING WITH FILES  CREATING FILES To create a file , use the following syntax Open "fileName" For Output As #fileNumber Each file created must have a file name and a file number for identification. As for file name, you must also specify the path where the file will reside.
WORKING WITH FILES  For example Open "c:\My Output As #1
Documents\sample.txt"
For
will create a text file by the name of sample.txt in the My Document folder. The accompany file number is 1. If you wish to create and save the file in A drive, simply change the path, as follows: Open "A:\sample.txt" For Output As #1
WORKING WITH FILES  Sample program: Creating Files Private Sub Command1_Click() Dim a(100) As Double Open "out.txt" For Output As #1 n=0 For n = 0 To 9 a(n) = n + 1 Print #1, a(n) List1.AddItem a(n) Next Close #1 End Sub
WORKING WITH FILES  READING FILES To read a file, you can use the input # statement. However, we can only read the file according to the format when it was written. You have to open the file according to its file number and the variable that hold the data. We also need to declare the variable using the DIM command.
WORKING WITH FILES  Sample Program: Reading file Private Sub Reading Click() Dim variable1 As String Open "c:\My Documents\sample.txt" For Input As #1 Input #1, variable1 Text1.Text = variable1 Close #1