Procedure for Listening for Events in Visual Basic |
Take the following steps to create a program that listens for event notifications from FREDI.
Example in Visual Basic
This example will use the following:
In this example, the program is created to listen for event notifications returned from FREDI ActiveX/COM when it reads an EDI file. The visual basic program contains a simple form having a command button to start the program execution, and a text control to display the event notifications.
Declare ediDocument object with WithEvents. Declare ediDocument as object that is an event source. The WithEvents keyword is used in the declaration as follows:
' Declare FREDI as event source.
Private WithEvents m_oEdiDoc As Fredi.ediDocument
Create an instance of ediDocument object, and apply environment settings. The ediDocument object is the top level application instance for Framework EDI (FREDI). This object is the topmost object in the object model hierarchy of FREDI. This instance is created at the top of the form and the variable is a class member of the form.
Dim oSchemas As Fredi.ediSchemas
Dim oSchema As Fredi.ediSchema' Instance of FREDI as ediDocument.
Set m_oEdiDoc = new Fredi.ediDocument' To traverse all the data segments in the document at the best possible speed, enable Cursor_ForwardOnly.
m_oEdiDoc.CursorType = Cursor_ForwardOnly' Disable standard reference library.
Set oSchemas = m_oEdiDoc.GetSchemas
oSchemas.EnableStandardReference = False
' Load implementation guideline used by the EDI document.
Set oSchema = m_oEdiDoc.LoadSchema(App.Path & "\SampleSefX12_850.SEF", Schema_Standard_Exchange_Format)
Load EDI document. Load the EDI document to open and begin file traversal. When loaded successfully, traverse the file by stepping to each data segments in the EDI document.
' Load EDI document via the mailDocument object.
m_oEdiDoc.Load App.Path & "\SampleEdiX12_850.AS2.TXT"
' The EDI data is now made available in the ediDocument object.
Dim oSegment As Fredi.ediDataSegment' Traverse all the data segments in the document.
Set oSegment = m_oEdiDoc.FirstDataSegment
While Not oSegment Is NothingWend' Do something ...
Set oSegment = m_oEdiDoc.NextDataSegmentMsgBox "Done"
Add event handler. By declaring the ediDocument as an event source using WithEvents, the variable m_oEdiDoc variable will appear on the drop-down list to the left of the editor.
Select the m_oEdiDoc variable and the drop-down list to the right of the editor will display all the events available to the ediDocument class.
Select the EventNotify event and the m_oEdiDoc_EventNotify event procedure is automatically added to the form. Add the following code to display the events in the text control Text1.
Private Sub m_oEdiDoc_EventNotify(ByVal Severity As Fredi.EventIDConstants, ByVal ErrorCode As Long, ByVal ErrorDescription As String)
If Severity = Event_Info_InterchangeStart ThenForm1.Text1 = Form1.Text1 & "Start of Interchange. Notification: " & CStr(ErrorDescription) & vbCrLfElseIf Severity = Event_Info_InterchangeEnd Then
Form1.Text1.SelStart = Len(Form1.Text1)
DoEventsForm1.Text1 = Form1.Text1 & "End of Interchange. Notification: " & CStr(ErrorDescription) & vbCrLfElseIf Severity = Event_Info_FunctionalGroupStart Then
Form1.Text1.SelStart = Len(Form1.Text1)
DoEventsForm1.Text1 = Form1.Text1 & "Start of Functional Group. Notification: " & CStr(ErrorDescription) & vbCrLfElseIf Severity = Event_Info_FunctionalGroupEnd Then
Form1.Text1.SelStart = Len(Form1.Text1)
DoEventsForm1.Text1 = Form1.Text1 & "End of Functional Group. Notification: " & CStr(ErrorDescription) & vbCrLfElseIf Severity = Event_Info_TransactionSetStart Then
Form1.Text1.SelStart = Len(Form1.Text1)
DoEventsForm1.Text1 = Form1.Text1 & "Start of Transaction Set/Message. Notification: " & CStr(ErrorDescription) & vbCrLfElseIf Severity = Event_Info_TransactionSetEnd Then
Form1.Text1.SelStart = Len(Form1.Text1)
DoEventsForm1.Text1 = Form1.Text1 & "End of Transaction Set/Message. Notification: " & CStr(ErrorDescription) & vbCrLfElseIf Severity = Event_Notify Then
Form1.Text1.SelStart = Len(Form1.Text1)
DoEvents' Any general notifications sent by FREDI is captured here.ElseIf Severity = Event_Warning Then
Form1.Text1 = Form1.Text1 & "Code: " & CStr(ErrorCode) & ", Notification: " & CStr(ErrorDescription) & vbCrLf
Form1.Text1.SelStart = Len(Form1.Text1)
DoEvents' Any warnings sent by FREDI is captured here.ElseIf Severity = Event_Severe Then
Form1.Text1 = Form1.Text1 & "Warning Code: " & CStr(ErrorCode) & ", Warning Description: " & ErrorDescription & vbCrLf
Form1.Text1.SelStart = Len(Form1.Text1)
DoEvents' Any severe errors sent by FREDI is captured here.End If
Form1.Text1 = Form1.Text1 & "Severe Code: " & CStr(ErrorCode) & ", Severe Description: " & ErrorDescription & vbCrLf
Form1.Text1.SelStart = Len(Form1.Text1)
DoEvents
End Sub