Private Sub cmdTranslate_Click()

    'This is an example program to show how to acknowledge and EDI file by
    'creating a Functional Acknowledgment 997 in Visual Basic using Framework EDI component
    
    Dim oEdiDoc As Fredi.ediDocument
    Dim oAck As Fredi.ediAcknowledgment
    Dim oSegment As Fredi.ediDataSegment
    Dim oIsa As Fredi.ediDataSegment
    Dim oSchemas As Fredi.ediSchemas
    Dim sSefFile As String
    Dim sEdiFile As String
    Dim sAckfile As String
    Dim sPath As String
    
    cmdTranslate.Enabled = False
    Me.MousePointer = vbHourglass
    
    sPath = App.Path & "\"
    
    sEdiFile = txtEDIfile.Text
    sSefFile = txtSEFfile.Text
    sAckfile = "997_" & Trim(sEdiFile)
    
    Set oEdiDoc = New Fredi.ediDocument
    
    'To support large EDI files without consuming much memory
    oEdiDoc.CursorType = Cursor_ForwardOnly
    
    'By default, FREDI uses the  universal coordinated time (UTC), however you can change it to local time
    oEdiDoc.Option(OptDocument_UseLocalTime) = 1
    
    'Stops FREDI from using the internal standard reference
    Set oSchemas = oEdiDoc.GetSchemas
    oSchemas.EnableStandardReference = False
    
    'Load the SEf file for EDI file
    If Len(Trim(sPath & sSefFile)) > 0 Then
        oEdiDoc.LoadSchema sPath & sSefFile, 0
    End If
    
    'Load SEF file for 997 EDI file
    oEdiDoc.LoadSchema sPath & "997_4010.SEF", 0
    
    Set oAck = oEdiDoc.GetAcknowledgment
    
    'Do not show segments AK2, AK3, AK5 if transaction set is accepted (AK501 = "A")
    oAck.Option(OptAcknowledgment_ShowReportingLevel) = 0
    
    
    'Set starting point of EDI Ack control numbers
    oAck.Property(PropertyAck_StartInterchangeControlNum) = 1010
    oAck.Property(PropertyAck_StartGroupControlNum) = 10
    oAck.Property(PropertyAck_StartTransactionSetControlNum) = 1
    
    'Property to include the TA1 segment in the acknowledgment
    oAck.EnableInterchangeAcknowledgment = True
    
    'Property that determines whether a 997 acknowledgment is created or not while loading the EDI file.
    oAck.EnableFunctionalAcknowledgment = True
    
    'Property to specify that the transaction set is accepted even with errors
'    oAck.AcceptSetWithErrors = True

    'Property to specify that the group set is accepted even with errors
'    oAck.AcceptGroupWithErrors = True

    'Errors that are not automatically included by FREDI in the 997 file can be added
    ' by mapping FREDI's error code number to the 997 error code.
    'In this example, errors caused by leading zeros in numeric data type are mapped to
    '997 error code 5 (Element too long)
    oAck.MapDataElementLevelError 13209, 5

    
    oEdiDoc.LoadEdi sPath & sEdiFile
    
    'When the cursor type is set to ForwardOnly, make sure to iterate through all segments to allow FREDI to validate them.
    Set oSegment = oEdiDoc.FirstDataSegment
    Do While Not oSegment Is Nothing
        Set oSegment = oSegment.Next
    Loop
    
    
    'Anything in the oAck object that was automtically generated can be changed before saving to file.
    'Changing the date and time in the ISA segment of the oAck
    
'    'Changing the TA1 segment
'    Set oSegment = oAck.GetFirstTA1DataSegment
'    Set oSegment = oSegment.GetDataSegmentByPos("\ISA\TA1")
'    oSegment.DataElementValue(4) = "R"

'    'Another method of changing the date and time in the GS segment of the oAck
'    Set oSegment = oAck.GetFirst997DataSegment
'    Set oSegment = oSegment.GetDataSegmentByPos("\ISA\GS\GS")
'    oSegment.DataElementValue(4) = "20010101"  ' Change date
'    oSegment.DataElementValue(5) = "110130"     ' Change time
    
    'Get last transaction set control number used in auto-generated acknowledgment
'    Dim sLastTsControlNo As String
'    sLastTsControlNo = oAck.Property(PropertyAck_EndingTransactionSetControlNum)
    
    'Writes the Acknowledgment to file
    oAck.Save sPath & sAckfile
    
    Me.MousePointer = vbNormal
    MsgBox "Done.  997 Ack file = " & sAckfile
    
    Set oEdiDoc = Nothing
    Set oAck = Nothing
    
End Sub