home *** CD-ROM | disk | FTP | other *** search
- ' The following constants define the location of the drive and directory
- ' where the Inscribe tutorial was copied. You should change these values
- ' if you copied the tutorial to a directory different from the one listed
- ' below.
- Const TutorialDrive$ = "C:"
- Const TutorialDirectory$ = "\INSCRIBE"
-
- ' -------------------------------------------------------------------------
- ' The SBL Letter() procedure is called to generate a confirmation or
- ' rejection letter for a student who has attempted to register for a class.
- ' A confirmation letter will be generated if the maximum enrollment for
- ' the class has not yet been reached. Otherwise, a rejection letter will
- ' be generated.
- '
- ' 1) Letter is called with arguments based on the following SSQL
- ' prototype:
- '
- ' CREATE PROCEDURE Letter
- ' (IN StudentId INTEGER(4),
- ' IN ClassName CHAR(60),
- ' IN Confirm CHAR(1)
- ' ); EXTERNAL
- '
- ' For example, a call might appear as:
- '
- ' Letter (123456789, -- Student ID
- ' "Accounting 101", -- Class Name
- ' "Y") -- Confirmation (Y = yes, N = no)
- '
- ' 2) Read a student's name and address from an address file, which is
- ' organized with Student ID, Student Name, Address1, Address2, and
- ' Address3 on each line with "%" as the separation character. The
- ' Student Id is used to locate the correct information for the
- ' student.
- '
- ' 3) Read each line of a text file representing the template for the
- ' letter.
- '
- ' 4) Replace strings in the template of the form %sn% with student name,
- ' %cn% with class name, %a1% with address1, etc.
- '
- ' 5) Write each line to an output file.
- ' -------------------------------------------------------------------------
-
-
- ' Forward Declarations
- Declare Function GetAddr&(StudentId&, StudentName$, Address1$, _
- Address2$, Address3$)
-
- Declare Sub Letter(StudentId&, ClassName$, Confirm$)
-
-
- ' -------------------------------------------------------------------------
- ' Main is not called from SSQL. It contains calls to Letter so Letter can
- ' be tested independently of SSQL, if desired.
- ' -------------------------------------------------------------------------
- Sub main
-
- Call Letter(123456789, "Film Making 101", "Y")
- Call Letter(888888888, "Physical Chemistry 200", "N")
- Call Letter(345678901, "General Studies 201", "Y")
-
- End Sub
-
-
- ' -------------------------------------------------------------------------
- ' Generate a confirmation or rejection letter for a student who has
- ' requested enrollment in a class. The address information is extracted
- ' from a student address file.
- ' -------------------------------------------------------------------------
- Sub Letter(StudentId&, ClassName$, Confirm$)
-
- Dim template_line$ ' A line read from the univtmpl.txt template file
- Dim output_line$ ' A processed template_line ready to be written
- Dim output_file$ ' The output file to which output_line is written
- Dim student_name$ ' The student's name
- Dim address1$ ' The first line of the student's address
- Dim address2$ ' The second line of the student's address
- Dim address3$ ' The third line of the student's address
- Dim field$ ' A field of text in template_line
- Dim field_num% ' The number of a field in template_line
- Dim separators$ ' Separators for the fields in template_line
- Dim newline ' The newline character
- Dim alldone% ' Set non-zero when we're done with univtmpl.txt
-
- ' Set the current drive and directory to the location of the template
- ' file and the generated letters.
- On Error GoTo WrapUp
- ChDrive TutorialDrive
- ChDir TutorialDirectory
-
- ' Extract the student address information from an address file
- If GetAddr(StudentId, student_name, address1, address2, address3) < 0 then
- MsgBox "Error: Unable to locate student id " & CStr(StudentId) & _
- " in the address file"
- Stop
- End If
-
- ' Construct the output_file pathname from the student's last name
- output_file = GetField$(student_name, 3, " ")
- If Len(output_file)=0 then
- output_file = GetField$(student_name, 2, " ")
- End If
- If Len(output_file) = 0 then
- output_file = GetField$(student_name, 1, " ")
- End If
- output_file = Left$(output_file, 8) & ".txt"
-
- ' Perform initializations to open files and define variables used below
- Open "univtmpl.txt" For Input As #1
- Open output_file For Output As #2
- alldone = 0
- newline = Chr(10)
- separators = "%" & newline
-
- ' Read each line of the template file and substitute the appropriate
- ' values for fields of the form %sn%, %cn%, %a1%, etc.
- Do Until alldone > 0
-
- ' Read the next template line and set alldone if it's the last line
- Line Input #1, template_line
- If Seek(1) > Lof(1) then
- alldone = 1
- End If
-
- ' Scan the line from the template and process pairs of fields separated
- ' by the characters in 'separators'. We perform substitution on the
- ' second field of the pair, not the first field. For example, the line
- ' "Dear %cn%:" will process the fields "Dear " and "cn" on the the
- ' first iteration and process ":" and Null on the second iteration.
- field_num = 1
- output_line = ""
- Do
- ' Get an odd-numbered field -- this is a text field where we don't
- ' look for substitution characters. Note: this field may be Null,
- ' but we don't exit since non-Null fields may follow this field.
- field = GetField$(template_line, field_num, separators)
- output_line = output_line & field
-
- ' Get an even-numbered field -- this is a text field where we look
- ' for substitution characters. Note that if this field is Null, we
- ' exit since no more fields will follow.
- field = GetField$(template_line, field_num+1, separators)
- If Len(field) = 0 then
- Exit Do
- End If
-
- ' Check the 'field' value to determine which string needs to be
- ' placed in 'output_line'
- If StrComp(field, "sn", 1) = 0 then
- ' Student name
- output_line = output_line & student_name
-
- ElseIf StrComp(field, "a1", 1) = 0 then
- ' Address Line 1
- output_line = output_line & address1
-
- ElseIf StrComp(field, "a2", 1) = 0 then
- ' Address Line 2
- output_line = output_line & address2
-
- ElseIf StrComp(field, "a3", 1) = 0 then
- ' Address Line 3
- output_line = output_line & address3
-
- ElseIf StrComp(field, "cn", 1) = 0 then
- ' Class Name
- output_line = output_line & ClassName
-
- ElseIf StrComp(field, "da", 1) = 0 then
- ' Date - Output date with the day of the week stripped off
- field = Format$(Date$, "Long Date")
- output_line = output_line & GetField$(field, 2, " ") & " " & _
- GetField$(field, 3, " ") & " " & _
- GetField$(field, 4, " ")
-
- ElseIf StrComp(field, "co", 1) = 0 then
- ' Confirmation
- If StrComp(Confirm, "Y", 1) = 0 then
- output_line = output_line & "has been confirmed"
- Else
- output_line = output_line & _
- "has been denied because the class " & _
- "has reached its maximum size"
- End If
-
- Else
- ' Unrecognized field name so generate a string of asterisks
- output_line = output_line & "********"
- End If
-
- field_num = field_num + 2
-
- Loop
-
- Print #2, output_line
-
- Loop
-
- Close #1
- Close #2
-
- Done:
- Exit Sub
-
- WrapUp:
- field = "Error " & Err & " at line " & Erl & ": " & Error$
- MsgBox field
- Resume Done
-
- End Sub
-
-
- ' --------------------------------------------------------------------------
- ' Read an address file to get the address of the specified student.
- ' --------------------------------------------------------------------------
- Function GetAddr&(StudentId&, StudentName$, Address1$, Address2$, Address3$)
-
- Dim address_line$ ' A line read from the address.txt file
- Dim field$ ' A field of text in address_line
- Dim separators$ ' Separators for the fields in address_line
- Dim newline ' The newline character
- Dim alldone% ' Set non-zero when we're done with address.txt
- Dim retcode& ' Return code
-
- On Error GoTo WrapUp
-
- Open "address.txt" For Input As #1
- GetAddr = 0
- alldone = 0
- newline = Chr(10)
- separators = "%" & newline
-
- ' Read each line of the address file until the StudentId is located
- Do Until alldone > 0
-
- ' Read the next address line
- Line Input #1, address_line
-
- ' Check the line just read to see if the student id is present in the
- ' first field of the record.
- field = GetField$(address_line, 1, separators)
-
- If Val(field) = StudentId then
- ' Found the student so extract the name and address information
- ' from the remaining fields and then exit the loop.
- StudentName = GetField$(address_line, 2, separators)
- Address1 = GetField$(address_line, 3, separators)
- Address2 = GetField$(address_line, 4, separators)
- Address3 = GetField$(address_line, 5, separators)
- Exit Do
- End If
-
- ' Set alldone if it's the last line
- If Seek(1) > Lof(1) then
- alldone = 1
- End If
-
- Loop
-
- Close #1
- If alldone > 0 then
- ' We scanned the entire address file and didn't find the student id.
- GetAddr = -1
- End If
-
- Done:
- Exit Function
-
- WrapUp:
- field = "Address file error " & Err & " at line " & Erl & ": " & Error$
- MsgBox field
- GetAddr = -1
- Resume Done
-
- End Function
-
-