home *** CD-ROM | disk | FTP | other *** search
/ Mastering Web Site Development / Microsoft Mastering Web Site Development (Microsoft) (1997).iso / Labs / Lab09 / add.cls next >
Encoding:
Visual Basic class definition  |  1997-04-24  |  1.9 KB  |  65 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "Obj"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = True
  10. Const MAXCLASSES = 6
  11. Function OverEnrolled(conn As ADODB.Connection, lngStudentID As Long) As Boolean
  12.     Dim rsEnrollment As Object
  13.     Dim strSQL As String
  14.     
  15.     'Student cannot add if already at class limit
  16.     strSQL = "select count(studentid) from enrollment where studentid = " & lngStudentID
  17.     Set rsEnrollment = CreateObject("ADODB.recordset")
  18.     rsEnrollment.ActiveConnection = conn
  19.     rsEnrollment.Open strSQL, , adOpenKeyset
  20.     If rsEnrollment.Fields(0).Value > MAXCLASSES Then
  21.         OverEnrolled = True
  22.     Else
  23.         OverEnrolled = False
  24.     End If
  25.     rsEnrollment.Close
  26. End Function
  27. Public Function Add(ByVal lngStudentID As Long, ByVal strClassID As String) As Integer
  28.     Dim ctxObject As ObjectContext
  29.     Dim conn As ADODB.Connection
  30.     Dim strSQL As String
  31.     
  32.     On Error GoTo ErrorHandler
  33.     Add = 0
  34.     'Get Context Object
  35.     Set ctxObject = GetObjectContext
  36.     Set conn = CreateObject("ADODB.Connection")
  37.  
  38.     'Open connection with State University Database
  39.     conn.Open "DSN=StateU;UID=sa;PWD=;"
  40.  
  41.     'Business rule
  42.     'See if student is over enrolled
  43.     If OverEnrolled(conn, lngStudentID) Then
  44.         Add = 1
  45.         ctxObject.SetAbort
  46.         Exit Function
  47.     End If
  48.     
  49.     'Perform the work of adding the student to the class
  50.     strSQL = "INSERT INTO enrollment (ClassID, StudentID) VALUES ('" & strClassID & "'," & lngStudentID & ")"
  51.     conn.Execute strSQL
  52.         
  53.     'Close the connection
  54.     conn.Close
  55.     'Work completed successfully
  56.     ctxObject.SetComplete
  57.     Exit Function
  58.  
  59. ErrorHandler:
  60.     'Just abort if any errors occur
  61.     Add = 1
  62.     ctxObject.SetAbort
  63. End Function
  64.  
  65.