Private Sub cmdGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGenerate.Click
        Dim oEdiDoc As ediDocument
        Dim oInterchange As ediInterchange
        Dim oGroup As ediGroup
        Dim oTransactionset As ediTransactionSet
        Dim oSegment As ediDataSegment
        Dim oSchema As ediSchema
        Dim oSchemas As ediSchemas
        Dim sSefFile As String
        Dim sEdiFile As String
        Dim nHlCount As Integer
        Dim nHlProvParent As Integer
        Dim nHlSubscriberParent As Integer

        Dim oClaimsDs As DataSet
        Dim oClaimsRow As DataRow


        'Prepare a dataset from the Interchange table
        Dim sSql As String = "select * from Interchange"
        Dim oAdapter As New OleDb.OleDbDataAdapter(sSql, oConnection)
        Dim oInterchangeDS As New DataSet("dsInterchange")
        oAdapter.Fill(oInterchangeDS, "dsInterchange")
        Dim oInterchangeRow As DataRow

        Me.Cursor = Cursors.WaitCursor
        sPath = AppDomain.CurrentDomain.BaseDirectory

        sSefFile = sPath & "837_X098.sef"
        sEdiFile = sPath & "837Ex1OUTPUT.x12"

        ediDocument.Set(oEdiDoc, New ediDocument)

        'Changing the cursor type to forward write is memory efficient and improves performance.
        'Also, the loop instances do not have to be specified in the heirarchy string syntax
        oEdiDoc.CursorType = DocumentCursorTypeConstants.Cursor_ForwardWrite

        'Disable standard reference, and use SEF files 
        oSchemas = oEdiDoc.GetSchemas
        oSchemas.EnableStandardReference = False

        'load SEF file
        oSchema = oEdiDoc.[LoadSchema](sSefFile, 0)


        'create interchange loop
        For Each oInterchangeRow In oInterchangeDS.Tables("dsInterchange").Rows

            'set terminators
            oEdiDoc.SegmentTerminator = "~" & vbCrLf
            oEdiDoc.ElementTerminator = "*"
            oEdiDoc.CompositeTerminator = ":"

            'CREATE INTERCHANGE
            ediInterchange.Set(oInterchange, oEdiDoc.CreateInterchange("X", "004010"))
            ediDataSegment.Set(oSegment, oInterchange.GetDataSegmentHeader)
            oSegment.DataElementValue(1) = "00"         ' Authorization Information Qualifier (I01) 
            oSegment.DataElementValue(3) = "00"         ' Security Information Qualifier (I03) 
            oSegment.DataElementValue(5) = "12"         ' Interchange ID Qualifier (I05) 
            oSegment.DataElementValue(6) = oInterchangeRow("SenderID")        ' Interchange Sender ID (I06) 
            oSegment.DataElementValue(7) = "ZZ"         ' Interchange ID Qualifier (I05) 
            oSegment.DataElementValue(8) = oInterchangeRow("ReceiverID")        ' Interchange Receiver ID (I07) 
            oSegment.DataElementValue(9) = "010101"         ' Interchange Date (I08) 
            oSegment.DataElementValue(10) = "0101"      ' Interchange Time (I09) 
            oSegment.DataElementValue(11) = "U"         ' Interchange Control Standards Identifier (I10) 
            oSegment.DataElementValue(12) = "00401"         ' Interchange Control Version Number (I11) 
            oSegment.DataElementValue(13) = oInterchangeRow("ControlNo")         ' Interchange Control Number (I12) 
            oSegment.DataElementValue(14) = "0"         ' Acknowledgment Requested (I13) 
            oSegment.DataElementValue(15) = "T"         ' Usage Indicator (I14) 
            oSegment.DataElementValue(16) = "!"         ' Component Element Separator (I15) 

            'prepare dataset from the FuncGroup table
            sSql = "select * from FuncGroup where Interkey = " & Trim(Str(oInterchangeRow("Interkey")))
            oAdapter = New OleDb.OleDbDataAdapter(sSql, oConnection)
            Dim oGroupDs As New DataSet("dsGroup")
            Dim oGroupRow As DataRow
            oAdapter.Fill(oGroupDs, "dsGroup")

            'create the functional group loop
            For Each oGroupRow In oGroupDs.Tables("dsGroup").Rows

                'CREATE FUNCTIONAL GROUP
                ediGroup.Set(oGroup, oInterchange.CreateGroup("004010X098"))
                ediDataSegment.Set(oSegment, oGroup.GetDataSegmentHeader)
                oSegment.DataElementValue(1) = oGroupRow("FuncID")         ' Functional Identifier Code (479) 
                oSegment.DataElementValue(2) = "APP SENDER"         ' Application Sender's Code (142) 
                oSegment.DataElementValue(3) = "APP RECEIVER"       ' Application Receiver's Code (124) 
                oSegment.DataElementValue(4) = "20010821"       ' Date (373) 
                oSegment.DataElementValue(5) = "1548"       ' Time (337) 
                oSegment.DataElementValue(6) = oGroupRow("ControlNo")      ' Group Control Number (28) 
                oSegment.DataElementValue(7) = "X"      ' Responsible Agency Code (455) 
                oSegment.DataElementValue(8) = "004010X098"         ' Version / Release / Industry Identifier Code (480) 

                'prepare dataset from X098Header table 
                sSql = "select * from X098Header where Groupkey = " & Trim(Str(oGroupRow("Groupkey")))
                oAdapter = New OleDb.OleDbDataAdapter(sSql, oConnection)
                Dim oTranSetDs As New DataSet("dsTransactionSet")
                Dim oSetRow As DataRow
                oAdapter.Fill(oTranSetDs, "dsTransactionSet")

                'create the transaction set loop
                For Each oSetRow In oTranSetDs.Tables("dsTransactionSet").Rows

                    nHlCount = 0

                    'HEADER
                    'ST TRANSACTION SET HEADER
                    ediTransactionSet.Set(oTransactionset, oGroup.CreateTransactionSet("837"))
                    ediDataSegment.Set(oSegment, oTransactionset.GetDataSegmentHeader)
                    oSegment.DataElementValue(1) = "837"        ' Transaction Set Identifier Code (143) 
                    oSegment.DataElementValue(2) = oSetRow("ControlNo")       ' Transaction Set Control Number (329) 

                    'BHT BEGINNING OF HIERARCHICAL TRANSACTION
                    ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("BHT"))
                    oSegment.DataElementValue(1) = "0019"       ' Hierarchical Structure Code (1005) 
                    oSegment.DataElementValue(2) = "00"         ' Transaction Set Purpose Code (353) 
                    oSegment.DataElementValue(3) = "0123"         ' Reference Identification (127) 
                    oSegment.DataElementValue(4) = "19981015"       ' Date (373) 
                    oSegment.DataElementValue(5) = "1230"       ' Time (337) 
                    oSegment.DataElementValue(6) = "RP"         ' Transaction Type Code (640) 

                    'REF TRANSMISSION TYPE IDENTIFICATION
                    ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("REF"))
                    oSegment.DataElementValue(1) = "87"                     ' Reference Identification Qualifier
                    oSegment.DataElementValue(2) = oSetRow("ReferenceID")   ' Reference Identification

                    '1000A SUBMITTER
                    'NM1 SUBMITTER
                    ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("NM1\NM1"))
                    oSegment.DataElementValue(1) = "41"         ' Entity Identifier Code (98) 
                    oSegment.DataElementValue(2) = "1"      ' Entity Type Qualifier (1065) 
                    oSegment.DataElementValue(3) = oSetRow("SubmitterCompanyName")         ' Name Last or Organization Name (1035) 
                    oSegment.DataElementValue(8) = "46"         ' Identification Code Qualifier (66) 
                    oSegment.DataElementValue(9) = oSetRow("SubmitterCode")         ' Identification Code (67) 


                    'PER SUBMITTER EDI CONTACT INFORMATION
                    ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("NM1\PER"))
                    oSegment.DataElementValue(1) = "IC"         ' Contact Function Code (366) 
                    oSegment.DataElementValue(2) = oSetRow("SubmitterContactName")         ' Name (93) 
                    oSegment.DataElementValue(3) = "TE"         ' Communication Number Qualifier (365) 
                    oSegment.DataElementValue(4) = oSetRow("SubmitterPhone")         ' Communication Number (364) 
                    oSegment.DataElementValue(5) = "EX"         ' Communication Number Qualifier (365) 
                    oSegment.DataElementValue(6) = oSetRow("SubmitterExt")        ' Communication Number (364) 


                    '1000B RECEIVER
                    'NM1 RECEIVER NAME
                    ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("NM1(2)\NM1"))
                    oSegment.DataElementValue(1) = "40"         ' Entity Identifier Code (98) 
                    oSegment.DataElementValue(2) = "2"      ' Entity Type Qualifier (1065) 
                    oSegment.DataElementValue(3) = oSetRow("ReceiverCompanyName")         ' Name Last or Organization Name (1035) 
                    oSegment.DataElementValue(8) = "46"         ' Identification Code Qualifier (66) 
                    oSegment.DataElementValue(9) = oSetRow("ReceiverCode")         ' Identification Code (67) 

                    '**** BILLING/PAY-TO PROVIDER HIERARCHICAL LEVEL *******************************************
                    sSql = "select * from X098ProviderInfo where Headerkey = " & Trim(Str(oSetRow("Headerkey")))
                    oAdapter = New OleDb.OleDbDataAdapter(sSql, oConnection)
                    Dim oProviderDs As New DataSet("dsProvider")
                    Dim oProviderRow As DataRow
                    oAdapter.Fill(oProviderDs, "dsProvider")
                    For Each oProviderRow In oProviderDs.Tables("dsProvider").Rows

                        nHlCount = nHlCount + 1
                        nHlProvParent = nHlCount

                        '2000A BILLING/PAY-TO PROVIDER HL LOOP
                        'HL-BILLING PROVIDER
                        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\HL"))
                        oSegment.DataElementValue(1) = nHlCount         ' Hierarchical ID Number (628) 
                        oSegment.DataElementValue(3) = "20"         ' Hierarchical Level Code (735) 
                        oSegment.DataElementValue(4) = "1"      ' Hierarchical Child Code (736) 

                        '2010AA BILLING PROVIDER
                        'NM1 BILLING PROVIDER NAME
                        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\NM1\NM1"))
                        oSegment.DataElementValue(1) = "85"         ' Entity Identifier Code (98) 
                        oSegment.DataElementValue(2) = "2"      ' Entity Type Qualifier (1065) 
                        oSegment.DataElementValue(3) = oProviderRow("CompanyName")         ' Name Last or Organization Name (1035) 
                        oSegment.DataElementValue(8) = "XX"         ' Identification Code Qualifier (66) 
                        oSegment.DataElementValue(9) = oProviderRow("BillingID")         ' Identification Code (67) 

                        'N3 BILLING PROVIDER ADDRESS
                        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\NM1\N3"))
                        oSegment.DataElementValue(1) = oProviderRow("Address1")      ' Address Information (166) 

                        'N4 BILLING PROVIDER LOCATION
                        ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\NM1\N4"))
                        oSegment.DataElementValue(1) = oProviderRow("City")      ' City Name (19) 
                        oSegment.DataElementValue(2) = oProviderRow("State")         ' State or Province Code (156) 
                        oSegment.DataElementValue(3) = oProviderRow("Zip")      ' Postal Code (116) 

                        'LOOP 2000B SUBSCRIBER HIERARCHICAL LEVEL
                        sSql = "select * from X098SubscriberInfo where Providerkey = " & Trim(Str(oProviderRow("Providerkey")))
                        oAdapter = New OleDb.OleDbDataAdapter(sSql, oConnection)
                        Dim oSubscriberDs As New DataSet("dsSubscriber")
                        Dim oSubscriberRow As DataRow
                        oAdapter.Fill(oSubscriberDs, "dsSubscriber")
                        For Each oSubscriberRow In oSubscriberDs.Tables("dsSubscriber").Rows
                            nHlCount = nHlCount + 1
                            nHlSubscriberParent = nHlCount

                            '2000B SUBSCRIBER HL LOOP
                            'HL-SUBSCRIBER
                            ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\HL"))
                            oSegment.DataElementValue(1) = nHlCount         ' Hierarchical ID Number (628) 
                            oSegment.DataElementValue(2) = nHlProvParent         ' Hierarchical Parent ID Number (734) 
                            oSegment.DataElementValue(3) = "22"         ' Hierarchical Level Code (735) 

                            Dim sIndvRel As String = IIf(IsDBNull(oSubscriberRow("IndividualRelation")), "", oSubscriberRow("IndividualRelation"))
                            If sIndvRel = "18" Then
                                oSegment.DataElementValue(4) = "0"
                            Else
                                oSegment.DataElementValue(4) = "1"
                            End If


                            'SBR SUBSCRIBER INFORMATION
                            ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\SBR"))
                            oSegment.DataElementValue(1) = "P"      ' Payer Responsibility Sequence Number Code (1138) 
                            oSegment.DataElementValue(2) = sIndvRel         ' Individual Relationship Code (1069) 
                            oSegment.DataElementValue(3) = oSubscriberRow("PolicyNo")         ' Reference Identification (127) 
                            oSegment.DataElementValue(9) = "HM"         ' Claim Filing Indicator Code (1032) 

                            '2010BA SUBSCRIBER
                            'NM1 SUBSCRIBER NAME
                            ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\NM1\NM1"))
                            oSegment.DataElementValue(1) = "IL"         ' Entity Identifier Code (98) 
                            oSegment.DataElementValue(2) = "1"      ' Entity Type Qualifier (1065) 
                            oSegment.DataElementValue(3) = oSubscriberRow("SubscriberLastOrgName")         ' Name Last or Organization Name (1035) 
                            oSegment.DataElementValue(4) = oSubscriberRow("SubscriberFirstname")         ' Name First (1036) 
                            oSegment.DataElementValue(8) = "MI"         ' Identification Code Qualifier (66) 
                            oSegment.DataElementValue(9) = oSubscriberRow("SubscriberMemberID")         ' Identification Code (67) 

                            'N3 SUBSCRIBER ADDRESS
                            ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\NM1\N3"))
                            oSegment.DataElementValue(1) = oSubscriberRow("SubscriberAddress")  ' Address Information

                            'N4 SUBSCRIBER CITY
                            ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\NM1\N4"))
                            oSegment.DataElementValue(1) = oSubscriberRow("SubscriberCity")      ' City Name (19) 
                            oSegment.DataElementValue(2) = oSubscriberRow("SubscrberState")         ' State or Province Code (156) 
                            oSegment.DataElementValue(3) = oSubscriberRow("SubscriberZip")      ' Postal Code (116) 

                            'DMG SUBSCRIBER DEMOGRAPHIC INFORMATION
                            If Not IsDBNull(oSubscriberRow("SubscriberDOB")) Then
                                ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\NM1\DMG"))
                                oSegment.DataElementValue(1) = "D8"                             ' Date Time Period Format Qualifier
                                oSegment.DataElementValue(2) = oSubscriberRow("SubscriberDOB")  ' Date Time Period 
                                oSegment.DataElementValue(3) = oSubscriberRow("SubscriberGender")   ' Gender Code
                            End If


                            '2010BB SUBSCRIBER/PAYER
                            'NM1 PAYER NAME
                            ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\NM1(2)\NM1"))
                            oSegment.DataElementValue(1) = "PR"                         ' Entity Identifier Code (98) 
                            oSegment.DataElementValue(2) = "2"                          ' Entity Type Qualifier (1065) 
                            If Not IsDBNull(oSubscriberRow("PayerLastOrgName")) Then
                                oSegment.DataElementValue(3) = oSubscriberRow("PayerLastOrgName")   ' Name Last or Organization Name
                            End If
                            If Not IsDBNull(oSubscriberRow("PayerID")) Then
                                oSegment.DataElementValue(8) = "PI"                         ' Identification Code Qualifier
                                oSegment.DataElementValue(9) = oSubscriberRow("PayerID")    ' Identification Code
                            End If

                            'N2 PAYER ADDITIONAL NAME INFORMATION
                            ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\NM1(2)\N2"))
                            oSegment.DataElementValue(1) = "COMPANY"    ' Name

                            'LOOP 2300 CLAIM INFORMATION
                            sSql = "select * from X098Claims where Subscriberkey = " & Trim(Str(oSubscriberRow("Subscriberkey")))
                            oAdapter = New OleDb.OleDbDataAdapter(sSql, oConnection)
                            oClaimsDs = New DataSet("dsClaims")
                            oAdapter.Fill(oClaimsDs, "dsClaims")
                            For Each oClaimsRow In oClaimsDs.Tables("dsClaims").Rows
                                Proc_2300_Claim(oTransactionset, oClaimsDs)
                            Next


                            'LOOP 2000C PATIENT HIERARCHICAL LEVEL
                            sSql = "select * from X098PatientInfo where SubscriberKey = " & Trim(Str(oSubscriberRow("SubscriberKey")))
                            oAdapter = New OleDb.OleDbDataAdapter(sSql, oConnection)
                            Dim oPatientDs As New DataSet("dsPatient")
                            Dim oPatientRow As DataRow
                            oAdapter.Fill(oPatientDs, "dsPatient")
                            For Each oPatientRow In oPatientDs.Tables("dsPatient").Rows
                                nHlCount = nHlCount + 1

                                '2000C Patient HL LOOP
                                'HL-Patient
                                ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\HL"))
                                oSegment.DataElementValue(1) = nHlCount         ' Hierarchical ID Number (628) 
                                oSegment.DataElementValue(2) = nHlSubscriberParent         ' Hierarchical Parent ID Number (734) 
                                oSegment.DataElementValue(3) = "23"         ' Hierarchical Level Code (735) 
                                oSegment.DataElementValue(4) = "0"      ' Hierarchical Child Code (736) 

                                'PAT - PATIENT Patient INFORMATION
                                ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\PAT"))
                                oSegment.DataElementValue(1) = oPatientRow("RelationshipCode")      'Entity Identifier Code

                                'LOOP 2010CA PATIENT NAME
                                'NM1 - INDIVIDUAL OR ORGANIZATIONAL NAME
                                ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\NM1"))
                                oSegment.DataElementValue(1) = "QC"      'Entity Identifier Code
                                oSegment.DataElementValue(2) = "1"      'Entity Type Qualifier
                                oSegment.DataElementValue(3) = oPatientRow("Lastname")       'Name Last or Organization Name
                                oSegment.DataElementValue(4) = oPatientRow("Firstname")      'Name First
                                'oSegment.DataElementValue(5) = oRsGX098SubscriberInfo("NM105_MiddleName")     'Name Middle

                                'N3 - ADDRESS INFORMATION
                                ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\NM1\N3"))
                                oSegment.DataElementValue(1) = oPatientRow("Address")       'Address Information

                                'N4 - GEOGRAPHIC LOCATION
                                ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\NM1\N4"))
                                If Not IsDBNull(oPatientRow("City")) Then
                                    oSegment.DataElementValue(1) = oPatientRow("City")  'City Name
                                End If

                                If Not IsDBNull(oPatientRow("State")) Then
                                    oSegment.DataElementValue(2) = oPatientRow("State")  'State
                                End If

                                If Not IsDBNull(oPatientRow("Zip")) Then 'Zip code
                                    oSegment.DataElementValue(3) = oPatientRow("Zip")
                                End If

                                'DMG - DEMOGRAPHIC INFORMATION
                                If Not IsDBNull(oPatientRow("DOB")) Then
                                    ediDataSegment.Set(oSegment, oTransactionset.CreateDataSegment("HL\NM1\DMG"))
                                    oSegment.DataElementValue(1) = "D8"      'Date Time Period Format Qualifier
                                    oSegment.DataElementValue(2) = oPatientRow("DOB") 'Date
                                    oSegment.DataElementValue(3) = oPatientRow("Gender") 'State Name
                                End If

                                'LOOP 2300 CLAIM INFORMATION
                                sSql = "select * from X098Claims where Patientkey = " & Trim(Str(oPatientRow("Patientkey"))) & " and Subscriberkey = " & Trim(Str(oSubscriberRow("Subscriberkey")))
                                oAdapter = New OleDb.OleDbDataAdapter(sSql, oConnection)
                                oClaimsDs = New DataSet("dsClaims")
                                oAdapter.Fill(oClaimsDs, "dsClaims")
                                For Each oClaimsRow In oClaimsDs.Tables("dsClaims").Rows
                                    Proc_2300_Claim(oTransactionset, oClaimsDs)
                                Next 'Claims
                            Next    'Patient
                        Next    'Subscriber
                    Next    'Provider Info
                Next    'SetRow
            Next    'GroupRow
        Next    'oInterchangeRow

        oEdiDoc.Save(sEdiFile)  'save to a file

        'Display EDI string
        Dim ofrmViewEDI As frmViewEDI
        ofrmViewEDI = New frmViewEDI
        ofrmViewEDI.Text = "837 4010X098 EDI File"
        ofrmViewEDI.RichTextBox1.Text = oEdiDoc.GetEdiString() 'get EDI string from FREDI object
        ofrmViewEDI.Show()

        oSegment.Dispose()
        oTransactionset.Dispose()
        oGroup.Dispose()
        oInterchange.Dispose()
        oSchema.Dispose()
        oSchemas.Dispose()
        oEdiDoc.Dispose()

        Me.Cursor = Cursors.Default
        MsgBox("Done")
        cmdGenerate.Enabled = False
    End Sub

    Private Sub Proc_2300_Claim(ByRef oTransactionSet As ediTransactionSet, ByRef oClaimsDs As DataSet)
        Dim oSegment As ediDataSegment
        Dim oAdapter As OleDb.OleDbDataAdapter
        Dim oClaimsRow As DataRow
        Dim oServiceDs As DataSet
        Dim oServiceRow As DataRow
        Dim sSql As String
        Dim iItemCount As Integer

        For Each oClaimsRow In oClaimsDs.Tables("dsClaims").Rows
            '2300 CLAIM
            'CLM CLAIM LEVEL INFORMATION
            ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("HL\CLM\CLM"))
            oSegment.DataElementValue(1) = oClaimsRow("PatientAccountNo")         ' Claim Submitter's Identifier (1028) 
            oSegment.DataElementValue(2) = oClaimsRow("ClaimAmount")         ' Monetary Amount (782) 
            oSegment.DataElementValue(5, 1) = "11"      ' Facility Code Value (1331) 
            oSegment.DataElementValue(5, 3) = "1"       ' Claim Frequency Type Code (1325) 
            oSegment.DataElementValue(6) = "Y"      ' Yes/No Condition or Response Code (1073) 
            oSegment.DataElementValue(7) = "A"      ' Provider Accept Assignment Code (1359) 
            oSegment.DataElementValue(8) = "Y"      ' Yes/No Condition or Response Code (1073) 
            oSegment.DataElementValue(9) = "Y"      ' Release of Information Code (1363) 
            oSegment.DataElementValue(10) = "C"         ' Patient Signature Source Code (1351) 

            'DTP DATE OF ONSET
            ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("HL\CLM\DTP"))
            oSegment.DataElementValue(1) = "431"        ' Date/Time Qualifier (374) 
            oSegment.DataElementValue(2) = "D8"         ' Date Time Period Format Qualifier (1250) 
            oSegment.DataElementValue(3) = oClaimsRow("OnsetDate")         ' Date Time Period (1251) 

            'REF CLEARING HOUSE CLAIM NUMBER
            ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("HL\CLM\REF"))
            oSegment.DataElementValue(1) = "D9"         ' Reference Identification Qualifier (128) 
            oSegment.DataElementValue(2) = "17312345600006351"      ' Reference Identification (127) 

            'HI HEALTH CARE DIAGNOSIS CODES
            ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("HL\CLM\HI"))
            oSegment.DataElementValue(1, 1) = "BK"      ' Code List Qualifier Cod
            oSegment.DataElementValue(1, 2) = "0340"    ' Industry Code
            oSegment.DataElementValue(2, 1) = "BF"      ' Code List Qualifier Code
            oSegment.DataElementValue(2, 2) = "V7389"   ' Industry Code

            '2310B RENDERING PROVIDER
            'NM1 RENDERING PROVIDER NAME
            ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("HL\CLM\NM1\NM1"))
            oSegment.DataElementValue(1) = "82"         ' Entity Identifier Code (98) 
            oSegment.DataElementValue(2) = "1"      ' Entity Type Qualifier (1065) 
            oSegment.DataElementValue(3) = oClaimsRow("RenderingLastname")         ' Name Last or Organization Name (1035) 
            oSegment.DataElementValue(4) = oClaimsRow("RenderingFirstname")         ' Name First (1036) 
            oSegment.DataElementValue(8) = "34"         ' Identification Code Qualifier (66) 
            oSegment.DataElementValue(9) = oClaimsRow("RenderingID")         ' Identification Code (67) 

            'PRV RENDERING PROVIDER INFORMATION
            ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("HL\CLM\NM1\PRV"))
            oSegment.DataElementValue(1) = "PE"         ' Provider Code (1221) 
            oSegment.DataElementValue(2) = "ZZ"         ' Reference Identification Qualifier (128) 
            oSegment.DataElementValue(3) = "203BF0100Y"         ' Reference Identification (127) 

            '2310D SERVICE LOCATION
            'NM1 SERVICE FACILITY LOCATION
            ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("HL\CLM\NM1(2)\NM1"))
            oSegment.DataElementValue(1) = "77"         ' Entity Identifier Code (98) 
            oSegment.DataElementValue(2) = "2"      ' Entity Type Qualifier (1065) 
            oSegment.DataElementValue(3) = oClaimsRow("FacilityName")         ' Name Last or Organization Name (1035) 
            oSegment.DataElementValue(8) = "24"         ' Identification Code Qualifier (66) 
            oSegment.DataElementValue(9) = oClaimsRow("FacilityID")         ' Identification Code (67) 

            'N3 SERVICE FACILITY ADDRESS
            ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("HL\CLM\NM1(2)\N3"))
            oSegment.DataElementValue(1) = oClaimsRow("FacilityAddr")       ' Address Information 

            'N4 SERVICE FACILITY CITY/STATE/ZIP
            ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("HL\CLM\NM1(2)\N4"))
            oSegment.DataElementValue(1) = oClaimsRow("FacilityCity")       ' City
            oSegment.DataElementValue(2) = oClaimsRow("FacilityState")      'State
            oSegment.DataElementValue(3) = oClaimsRow("FacilityZip")        'Postal Code

            '******* Patient SERVICE LINE **************************************************************
            sSql = "select * from X098ServiceInfo where Claimskey = " & Trim(Str(oClaimsRow("Claimskey")))
            oAdapter = New OleDb.OleDbDataAdapter(sSql, oConnection)
            oServiceDs = New DataSet("dsService")
            oAdapter.Fill(oServiceDs, "dsService")
            '2400 SERVICE LINE
            iItemCount = 0
            For Each oServiceRow In oServiceDs.Tables("dsService").Rows
                iItemCount = iItemCount + 1

                'LX SERVICE LINE COUNTER
                ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("HL\CLM\LX\LX"))
                oSegment.DataElementValue(1) = iItemCount           ' Assigned Number (554) 

                'SV1 PROFESSIONAL SERVICE
                ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("HL\CLM\LX\SV1"))
                oSegment.DataElementValue(1, 1) = "HC"      ' Product/Service ID Qualifier (235) 
                oSegment.DataElementValue(1, 2) = oServiceRow("ServiceID")      ' Product/Service ID (234) 
                oSegment.DataElementValue(2) = oServiceRow("ServiceAmount")         ' Monetary Amount (782) 
                oSegment.DataElementValue(3) = "UN"         ' Unit or Basis for Measurement Code (355) 
                oSegment.DataElementValue(4) = "1"         ' Quantity (380) 
                oSegment.DataElementValue(7, 1) = oServiceRow("Diagnosis")      ' Diagnosis Code Pointer (1328) 
                oSegment.DataElementValue(9) = "N"      ' Yes/No Condition or Response Code (1073) 

                'DTP DATE - SERVICE DATE(S)
                ediDataSegment.Set(oSegment, oTransactionSet.CreateDataSegment("HL\CLM\LX\DTP"))
                oSegment.DataElementValue(1) = "472"        ' Date/Time Qualifier (374) 
                oSegment.DataElementValue(2) = "D8"         ' Date Time Period Format Qualifier (1250) 
                oSegment.DataElementValue(3) = oServiceRow("ServiceDate")        ' Date Time Period (1251) 
            Next    'Service
        Next    'Claims

    End Sub

    Click here to download a trial version of the Framework EDI