Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

        'This is just an example to show how to use the Framework EDI component in VB .NET to generate an 856 EDI file.

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

        Me.Cursor = Cursors.WaitCursor

        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

        'SET TERMINATORS
        oEdiDoc.SegmentTerminator = "~{13:10}"
        oEdiDoc.ElementTerminator = "*"
        oEdiDoc.CompositeTerminator = ">"

        'LOADS THE SEF FILE
        ediSchema.Set(oSchema, oEdiDoc.ImportSchema(sPath & "856_X12-4010.SEF", 0))

        'CREATES THE ISA SEGMENT
        ediInterchange.Set(oInterchange, oEdiDoc.CreateInterchange("X", "004010"))
        ediDataSegment.Set(oSegment, oInterchange.GetDataSegmentHeader)
        oSegment.DataElementValue(1) = "00"     'Authorization Information Qualifier
        oSegment.DataElementValue(2) = "          "     'Authorization Information
        oSegment.DataElementValue(3) = "00"     'Security Information Qualifier
        oSegment.DataElementValue(4) = "          "     'Security Information
        oSegment.DataElementValue(5) = "14"     'Interchange ID Qualifier
        oSegment.DataElementValue(6) = "0073268795005  "     'Interchange Sender ID
        oSegment.DataElementValue(7) = "ZZ"     'Interchange ID Qualifier
        oSegment.DataElementValue(8) = "RECEIVERISA    "     'Interchange Receiver ID
        oSegment.DataElementValue(9) = "960807"     'Interchange Date
        oSegment.DataElementValue(10) = "1548"     'Interchange Time
        oSegment.DataElementValue(11) = "U"     'Interchange Control Standards Identifier
        oSegment.DataElementValue(12) = "00401"     'Interchange Control Version Number
        oSegment.DataElementValue(13) = "000000020"     'Interchange Control Number
        oSegment.DataElementValue(14) = "0"     'Acknowledgment Requested
        oSegment.DataElementValue(15) = "T"     'Usage Indicator
        oSegment.DataElementValue(16) = ">"     'Component Element Separator

        'CREATES THE GS SEGMENT
        ediGroup.Set(oGroup, oInterchange.CreateGroup("004010"))
        ediDataSegment.Set(oSegment, oGroup.GetDataSegmentHeader)
        oSegment.DataElementValue(1) = "SH"     'Functional Identifier Code
        oSegment.DataElementValue(2) = "007326879"     'Application Sender's Code
        oSegment.DataElementValue(3) = "RECEIVERGS"     'Application Receiver's Code
        oSegment.DataElementValue(4) = "19960807"     'Date
        oSegment.DataElementValue(5) = "1548"     'Time
        oSegment.DataElementValue(6) = "1"     'Group Control Number
        oSegment.DataElementValue(7) = "X"     'Responsible Agency Code
        oSegment.DataElementValue(8) = "004010"     'Version / Release / Industry Identifier Code

        'CREATES THE ST SEGMENT
        ediTransactionSet.Set(oTransactionset, oGroup.CreateTransactionSet("856"))
        ediDataSegment.Set(oSegment, oTransactionset.GetDataSegmentHeader)
        oSegment.DataElementValue(1) = "856"     'Transaction Set Identifier Code
        oSegment.DataElementValue(2) = "00001"     'Transaction Set Control Number

        'BSN - BEGINNING SEGMENT FOR SHIP NOTICE
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("BSN"))
        oSegment.DataElementValue(1) = "00"      'Transaction Set Purpose Code
        oSegment.DataElementValue(2) = "3333"      'Shipment Identification
        oSegment.DataElementValue(3) = "20020301"      'Date
        oSegment.DataElementValue(4) = "1340"      'Time
        oSegment.DataElementValue(5) = "0002"      'Hierarchical Structure Code

        'hypothetical numbers of shipments, orders and items
        Dim nShipmentCounter As Integer = 1
        Dim nShipments As Integer = 1
        Dim nOrderCounter As Integer = 1
        Dim nOrders As Integer = 1
        Dim nItemCounter As Integer = 1
        Dim nItems As Integer = 6

        Dim nHlCounter As Integer
        Dim nHlShipmentParent As Integer
        Dim nHlOrderParent As Integer
        Dim nHlItemParent As Integer

        '********************************************************************************************
        'HL - HIERARCHICAL LEVEL - SHIPMENTS ********************************************************
        Do While nShipmentCounter <= nShipments

            nHlCounter = nHlCounter + 1
            nHlOrderParent = nHlCounter

            ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\HL"))
            oSegment.DataElementValue(1) = nHlCounter      'Hierarchical ID Number
            oSegment.DataElementValue(3) = "S"      'Hierarchical Level Code
            oSegment.DataElementValue(4) = "1"      'Hierarchical Child Code

            'TD1 - CARRIER DETAILS (QUANTITY AND WEIGHT)
            ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\TD1"))
            oSegment.DataElementValue(1) = "TKT"      'Packaging Code
            oSegment.DataElementValue(2) = "207"      'Lading Quantity
            oSegment.DataElementValue(6) = "A3"      'Weight Qualifier
            oSegment.DataElementValue(7) = "46.09"      'Weight
            oSegment.DataElementValue(8) = "01"      'Unit or Basis for Measurement Code

            'TD5 - CARRIER DETAILS (ROUTING SEQUENCE/TRANSIT TIME)
            ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\TD5"))
            oSegment.DataElementValue(2) = "2"      'Identification Code Qualifier
            oSegment.DataElementValue(3) = "IDCODE"      'Identification Code
            oSegment.DataElementValue(4) = "M"      'Transportation Method/Type Code
            oSegment.DataElementValue(5) = "FREEFORM"      'Routing

            'TD3 - CARRIER DETAILS (EQUIPMENT)
            ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\TD3"))
            oSegment.DataElementValue(1) = "TN"      'Equipment Description Code
            oSegment.DataElementValue(2) = "BPL3"      'Equipment Initial
            oSegment.DataElementValue(3) = "777777"      'Equipment Number

            'REF - REFERENCE IDENTIFICATION
            ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\REF"))
            oSegment.DataElementValue(1) = "BM"      'Reference Identification Qualifier
            oSegment.DataElementValue(2) = "33333"      'Reference Identification

            'DTM - DATE/TIME REFERENCE - SHIPPED DATE
            ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\DTM"))
            oSegment.DataElementValue(1) = "011"      'Date/Time Qualifier 
            oSegment.DataElementValue(2) = "19971211"      'Date

            'DTM - DATE/TIME REFERENCE - ESTIMATED DELIVERY DATE
            ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\DTM(2)"))
            oSegment.DataElementValue(1) = "017"      'Date/Time Qualifier
            oSegment.DataElementValue(2) = "19971211"      'Date

            'BILL-TO INFORMATION **************
            'N1 - NAME 
            ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\N1\N1"))
            oSegment.DataElementValue(1) = "BT"      'Entity Identifier Code
            oSegment.DataElementValue(2) = "BUYSNACKS"      'Name
            oSegment.DataElementValue(3) = "1"      'Identification Code Qualifier
            oSegment.DataElementValue(4) = "1223334444"      'Identification Code

            'N3 - ADDRESS INFORMATION
            ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\N1\N3"))
            oSegment.DataElementValue(1) = "P.O. BOX 0000"      'Address Information

            'N4 - GEOGRAPHIC LOCATION
            ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\N1\N4"))
            oSegment.DataElementValue(1) = "TEMPLE"      'City Name
            oSegment.DataElementValue(2) = "TX"      'State or Province Code
            oSegment.DataElementValue(3) = "76503"      'Postal Code

            'SHIP-TO INFORMATION ***************
            'N1 - NAME
            ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\N1\N1"))
            oSegment.DataElementValue(1) = "ST"      'Entity Identifier Code
            oSegment.DataElementValue(2) = "BUYSNACKS PORT"      'Name
            oSegment.DataElementValue(3) = "1"      'Identification Code Qualifier
            oSegment.DataElementValue(4) = "1223334445"      'Identification Code

            'N3 - ADDRESS INFORMATION
            ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\N1\N3"))
            oSegment.DataElementValue(1) = "1000 N. SAMPLE HIGHWAY"      'Address Information

            'N4 - GEOGRAPHIC LOCATION
            ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\N1\N4"))
            oSegment.DataElementValue(1) = "ATHENS"      'City Name
            oSegment.DataElementValue(2) = "GA"      'State or Province Code
            oSegment.DataElementValue(3) = "30603"      'Postal Code


            '********************************************************************************************
            'HL - HIERARCHICAL LEVEL - ORDER ************************************************************
            Do While nOrderCounter <= nOrders

                nHlCounter = nHlCounter + 1
                nHlItemParent = nHlCounter

                ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\HL"))
                oSegment.DataElementValue(1) = nHlCounter      'Hierarchical ID Number
                oSegment.DataElementValue(2) = nHlOrderParent      'Hierarchical Parent ID Number
                oSegment.DataElementValue(3) = "O"      'Hierarchical Level Code    
                oSegment.DataElementValue(4) = "1"      'Hierarchical Child Code

                'PRF - PURCHASE ORDER REFERENCE
                ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\PRF"))
                oSegment.DataElementValue(1) = "A99999-01"      'Purchase Order Number
                oSegment.DataElementValue(2) = "016900"      'Release Number
                oSegment.DataElementValue(4) = "19971207"      'Date

                'REF - REFERENCE IDENTIFICATION
                ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\REF"))
                oSegment.DataElementValue(1) = "IV"      'Reference Identification Qualifier
                oSegment.DataElementValue(2) = "00001"      'Reference Identification

                'FOB - F.O.B. RELATED INSTRUCTIONS
                ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\FOB"))
                oSegment.DataElementValue(1) = "PS"      'Shipment Method of Payment
                oSegment.DataElementValue(2) = "DE"      'Location Qualifier


                '********************************************************************************************
                'HL - HIERARCHICAL LEVEL - ITEMS ************************************************************
                Do While nItemCounter <= nItems
                    nHlCounter = nHlCounter + 1

                    ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\HL")) '1ST INSTANCE OF ITEM LOOP
                    oSegment.DataElementValue(1) = nHlCounter      'Hierarchical ID Number
                    oSegment.DataElementValue(2) = nHlItemParent      'Hierarchical Parent ID Number
                    oSegment.DataElementValue(3) = "I"      'Hierarchical Level Code
                    oSegment.DataElementValue(4) = "0"      'Hierarchical Child Code

                    'LIN - ITEM IDENTIFICATION
                    ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\LIN"))
                    oSegment.DataElementValue(1) = nItemCounter      'Assigned Identification
                    oSegment.DataElementValue(2) = "UA"      'Product/Service ID Qualifier
                    oSegment.DataElementValue(3) = "Product ID of item " & Trim(Str(nItemCounter))      'Product/Service ID

                    'SN1 - ITEM DETAIL (SHIPMENT)
                    ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\SN1"))
                    oSegment.DataElementValue(2) = "13"      'Number of Units Shipped
                    oSegment.DataElementValue(3) = "CA"      'Unit or Basis for Measurement Code
                    oSegment.DataElementValue(5) = "13"      'Quantity Ordered
                    oSegment.DataElementValue(6) = "CA"      'Unit or Basis for Measurement Code
                    oSegment.DataElementValue(8) = "AC"      'Line Item Status Code

                    'PRF - PURCHASE ORDER REFERENCE
                    ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\PRF"))
                    oSegment.DataElementValue(1) = "A99999-01"      'Purchase Order Number
                    oSegment.DataElementValue(2) = "016900"      'Release Number
                    oSegment.DataElementValue(4) = "19971207"      'Date

                    'PID - PRODUCT/ITEM DESCRIPTION
                    ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\PID"))
                    oSegment.DataElementValue(1) = "F"      'Item Description Type
                    oSegment.DataElementValue(5) = "Description of item " & Trim(Str(nItemCounter))     'Description

                    nItemCounter = nItemCounter + 1 'increment nItemCounter

                Loop    'nItemCounter

                nOrderCounter = nOrderCounter + 1   'increment nOrderCounter

            Loop    'nOrderCounter

            nShipmentCounter = nShipmentCounter + 1     'increment nShipmentCounter
        Loop    'Shipment


        'CTT - TRANSACTION TOTALS
        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("CTT"))
        oSegment.DataElementValue(1) = nItems      'Number of Line Items

        'TRAILING SEGMENTS ARE AUTOMATICALLY CREATED WHEN FREDI COMMITS (SAVES)
        'THE EDIDOC OBJECT INTO AN EDI FILE.
        oEdiDoc.Save(sPath & "856OUTPUT.X12")

        'saves edi string  to a variable
        Dim sEdifile As String
        sEdifile = oEdiDoc.GetEdiString
        MessageBox.Show(sEdifile)


        Me.Cursor = Cursors.Default

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

        MessageBox.Show("Done")


    End Sub
    

    Click here to download a trial version of the Framework EDI