
'----------------------------------------------------------------
' Custom Project 27-1
' Part 2
' cRecordLogger Class Module
'----------------------------------------------------------------

' Enter this declaration at the top of the module just below the 
' Option Compare Database and Option Explicit statements

Private WithEvents m_frm As Access.Form

Public Property Set Form(cur_frm As Form)
    Set m_frm = cur_frm
    m_frm.AfterUpdate = "[Event Procedure]"
End Property


Private Sub m_frm_AfterUpdate()
    Dim fso As FileSystemObject
    Dim myFile As Object
    Dim strFileN As String
    Dim ctrl As Control
    
    On Error Resume Next
       
    Set fso = New FileSystemObject
    strFileN = "C:\Acc07_ByExample\MyCust.txt"
    Set myFile = fso.GetFile(strFileN)
    
    If Err.Number = 0 Then
        ' open text file
        Set myFile = fso.OpenTextFile(strFileN, 8)
    Else
        ' create a text file
        Set myFile = fso.CreateTextFile(strFileN)
    End If

    For Each ctrl In m_frm.Controls
        If ctrl.ControlType = acTextBox And _
            InStr(1, ctrl.Name, "ID") Then
             myFile.WriteLine "ID:" & ctrl.Value & _
                " Created on: " & Date & " " & Time & _
                " (Form: " & m_frm.Name & ")"
            MsgBox "See the audit trail in " & strFileN & "."
            Exit For
        End If
    Next

    myFile.Close
    Set fso = Nothing
End Sub


' --------------------------------------------------------------
' Custom Project 27-1
' Part 3
' frmCustomers form
' --------------------------------------------------------------


' Enter this declaration at the top of the module just below the 
' Option Compare Database and Option Explicit statements
Private clsRecordLogger As cRecordLogger

Private Sub Form_Open(Cancel As Integer)
    Set clsRecordLogger = New cRecordLogger
    Set clsRecordLogger.Form = Me
End Sub


Private Sub Form_Close()
    Set clsRecordLogger = Nothing
End Sub


'----------------------------------------------------------------
' Code on page 648
'----------------------------------------------------------------

Private Sub Form_AfterUpdate()
    MsgBox "Transferring control to the custom class."
    ' when you click OK to this message, the code
    ' inside the AfterUpdate procedure in the custom
    ' class module will run
End Sub


'----------------------------------------------------------------
' Hands-On 27-1 - UCaseBox Class Module
' Step 3
'----------------------------------------------------------------

'Enter this declaration statement at the top of the class module
Private WithEvents txtBox As Access.TextBox

Public Function InitializeMe(myTxt As TextBox)
    Set txtBox = myTxt
    txtBox.OnKeyPress = "[Event Procedure]"
End Function


Private Sub txtBox_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        Case 48 To 57
            MsgBox "Numbers are not allowed!"
            KeyAscii = 0
        Case Else
        ' convert to uppercase
            KeyAscii = Asc(UCase(Chr(KeyAscii)))
    End Select

    txtBox.FontBold = True
    txtBox.FontItalic = True
    txtBox.BackColor = vbYellow
End Sub


'----------------------------------------------------------------
' Hands-On 27-1 
' Steps 5-9 - Final code from Figure 27-9
'----------------------------------------------------------------

' module-level variable declaration
Private clsRecordLogger As cRecordLogger
Private clsTextBox1 As UCaseBox


Private Sub Form_Open(Cancel As Integer)
    Set clsRecordLogger2 = New cRecordLogger
    Set clsRecordLogger2.Form = Me
    
    Set clsTextBox1 = New UCaseBox
    clsTextBox1.InitializeMe Me.Controls("ProductName")
End Sub


Private Sub Form_Close()
    Set clsRecordLogger2 = Nothing
    Set clsTextBox1 = Nothing
End Sub


Private Sub Form_AfterUpdate()
    MsgBox "Transferring control to the custom class."
    ' when you click OK to this message, the code
    ' inside the AfterUpdate procedure in the custom
    ' class module will run
End Sub


' ---------------------------------------------------------------
' code on page 653
' ---------------------------------------------------------------

Public Sub Dispatch(ByVal toWhom As String, cancel As Boolean)
   RaiseEvent SendFlowers(toWhom, True)
End Sub


'----------------------------------------------------------------
' Hands-On 27-2 - cDispatch Class Module
'----------------------------------------------------------------

Public Event SendFlowers(ByVal strName As String, _
                    cancel As Boolean)

Sub Dispatch(ByVal ToWhom As String, cancel As Boolean)
    If ToWhom = "Julitta" Then
        cancel = True
        MsgBox "Dispatch to " & ToWhom & " was cancelled.", _
        vbInformation + vbOKOnly, "Reason Unknown"
    Else
        RaiseEvent SendFlowers(ToWhom, True)
    End If
End Sub


'----------------------------------------------------------------
' Hands-On 27-2 - steps 6 - 9 - frmFlowers form
'----------------------------------------------------------------

' Enter this declaration at the top of the class module
Private WithEvents clsDispatch As cDispatch


Private Sub Form_Load()
  Set clsDispatch = New cDispatch
End Sub


Private Sub Form_Close()
  Set clsDispatch = Nothing
End Sub


Private Sub cmdFlowers_Click()
    If Len(Me.Recipient) > 0 Then
        clsDispatch.Dispatch Me.Recipient, False
    Else
        MsgBox "Please specify the recipient name."
        Me.Recipient.SetFocus
        Exit Sub
    End If
End Sub


Private Sub clsDispatch_SendFlowers(ByVal strName As String, cancel As Boolean)
    MsgBox "Flowers will be sent to " & strName & ".", , "Order taken"
End Sub



