UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> Message Engine    

This class handles Message Creation and Distribution. See the companion article Messaging System for conceptual usage information.


' Module    : clsMsgEngine
' Author    : Jack D. Leach
' Date      : 4/21/2011
' Purpose   : Provides an engine for Message creation and distribution

Option Compare Database
Option Explicit

'error constants
Const cERRN_MESSAGE_EXISTS = vbObjectError + 9006
Const cERRM_MESSAGE_EXISTS As String = "The message already exists"

Const cERRN_MESSAGE_DISTRIBUTED = vbObjectError + 9007
Const cERRM_MESSAGE_DISTRIBUTED As String = "The message has already been distributed"

Const cERRN_MESSAGE_REQUIRED_INFO = vbObjectError + 9008
Const cERRM_MESSAGE_REQUIRED_INFO As String = "The Subject and Message properties are required to create a message"

Const cERRN_MESSAGE_NOT_CREATED = vbObjectError + 9009
   "You cannot distribute this message as it has not yet been created"

' exposed property fields
Private m_Subject As String 'required
Private m_Message As String 'required
Private m_RefType As eMessageReferenceID 'required
Private m_RefID As Variant

'private fields
Private IsCreated As Boolean
Private IsDistributed As Boolean
Private msgID As Long

'exposed property definitions
Public Property Get Message() As String
 Message = m_Message
End Property
Public Property Let Message(s As String)
 m_Message = s
End Property

Public Property Get Subject() As String
 Subject = m_Subject
End Property
Public Property Let Subject(s As String)
 m_Subject = s
End Property

Public Property Get RefType() As eMessageReferenceID
 RefType = m_RefType
End Property
Public Property Let RefType(r As eMessageReferenceID)
 m_RefType = r
End Property

Public Property Get RefID() As Variant
 RefID = m_RefID
End Property
Public Property Let RefID(v As Variant)
 m_RefID = v
End Property

'exposed methods
Public Sub CreateMessage()

 'make sure they didn't call this twice
 If IsCreated Then
   Exit Sub
 End If
 'make sure there's a subject and message
 If Not IsCreatable Then
   Exit Sub
 End If
 'validated - create the message
 '(note that an Autonumber with @@Identity or a DMax solution
 'can be implemented here - there is no specific advantage to using
 'the Custom Autonumbers functionality in this case
 msgID = CLng(GetAutoNum(dsAutoNumMessage, True))
 CurrentDb.Execute _
   "INSERT INTO qryMessages (fldID, fldSubject, fldData, " & _
   "fldRefID, fldReference, fldDate) VALUES (" & _
   msgID & ", """ & Me.Subject & """, """ & Me.Message & """, " & _
   Me.RefType & ", " & IIf(IsNull(Me.RefID), "NULL, ", " """ & Me.RefID & """, ") & _
   "#" & Now() & "#)" _
   , dbFailOnError
 IsCreated = True
End Sub

Public Sub DistributeMessage()

 Dim sTargets As String 'users to distribute to

 'make sure they created a message
 If Not IsCreated Then
   Exit Sub
 End If
 'make sure they didn't call this twice
 If IsDistributed Then
   Exit Sub
 End If
 'validated - distribute the message
 CurrentDb.Execute _
   "INSERT INTO jtblUMessages (fldMessageID, fldUser, fldPriority) " & _
   "SELECT " & msgID & " AS cMsg, fldUser, fldPriority " & _
   "FROM tblUserSubscriptions " & _
   "WHERE (fldRefType = " & Me.RefType & ") AND (fldEnabled = -1)" _
   , dbFailOnError

 IsDistributed = True
End Sub

Private Function IsCreatable() As Boolean
 Dim ret As Boolean

 'init true, will flag false if missing info
 ret = True
 If Len(Me.Subject) = 0 Then ret = False
 If Len(Me.Message) = 0 Then ret = False
 If Me.RefType = msgrefNoReference Then ret = False
 IsCreatable = ret

End Function

Private Sub Class_Initialize()
 'set the RefID property to Null so we don't have to check
 'both IsEmpty and IsNull in the creation query
 Me.RefID = Null
End Sub

Creative Commons License
Message Engine by UtterAccess Wiki is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Editing and revision of the content is freely encouraged; for details, see Expected Usage.

Edit Discussion
Custom Search
Thank you for your support!
This page has been accessed 4,103 times.  This page was last modified 05:51, 23 April 2011 by Jack Leach.   Disclaimers