Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click

        Dim oEdiDoc As ediDocument
        Dim oSchema As ediSchema
        Dim oSchemas As ediSchemas
        Dim oInterchange As ediInterchange
        Dim oTransactionset As ediTransactionSet
        Dim oSegment As ediDataSegment
        Dim sPath As String

        sPath = AppDomain.CurrentDomain.BaseDirectory

        'CREATES OEDIDOC OBJECT
        ediDocument.Set(oEdiDoc, New ediDocument)

        'THIS MAKES CERTAIN THAT FREDI ONLY USES THE SEF FILE PROVIDED, AND THAT IT DOES 
        'NOT USE ITS BUILT-IN STANDARD REFERENCE TABLE TO GENERATE THE EDI FILE.
        ediSchemas.Set(oSchemas, oEdiDoc.GetSchemas)
        oSchemas.EnableStandardReference = False

        'ENABLES FORWARD WRITE, AND INCREASES BUFFER I/O TO IMPROVE PERFORMANCE
        oEdiDoc.CursorType = DocumentCursorTypeConstants.Cursor_ForwardWrite
        oEdiDoc.Property(DocumentPropertyIDConstants.Property_DocumentBufferIO) = 2000

        'LOADS THE SEF FILE
        ediSchema.Set(oSchema, oEdiDoc.LoadSchema(sPath & "DESADV_D97A.EVAL0.SEF", 0))

        'SET TERMINATORS
        oEdiDoc.SegmentTerminator = "'"
        oEdiDoc.ElementTerminator = "+"
        oEdiDoc.CompositeTerminator = ":"
        oEdiDoc.ReleaseIndicator = "?"

        'CREATES THE UNB SEGMENT
        ediInterchange.Set(oInterchange, oEdiDoc.CreateInterchange("UN", "D97A"))
        ediDataSegment.Set(oSegment, oInterchange.GetDataSegmentHeader)
        oSegment.DataElementValue(1, 1) = "UNOA"    'Syntax identifier
        oSegment.DataElementValue(1, 2) = "2"    'Syntax version number
        oSegment.DataElementValue(2) = "003456789"     'Interchange sender identification
        oSegment.DataElementValue(3) = "112836044"     'Recipient identification
        oSegment.DataElementValue(4, 1) = "205030"    'Date
        oSegment.DataElementValue(4, 2) = "1600"    'Time
        oSegment.DataElementValue(5) = "1234"     'Interchange control reference
        oSegment.DataElementValue(11) = "1"     'Test file

        'CREATES THE UNH SEGMENT
        ediTransactionSet.Set(oTransactionset, oInterchange.CreateTransactionSet("DESADV"))
        ediDataSegment.Set(oSegment, oTransactionset.GetDataSegmentHeader)
        oSegment.DataElementValue(1) = "1"     'Message reference number
        oSegment.DataElementValue(2, 1) = "DESADV"    'Message type identifier
        oSegment.DataElementValue(2, 2) = "D"    'Message version number
        oSegment.DataElementValue(2, 3) = "97A"    'Message release number
        oSegment.DataElementValue(2, 4) = "UN"    'Controlling agency

        'BGM - BEGINNING OF MESSAGE
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("BGM"))
        oSegment.DataElementValue(2, 1) = "0011836"    'Document/message number
        oSegment.DataElementValue(3) = "9"     'Message function, coded

        'DTM - DATE/TIME/PERIOD
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("DTM"))
        oSegment.DataElementValue(1, 1) = "137"    'Date/time/period qualifier
        oSegment.DataElementValue(1, 2) = "200503051500"    'Date/time/period
        oSegment.DataElementValue(1, 3) = "203"    'Date/time/period format qualifier

        'DTM - DATE/TIME/PERIOD
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("DTM(2)"))
        oSegment.DataElementValue(1, 1) = "11"    'Date/time/period qualifier
        oSegment.DataElementValue(1, 2) = "200503051400"    'Date/time/period
        oSegment.DataElementValue(1, 3) = "203"    'Date/time/period format qualifier

        'MEA - MEASUREMENTS
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("MEA"))
        oSegment.DataElementValue(1) = "AAE"     'Measurement purpose qualifier
        oSegment.DataElementValue(2, 1) = "SQ"    'Property measured, coded
        oSegment.DataElementValue(3, 1) = "C62"    'Measure unit qualifier
        oSegment.DataElementValue(4) = "1S"     'Surface/layer indicator, coded

        'NAD - NAME AND ADDRESS
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("NAD\NAD"))
        oSegment.DataElementValue(1) = "ST"     'Party qualifier
        oSegment.DataElementValue(2, 1) = "0452"    'Party identification
        oSegment.DataElementValue(2, 3) = "92"    'Code list responsible agency, coded

        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("NAD\LOC"))
        oSegment.DataElementValue(1) = "18"     'Place/Location qualifier
        oSegment.DataElementValue(2, 1) = "WAREHOUSE A"    'Place/Location identification

        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("NAD\CTA\CTA"))
        oSegment.DataElementValue(1) = "AC"
        oSegment.DataElementValue(2, 2) = "JOHN SMITH"

        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("NAD\CTA\COM"))
        oSegment.DataElementValue(1, 1) = "555-5555"
        oSegment.DataElementValue(1, 2) = "TE"

        'NAD - NAME AND ADDRESS
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("NAD(2)\NAD"))
        oSegment.DataElementValue(1) = "SU"     'Party qualifier
        oSegment.DataElementValue(2, 1) = "003456789"    'Party identification
        oSegment.DataElementValue(2, 3) = "16"    'Code list responsible agency, coded

        'TDT - TRANSPORT INFORMATION
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("TDT\TDT"))
        oSegment.DataElementValue(1) = "12"     'Transport stage qualifier
        oSegment.DataElementValue(3, 1) = " M"    'Mode of transport, coded
        oSegment.DataElementValue(5, 1) = "RYDD"    'Carrier identification
        oSegment.DataElementValue(5, 3) = "92"    'Code list responsible agency, coded

        'EQD - EQUIPMENT DETAILS
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("EQD\EQD"))
        oSegment.DataElementValue(1) = "TE"     'Equipment qualifier
        oSegment.DataElementValue(2, 1) = "ABC001998"    'Equipment identification number

        'CPS - CONSIGNMENT PACKING SEQUENCE
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("CPS\CPS"))
        oSegment.DataElementValue(1) = "1"     'Hierarchical id. number
        oSegment.DataElementValue(3) = "1"     'Packaging level, coded

        'LIN - LINE ITEM
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("CPS\LIN\LIN"))
        oSegment.DataElementValue(3, 1) = "12345678"    'Item number
        oSegment.DataElementValue(3, 2) = "IN"    'Item number type, coded

        'PIA - ADDITIONAL PRODUCT ID
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("CPS\LIN\PIA"))
        oSegment.DataElementValue(1) = "1"     'Product id. function qualifier
        oSegment.DataElementValue(2, 1) = "B"    'Item number
        oSegment.DataElementValue(2, 2) = "EC"    'Item number type, coded

        'QTY - QUANTITY
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("CPS\LIN\QTY"))
        oSegment.DataElementValue(1, 1) = "12"    'Quantity qualifier
        oSegment.DataElementValue(1, 2) = "18997"    'Quantity
        oSegment.DataElementValue(1, 3) = "C62"    'Measure unit qualifier

        'RFF - REFERENCE
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("CPS\LIN\RFF\RFF"))
        oSegment.DataElementValue(1, 1) = "ON"    'Reference qualifier
        oSegment.DataElementValue(1, 2) = "5500001122"    'Reference number
        oSegment.DataElementValue(1, 3) = "00010"    'Line number

        'RFF - REFERENCE
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("CPS\LIN\RFF(2)\RFF"))
        oSegment.DataElementValue(1, 1) = "AAN"    'Reference qualifier
        oSegment.DataElementValue(1, 2) = "21"    'Reference number
        oSegment.DataElementValue(1, 3) = "C62"    'Line number

        'CPS - CONSIGNMENT PACKING SEQUENCE
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("CPS(2)\CPS"))
        oSegment.DataElementValue(1) = "2"     'Hierarchical id. number
        oSegment.DataElementValue(3) = "1"     'Packaging level, coded

        'LIN - LINE ITEM
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("CPS(2)\LIN\LIN"))
        oSegment.DataElementValue(3, 1) = "23456789"    'Item number
        oSegment.DataElementValue(3, 2) = "IN"    'Item number type, coded

        'PIA - ADDITIONAL PRODUCT ID
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("CPS(2)\LIN\PIA"))
        oSegment.DataElementValue(1) = "1"     'Product id. function qualifier
        oSegment.DataElementValue(2, 1) = "C"    'Item number
        oSegment.DataElementValue(2, 2) = "EC"    'Item number type, coded

        'QTY - QUANTITY
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("CPS(2)\LIN\QTY"))
        oSegment.DataElementValue(1, 1) = "12"    'Quantity qualifier
        oSegment.DataElementValue(1, 2) = "18997"    'Quantity
        oSegment.DataElementValue(1, 3) = "C62"    'Measure unit qualifier

        'RFF - REFERENCE
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("CPS(2)\LIN\RFF\RFF"))
        oSegment.DataElementValue(1, 1) = "ON"    'Reference qualifier
        oSegment.DataElementValue(1, 2) = "5500001122"    'Reference number
        oSegment.DataElementValue(1, 3) = "00020"    'Line number

        'RFF - REFERENCE
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("CPS(2)\LIN\RFF(2)\RFF"))
        oSegment.DataElementValue(1, 1) = "AAN"    'Reference qualifier
        oSegment.DataElementValue(1, 2) = "21"    'Reference number
        oSegment.DataElementValue(1, 3) = "C62"    'Line number

        'TRAILING SEGMENTS ARE AUTOMATICALLY CREATED WHEN FREDI COMMITS (SAVES) THE EDIDOC OBJECT INTO A FILE.
        oEdiDoc.Save(sPath & "desadv.txt")

        'display edi string
        MessageBox.Show(oEdiDoc.GetEdiString, "DESADV D97A EDI string")

        'DESTROYS OBJECTS
        oSegment.Dispose()
        oTransactionset.Dispose()
        oSchema.Dispose()
        oInterchange.Dispose()
        oEdiDoc.Dispose()


    End Sub
    

    Click here to download a trial version of the Framework EDI