Private Sub Command0_Click()
    ' THIS CODE IS SUPPLEMENTAL INFORMATION ONLY AND IS PROVIDED "AS IS" WITHOUT WARRANTY
    '   OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
    '   WARRANTIES OF MERCHANTIBILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
    '   USE AT YOUR OWN RISK.
    
    'THIS PROGRAM IS TO DEMONSTRATE HOW TO USE THE FRAMEWORK EDI COMPONENT TO GENERATE AN EDI FILE FROM A DATABASE IN ACCESS.

    'FREDI objects
    Dim oEdiDoc As Fredi.ediDocument
    Dim oSchema As Fredi.ediSchema
    Dim oSchemas As Fredi.ediSchemas
    Dim oInterchange As Fredi.ediInterchange
    Dim oGroup As Fredi.ediGroup
    Dim oTransactionset As Fredi.ediTransactionSet
    Dim oSegment As Fredi.ediDataSegment
     
    'ADO objects
    Dim oConn As ADODB.Connection
    Dim oRsInterchange As ADODB.Recordset
    Dim oRsFuncGroup As ADODB.Recordset
    Dim oRsX096Header As ADODB.Recordset
    Dim oRsX096ProviderInfo As ADODB.Recordset
    Dim oRsX096SubscriberInfo As ADODB.Recordset
    Dim oRsX096Claims As ADODB.Recordset
    Dim oRsX096OtherSubscriberInfo As ADODB.Recordset
    Dim oRsX096ServiceInfo As ADODB.Recordset
    Dim oRsX096DependentInfo As ADODB.Recordset

    
    Dim sPath As String
    Dim sEdiFile As String
    Dim sSefFile As String
    Dim sInterID As String
    Dim sMessageID As String
    Dim sSubscriberMemberID As String
    Dim sPayerID As String
    Dim bPatient As Boolean
    Dim sClaimSubmitID As String
    Dim sConn As String
    Dim nHL As Integer
    Dim nHlSubscriberParent As Integer
    Dim nHlDependentParent As Integer
    
    sPath = CurrentProject.Path & "\"
    sEdiFile = "837X096_Output.X12"
    sSefFile = "837_X096.SEF"

    'CREATES EDI DOCUMENT OBJECT
    Set oEdiDoc = New Fredi.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.
    Set oSchemas = oEdiDoc.GetSchemas
    oSchemas.EnableStandardReference = False
     
    'ENABLES FORWARD WRITE, AND INCREASES BUFFER I/O TO IMPROVE PERFORMANCE
    oEdiDoc.CursorType = Cursor_ForwardWrite
    oEdiDoc.Property(Property_DocumentBufferIO) = 200
     
    'SET TERMINATORS
    oEdiDoc.SegmentTerminator = "~{13:10}"
    oEdiDoc.ElementTerminator = "*"
    oEdiDoc.CompositeTerminator = ":"
     
    'LOADS THE SEF FILE
    Set oSchema = oEdiDoc.ImportSchema(sPath & sSefFile, 0)
     
     'OPENS DATABASE CONNECTION
    sConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & sPath & "GenX096.mdb"
    Set oConn = New ADODB.Connection
    oConn.Open sConn

    'CREATE TABLE OBJECTS
    Set oRsInterchange = New ADODB.Recordset
    Set oRsFuncGroup = New ADODB.Recordset
    Set oRsX096Header = New ADODB.Recordset
    Set oRsX096ProviderInfo = New ADODB.Recordset
    Set oRsX096SubscriberInfo = New ADODB.Recordset
    Set oRsX096DependentInfo = New ADODB.Recordset
    Set oRsX096Claims = New ADODB.Recordset
    Set oRsX096OtherSubscriberInfo = New ADODB.Recordset
    Set oRsX096ServiceInfo = New ADODB.Recordset
 
    'INTERCHANGE INFO
    oRsInterchange.Open "select * from Interchange", oConn
    Do While Not oRsInterchange.EOF
         
        'CREATES THE ISA SEGMENT
        Set oInterchange = oEdiDoc.CreateInterchange("X", "004010")
        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) = oRsInterchange("SenderQlfr").Value      'Interchange ID Qualifier
        oSegment.DataElementValue(6) = oRsInterchange("SenderID").Value      'Interchange Sender ID
        oSegment.DataElementValue(7) = oRsInterchange("ReceiverQlfr").Value     'Interchange ID Qualifier
        oSegment.DataElementValue(8) = oRsInterchange("ReceiverID").Value     'Interchange Receiver ID
        oSegment.DataElementValue(9) = "010821"     '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) = oRsInterchange("ControlNo").Value     'Interchange Control Number
        oSegment.DataElementValue(14) = "0"     'Acknowledgment Requested
        oSegment.DataElementValue(15) = "T"     'Usage Indicator
        oSegment.DataElementValue(16) = ":"     'Component Element Separator
        
        'FUNCTIONAL GROUP INFO
        oRsFuncGroup.Open "select * from FuncGroup where InterchangeKey = " & Trim(Str(oRsInterchange("InterchangeKey").Value)), oConn
    
        Do While Not oRsFuncGroup.EOF
            'CREATES THE GS SEGMENT
            Set oGroup = oInterchange.CreateGroup("004010X096")
            Set oSegment = oGroup.GetDataSegmentHeader
            oSegment.DataElementValue(1) = oRsFuncGroup("FuncID").Value      'Functional Identifier Code
            oSegment.DataElementValue(2) = oRsInterchange("SenderID").Value       'Application Sender's Code
            oSegment.DataElementValue(3) = oRsInterchange("ReceiverID").Value    'Application Receiver's Code
            oSegment.DataElementValue(4) = "20010821"     'Date
            oSegment.DataElementValue(5) = "1548"     'Time
            oSegment.DataElementValue(6) = oRsFuncGroup("ControlNo").Value      'Group Control Number
            oSegment.DataElementValue(7) = "X"     'Responsible Agency Code
            oSegment.DataElementValue(8) = "004010X096"     'Version / Release / Industry Identifier Code
            
            'MESSAGE AND HEADER INFO
            oRsX096Header.Open "select * from X096Header where GroupKey = " & Trim(Str(oRsFuncGroup("GroupKey").Value)), oConn
            Do While Not oRsX096Header.EOF
                nHL = 0 'init HL counter

                'CREATES THE ST SEGMENT
                Set oTransactionset = oGroup.CreateTransactionSet("837")
                Set oSegment = oTransactionset.GetDataSegmentHeader
                oSegment.DataElementValue(1) = oRsX096Header("MessageNo").Value      'Transaction Set Identifier Code
                oSegment.DataElementValue(2) = oRsX096Header("ControlNo").Value     'Transaction Set Control Number
                 
                'BHT - BEGINNING OF HIERARCHICAL TRANSACTION
                Set oSegment = oTransactionset.CreateDataSegment("BHT")
                oSegment.DataElementValue(1) = "0019"      'Hierarchical Structure Code
                oSegment.DataElementValue(2) = "00"      'Transaction Set Purpose Code
                oSegment.DataElementValue(3) = oRsX096Header("ReferenceID").Value       'Reference Identification
                oSegment.DataElementValue(4) = oRsX096Header("ReferenceDate").Value       'Date
                oSegment.DataElementValue(5) = "1230"      'Time
                oSegment.DataElementValue(6) = "CH"
                 
                'REF - REFERENCE IDENTIFICATION
                Set oSegment = oTransactionset.CreateDataSegment("REF")
                oSegment.DataElementValue(1) = "87"      'Reference Identification Qualifier
                oSegment.DataElementValue(2) = "004010X096"      'Reference Identification
                 
                'NM1 - INDIVIDUAL OR ORGANIZATIONAL NAME
                Set oSegment = oTransactionset.CreateDataSegment("NM1\NM1")
                oSegment.DataElementValue(1) = "41"      'Entity Identifier Code
                oSegment.DataElementValue(2) = "2"      'Entity Type Qualifier
                oSegment.DataElementValue(3) = oRsX096Header("SubmitterCompanyName").Value      'Name Last or Organization Name
                oSegment.DataElementValue(8) = "46"      'Identification Code Qualifier
                oSegment.DataElementValue(9) = oRsX096Header("SubmitterCode").Value       'Identification Code
                                
                Set oSegment = oTransactionset.CreateDataSegment("NM1\PER")
                oSegment.DataElementValue(1) = "IC"      'Entity Identifier Code
                oSegment.DataElementValue(2) = "SUBMI NAME"      'Entity Type Qualifier
                oSegment.DataElementValue(3) = "TE"
                oSegment.DataElementValue(4) = "(123)555-9876"
                
                'NM1 - INDIVIDUAL OR ORGANIZATIONAL NAME
                Set oSegment = oTransactionset.CreateDataSegment("NM1(2)\NM1")
                oSegment.DataElementValue(1) = "40"      'Entity Identifier Code
                oSegment.DataElementValue(2) = "2"      'Entity Type Qualifier
                oSegment.DataElementValue(3) = oRsX096Header("ReceiverCompanyName").Value       'Name Last or Organization Name
                oSegment.DataElementValue(8) = "46"      'Identification Code Qualifier
                oSegment.DataElementValue(9) = oRsX096Header("ReceiverCode").Value       'Identification Code
                 
                'PROVIDER INFORMATION
                oRsX096ProviderInfo.Open "select * from X096ProviderInfo where TSetKey = " & Trim(Str(oRsX096Header("TSetKey").Value)), oConn
                Do While Not oRsX096ProviderInfo.EOF
                
                    'HL - HIERARCHICAL LEVEL
                    nHL = nHL + 1
                    nHlSubscriberParent = nHL
                    Set oSegment = oTransactionset.CreateDataSegment("HL\HL")
                    oSegment.DataElementValue(1) = nHL      'Hierarchical ID Number
                    oSegment.DataElementValue(3) = "20"      'Hierarchical Level Code
                    oSegment.DataElementValue(4) = "1"      'Hierarchical Child Code
                     
                    'PRV - PROVIDER INFORMATION
                    Set oSegment = oTransactionset.CreateDataSegment("HL\PRV")
                    oSegment.DataElementValue(1) = "BI"      'Provider Code
                    oSegment.DataElementValue(2) = "ZZ"      'Reference Identification Qualifier
                    oSegment.DataElementValue(3) = oRsX096ProviderInfo("BillingID").Value       'Reference Identification
                     
                    'NM1 - INDIVIDUAL OR ORGANIZATIONAL NAME
                    Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\NM1")
                    oSegment.DataElementValue(1) = "85"      'Entity Identifier Code
                    oSegment.DataElementValue(2) = "2"      'Entity Type Qualifier
                    oSegment.DataElementValue(3) = oRsX096ProviderInfo("CompanyName").Value       'Name Last or Organization Name
                    oSegment.DataElementValue(8) = "XX"      'Identification Code Qualifier
                    oSegment.DataElementValue(9) = oRsX096ProviderInfo("NationalID").Value       'Identification Code
                     
                    'N3 - ADDRESS INFORMATION
                    Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\N3")
                    oSegment.DataElementValue(1) = oRsX096ProviderInfo("Address1").Value       'Address Information
                     
                    'N4 - GEOGRAPHIC LOCATION
                    Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\N4")
                    oSegment.DataElementValue(1) = oRsX096ProviderInfo("City").Value       'City Name
                    oSegment.DataElementValue(2) = oRsX096ProviderInfo("State").Value       'State or Province Code
                    oSegment.DataElementValue(3) = oRsX096ProviderInfo("Zip").Value       'Postal Code
                     
                    'REF - REFERENCE IDENTIFICATION
                    Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\REF")
                    oSegment.DataElementValue(1) = "G2"      'Reference Identification Qualifier
                    oSegment.DataElementValue(2) = oRsX096ProviderInfo("CommercialNo").Value       'Reference Identification
                    
                    
                    '************************************
                    '*                                  *
                    '* SUBSCRIBER LOOP                  *
                    '*                                  *
                    '************************************
                
                    'SUBSCRIBER INFORMATION
                    oRsX096SubscriberInfo.Open "select * from X096SubscriberInfo where Providerkey = " & Trim(Str(oRsX096ProviderInfo("Providerkey").Value)), oConn
                    Do While Not oRsX096SubscriberInfo.EOF
            
                        nHL = nHL + 1
                        nHlDependentParent = nHL
                        
                        If oRsX096SubscriberInfo("Patient").Value = "Y" Then
                            bPatient = True
                        Else
                            bPatient = False
                        End If
                        
                        'HL - HIERARCHICAL LEVEL
                        Set oSegment = oTransactionset.CreateDataSegment("HL\HL")
                        oSegment.DataElementValue(1) = nHL     'Hierarchical ID Number
                        oSegment.DataElementValue(2) = nHlSubscriberParent      'Hierarchical Parent ID Number
                        oSegment.DataElementValue(3) = "22"      'Hierarchical Level Code
                        If bPatient Then
                            oSegment.DataElementValue(4) = "0"      'Hierarchical Child Code
                        Else
                            oSegment.DataElementValue(4) = "1"      'Hierarchical Child Code
                        End If
                         
                        'SBR - SUBSCRIBER INFORMATION
                        Set oSegment = oTransactionset.CreateDataSegment("HL\SBR")
                        oSegment.DataElementValue(1) = "P"      'Payer Responsibility Sequence Number Code
                        If bPatient Then
                            oSegment.DataElementValue(2) = "18"      'Individual Relationship Code
                        End If
                        oSegment.DataElementValue(9) = "MB"      'Claim Filing Indicator Code
                         
                        'NM1 - INDIVIDUAL OR ORGANIZATIONAL NAME
                        Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\NM1")
                        oSegment.DataElementValue(1) = "IL"      'Entity Identifier Code
                        oSegment.DataElementValue(2) = "1"      'Entity Type Qualifier
                        oSegment.DataElementValue(3) = oRsX096SubscriberInfo("SubscriberLastOrgName").Value       'Name Last or Organization Name
                        oSegment.DataElementValue(4) = oRsX096SubscriberInfo("SubscriberFirstname").Value      'Name First
                        oSegment.DataElementValue(5) = "T"      'Name Middle
                        oSegment.DataElementValue(8) = "MI"      'Identification Code Qualifier
                        oSegment.DataElementValue(9) = oRsX096SubscriberInfo("SubscriberMemberID").Value      'Identification Code
                         
                        'N3 - ADDRESS INFORMATION
                        Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\N3")
                        oSegment.DataElementValue(1) = oRsX096SubscriberInfo("SubscriberAddress").Value       'Address Information
                         
                        'N4 - GEOGRAPHIC LOCATION
                        Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\N4")
                        oSegment.DataElementValue(1) = oRsX096SubscriberInfo("SubscriberCity").Value       'City Name
                        oSegment.DataElementValue(2) = "PA"      'State or Province Code
                        oSegment.DataElementValue(3) = oRsX096SubscriberInfo("SubscriberZip").Value       'Postal Code
                         
                        'DMG - DEMOGRAPHIC INFORMATION
                        Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\DMG")
                        oSegment.DataElementValue(1) = "D8"      'Date Time Period Format Qualifier
                        oSegment.DataElementValue(2) = "19261111"      'Date Time Period
                        oSegment.DataElementValue(3) = "M"      'Gender Code
                         
                        'NM1 - INDIVIDUAL OR ORGANIZATIONAL NAME
                        Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\NM1")
                        oSegment.DataElementValue(1) = "PR"      'Entity Identifier Code
                        oSegment.DataElementValue(2) = "2"      'Entity Type Qualifier
                        oSegment.DataElementValue(3) = oRsX096SubscriberInfo("PayerLastOrgName").Value       'Name Last or Organization Name
                        oSegment.DataElementValue(8) = "PI"      'Identification Code Qualifier
                        oSegment.DataElementValue(9) = oRsX096SubscriberInfo("PayerID").Value      'Identification Code
                        
                        If bPatient Then
                            'CLAIMS INFO
                            oRsX096Claims.Open "select * from X096Claims where SubscriberKey = " & Trim(Str(oRsX096SubscriberInfo("SubscriberKey").Value)), oConn
                            Do While Not oRsX096Claims.EOF
                               
                                'CLM - HEALTH CLAIM
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\CLM")
                                oSegment.DataElementValue(1) = oRsX096Claims("PatientAccountNo").Value      'Claim Submitter's Identifier
                                oSegment.DataElementValue(2) = oRsX096Claims("ClaimAmount").Value       'Monetary Amount
                                oSegment.DataElementValue(5, 1) = "14"    'Facility Code Value
                                oSegment.DataElementValue(5, 2) = "A"    'Facility Code Qualifier
                                oSegment.DataElementValue(5, 3) = "1"    'Claim Frequency Type Code
                                oSegment.DataElementValue(6) = "Y"      'Provider Accept Assignment Code
                                oSegment.DataElementValue(8) = "Y"      'Yes/No Condition or Response Code
                                oSegment.DataElementValue(9) = "Y"      'Release of Information Code
                                oSegment.DataElementValue(18) = "Y"
                                 
                                'DTP - DATE OR TIME OR PERIOD
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\DTP")
                                oSegment.DataElementValue(1) = "434"      'Date/Time Qualifier
                                oSegment.DataElementValue(2) = "D8"      'Date Time Period Format Qualifier
                                oSegment.DataElementValue(3) = oRsX096Claims("ClaimDate").Value       'Date Time Period
                                 
                                'CL1 - CLAIM CODES
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\CL1")
                                oSegment.DataElementValue(1) = "3"      'Admission Type Code
                                oSegment.DataElementValue(2) = "1"      'Admission Source Code
                                 
                                'HI - HEALTH CARE INFORMATION CODES
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\HI")
                                oSegment.DataElementValue(1, 1) = "BK"    'Code List Qualifier Code
                                oSegment.DataElementValue(1, 2) = "366.9"    'Industry Code
                                 
                                'HI - HEALTH CARE INFORMATION CODES
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\HI(2)")
                                oSegment.DataElementValue(1, 1) = "BF"    'Code List Qualifier Code
                                oSegment.DataElementValue(1, 2) = "401.9"    'Industry Code
                                oSegment.DataElementValue(2, 1) = "BF"    'Code List Qualifier Code
                                oSegment.DataElementValue(2, 2) = "794.31"    'Industry Code
                                 
                                'HI - HEALTH CARE INFORMATION CODES
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\HI(3)")
                                oSegment.DataElementValue(1, 1) = "BQ"    'Code List Qualifier Code
                                oSegment.DataElementValue(1, 2) = "15.3"    'Industry Code
                                oSegment.DataElementValue(1, 3) = "D8"    'Date Time Period Format Qualifier
                                oSegment.DataElementValue(1, 4) = "19960911"    'Date Time Period
                                 
                                'HI - HEALTH CARE INFORMATION CODES
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\HI(4)")
                                oSegment.DataElementValue(1, 1) = "BH"    'Code List Qualifier Code
                                oSegment.DataElementValue(1, 2) = "A1"    'Industry Code
                                oSegment.DataElementValue(1, 3) = "D8"    'Date Time Period Format Qualifier
                                oSegment.DataElementValue(1, 4) = "19261111"    'Date Time Period
                                oSegment.DataElementValue(2, 1) = "BH"    'Code List Qualifier Code
                                oSegment.DataElementValue(2, 2) = "A2"    'Industry Code
                                oSegment.DataElementValue(2, 3) = "D8"    'Date Time Period Format Qualifier
                                oSegment.DataElementValue(2, 4) = "19911101"    'Date Time Period
                                oSegment.DataElementValue(3, 1) = "BH"    'Code List Qualifier Code
                                oSegment.DataElementValue(3, 2) = "B1"    'Industry Code
                                oSegment.DataElementValue(3, 3) = "D8"    'Date Time Period Format Qualifier
                                oSegment.DataElementValue(3, 4) = "19261111"     'Date Time Period
                                oSegment.DataElementValue(4, 1) = "BH"    'Code List Qualifier Code
                                oSegment.DataElementValue(4, 2) = "B2"    'Industry Code
                                oSegment.DataElementValue(4, 3) = "D8"    'Date Time Period Format Qualifier
                                oSegment.DataElementValue(4, 4) = "19870101"    'Date Time Period
                                 
                                'HI - HEALTH CARE INFORMATION CODES
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\HI(5)")
                                oSegment.DataElementValue(1, 1) = "BE"    'Code List Qualifier Code
                                oSegment.DataElementValue(1, 2) = "A2"    'Industry Code
                                oSegment.DataElementValue(1, 5) = "15.31"    'Monetary Amount
                                 
                                'HI - HEALTH CARE INFORMATION CODES
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\HI(6)")
                                oSegment.DataElementValue(1, 1) = "BG"    'Code List Qualifier Code
                                oSegment.DataElementValue(1, 2) = "09"    'Industry Code
                                 
                                'NM1 - INDIVIDUAL OR ORGANIZATIONAL NAME
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\NM1\NM1")
                                oSegment.DataElementValue(1) = "71"      'Entity Identifier Code
                                oSegment.DataElementValue(2) = "1"      'Entity Type Qualifier
                                oSegment.DataElementValue(3) = oRsX096Claims("AttendingLastName").Value       'Name Last or Organization Name
                                oSegment.DataElementValue(4) = oRsX096Claims("AttendingFirstName").Value       'Name First
                                oSegment.DataElementValue(5) = "J"      'Name Middle
                                oSegment.DataElementValue(8) = "XX"      'Identification Code Qualifier
                                oSegment.DataElementValue(9) = oRsX096Claims("AttendingID").Value       'Identification Code
                                 
                                'PRV - PROVIDER INFORMATION
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\NM1\PRV")
                                oSegment.DataElementValue(1) = "AT"      'Provider Code
                                oSegment.DataElementValue(2) = "ZZ"      'Reference Identification Qualifier
                                oSegment.DataElementValue(3) = "363LP0200N"      'Reference Identification
                                
                                'OTHER SUBSCRIBER INFO
                                oRsX096OtherSubscriberInfo.Open "select * from X096OtherSubscriberInfo where ClaimKey = " & Trim(Str(oRsX096Claims("ClaimKey").Value)), oConn
                                Do While Not oRsX096OtherSubscriberInfo.EOF
                                
                                    'SBR - SUBSCRIBER INFORMATION
                                    Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\SBR\SBR")
                                    oSegment.DataElementValue(1) = "S"      'Payer Responsibility Sequence Number Code
                                    oSegment.DataElementValue(2) = "01"      'Individual Relationship Code
                                    oSegment.DataElementValue(3) = "AA123456"      'Reference Identification
                                    oSegment.DataElementValue(4) = "AAAA"      'Name
                                    oSegment.DataElementValue(9) = "CI"      'Claim Filing Indicator Code
                                     
                                    'DMG - DEMOGRAPHIC INFORMATION
                                    Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\SBR\DMG")
                                    oSegment.DataElementValue(1) = "D8"
                                    oSegment.DataElementValue(2) = "20010515"
                                    oSegment.DataElementValue(3) = "F"      'Gender Code
                                     
                                    'OI - OTHER HEALTH INSURANCE INFORMATION
                                    Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\SBR\OI")
                                    oSegment.DataElementValue(3) = "Y"      'Yes/No Condition or Response Code
                                    oSegment.DataElementValue(6) = "Y"      'Release of Information Code
                                     
                                    'NM1 - INDIVIDUAL OR ORGANIZATIONAL NAME
                                    Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\SBR\NM1\NM1")
                                    oSegment.DataElementValue(1) = "IL"      'Entity Identifier Code
                                    oSegment.DataElementValue(2) = "1"      'Entity Type Qualifier
                                    oSegment.DataElementValue(3) = oRsX096OtherSubscriberInfo("OtherSubLastName")      'Name Last or Organization Name
                                    oSegment.DataElementValue(4) = "JANE"      'Name First
                                    oSegment.DataElementValue(5) = "S"      'Name Middle
                                    oSegment.DataElementValue(8) = "MI"      'Identification Code Qualifier
                                    oSegment.DataElementValue(9) = oRsX096OtherSubscriberInfo("OtherSubMin").Value       'Identification Code
                                     
                                    'N3 - ADDRESS INFORMATION
                                    Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\SBR\NM1\N3")
                                    oSegment.DataElementValue(1) = "123 Street"      'Address Information
                                     
                                    'N4 - GEOGRAPHIC LOCATION
                                    Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\SBR\NM1\N4")
                                    oSegment.DataElementValue(1) = "Los Angeles"      'City Name
                                    oSegment.DataElementValue(2) = "CA"      'State or Province Code
                                    oSegment.DataElementValue(3) = "112233"      'Postal Code
                                     
                                    'NM1 - INDIVIDUAL OR ORGANIZATIONAL NAME
                                    Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\SBR\NM1\NM1")
                                    oSegment.DataElementValue(1) = "PR"      'Entity Identifier Code
                                    oSegment.DataElementValue(2) = "2"      'Entity Type Qualifier
                                    oSegment.DataElementValue(3) = oRsX096OtherSubscriberInfo("OtherPayerLastName").Value       'Name Last or Organization Name
                                    oSegment.DataElementValue(8) = "PI"      'Identification Code Qualifier
                                    oSegment.DataElementValue(9) = oRsX096OtherSubscriberInfo("OtherPayerID").Value       'Identification Code
                                    
                                    oRsX096OtherSubscriberInfo.MoveNext
                                Loop    'Not oRsX096OtherSubscriberInfo.EOF
                                oRsX096OtherSubscriberInfo.Close
                                    
                                'SERVICE LINE INFO
                                oRsX096ServiceInfo.Open "select * from X096ServiceInfo where ClaimKey = " & Trim(Str(oRsX096Claims("ClaimKey").Value)), oConn
                                Do While Not oRsX096ServiceInfo.EOF
            
                                    'LX - ASSIGNED NUMBER
                                    Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\LX\LX")
                                    oSegment.DataElementValue(1) = oRsX096ServiceInfo("ServiceLine").Value       'Assigned Number
                                     
                                    'SV2 - INSTITUTIONAL SERVICE
                                    Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\LX\SV2")
                                    oSegment.DataElementValue(1) = oRsX096ServiceInfo("ServiceID").Value       'Product/Service ID
                                    oSegment.DataElementValue(2, 1) = "HC"    'Product/Service ID Qualifier
                                    oSegment.DataElementValue(2, 2) = "SV12"    'Product/Service ID
                                    oSegment.DataElementValue(3) = oRsX096ServiceInfo("ServiceAmount").Value       'Monetary Amount
                                    oSegment.DataElementValue(4) = "UN"      'Unit or Basis for Measurement Code
                                    oSegment.DataElementValue(5) = "1"      'Quantity
                                     
                                    'DTP - DATE OR TIME OR PERIOD
                                    Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\LX\DTP")
                                    oSegment.DataElementValue(1) = "472"      'Date/Time Qualifier
                                    oSegment.DataElementValue(2) = "D8"      'Date Time Period Format Qualifier
                                    oSegment.DataElementValue(3) = oRsX096ServiceInfo("ServiceDate").Value      'Date Time Period
                                     
                                    oRsX096ServiceInfo.MoveNext
                                Loop    'Not oRsX096ServiceInfo.EOF
                                oRsX096ServiceInfo.Close
                                    
                                oRsX096Claims.MoveNext
                            Loop    'Not oRsX096Claims.EOF
                            oRsX096Claims.Close
                        End If  'bPatient
                        
                                              
                        
                        '************************************
                        '*                                  *
                        '* DEPENDENT LOOP                  *
                        '*                                  *
                        '************************************
                    
                        'DEPENDENT INFORMATION
                        oRsX096DependentInfo.Open "select * from X096DependentInfo where SubscriberKey = " & Trim(Str(oRsX096SubscriberInfo("SubscriberKey"))), oConn
                        Do While Not oRsX096DependentInfo.EOF
                
                            nHL = nHL + 1
                            'HL - HIERARCHICAL LEVEL
                            Set oSegment = oTransactionset.CreateDataSegment("HL\HL")
                            oSegment.DataElementValue(1) = nHL     'Hierarchical ID Number
                            oSegment.DataElementValue(2) = nHlDependentParent      'Hierarchical Parent ID Number
                            oSegment.DataElementValue(3) = "23"      'Hierarchical Level Code
                            oSegment.DataElementValue(4) = "0"      'Hierarchical Child Code
                             
                            'PAT - PATIENT dependent INFORMATION
                            Set oSegment = oTransactionset.CreateDataSegment("HL\PAT")
                            oSegment.DataElementValue(1) = oRsX096DependentInfo("RelationshipCode")      'Entity Identifier Code

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

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

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

                            If Not IsNull(oRsX096DependentInfo("State")) Then
                                oSegment.DataElementValue(2) = oRsX096DependentInfo("State")  'State
                            End If

                            If Not IsNull(oRsX096DependentInfo("Zip")) Then 'Zip code
                                oSegment.DataElementValue(3) = oRsX096DependentInfo("Zip")
                            End If

                            'DMG - DEMOGRAPHIC INFORMATION
                            If Not IsNull(oRsX096DependentInfo("DOB")) Then
                                Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\DMG")
                                oSegment.DataElementValue(1) = "D8"      'Date Time Period Format Qualifier
                                oSegment.DataElementValue(2) = oRsX096DependentInfo("DOB") 'Date
                                oSegment.DataElementValue(3) = oRsX096DependentInfo("Gender") 'State Name
                            End If
                        
                            'CLAIMS INFO
                            oRsX096Claims.Open "select * from X096Claims where DependentKey = " & Trim(Str(oRsX096DependentInfo("DependentKey").Value)) & " and Subscriberkey = " & Trim(Str(oRsX096SubscriberInfo("Subscriberkey").Value)), oConn
                            Do While Not oRsX096Claims.EOF
                               
                                'CLM - HEALTH CLAIM
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\CLM")
                                oSegment.DataElementValue(1) = oRsX096Claims("PatientAccountNo").Value      'Claim Submitter's Identifier
                                oSegment.DataElementValue(2) = oRsX096Claims("ClaimAmount").Value       'Monetary Amount
                                oSegment.DataElementValue(5, 1) = "14"    'Facility Code Value
                                oSegment.DataElementValue(5, 2) = "A"    'Facility Code Qualifier
                                oSegment.DataElementValue(5, 3) = "1"    'Claim Frequency Type Code
                                oSegment.DataElementValue(6) = "Y"      'Provider Accept Assignment Code
                                oSegment.DataElementValue(8) = "Y"      'Yes/No Condition or Response Code
                                oSegment.DataElementValue(9) = "Y"      'Release of Information Code
                                oSegment.DataElementValue(18) = "Y"
                                 
                                'DTP - DATE OR TIME OR PERIOD
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\DTP")
                                oSegment.DataElementValue(1) = "434"      'Date/Time Qualifier
                                oSegment.DataElementValue(2) = "D8"      'Date Time Period Format Qualifier
                                oSegment.DataElementValue(3) = oRsX096Claims("ClaimDate").Value       'Date Time Period
                                 
                                'CL1 - CLAIM CODES
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\CL1")
                                oSegment.DataElementValue(1) = "3"      'Admission Type Code
                                oSegment.DataElementValue(2) = "1"      'Admission Source Code
                                 
                                'HI - HEALTH CARE INFORMATION CODES
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\HI")
                                oSegment.DataElementValue(1, 1) = "BK"    'Code List Qualifier Code
                                oSegment.DataElementValue(1, 2) = "366.9"    'Industry Code
                                 
                                'HI - HEALTH CARE INFORMATION CODES
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\HI(2)")
                                oSegment.DataElementValue(1, 1) = "BF"    'Code List Qualifier Code
                                oSegment.DataElementValue(1, 2) = "401.9"    'Industry Code
                                oSegment.DataElementValue(2, 1) = "BF"    'Code List Qualifier Code
                                oSegment.DataElementValue(2, 2) = "794.31"    'Industry Code
                                 
                                'HI - HEALTH CARE INFORMATION CODES
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\HI(3)")
                                oSegment.DataElementValue(1, 1) = "BQ"    'Code List Qualifier Code
                                oSegment.DataElementValue(1, 2) = "15.3"    'Industry Code
                                oSegment.DataElementValue(1, 3) = "D8"    'Date Time Period Format Qualifier
                                oSegment.DataElementValue(1, 4) = "19960911"    'Date Time Period
                                 
                                'HI - HEALTH CARE INFORMATION CODES
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\HI(4)")
                                oSegment.DataElementValue(1, 1) = "BH"    'Code List Qualifier Code
                                oSegment.DataElementValue(1, 2) = "A1"    'Industry Code
                                oSegment.DataElementValue(1, 3) = "D8"    'Date Time Period Format Qualifier
                                oSegment.DataElementValue(1, 4) = "19261111"    'Date Time Period
                                oSegment.DataElementValue(2, 1) = "BH"    'Code List Qualifier Code
                                oSegment.DataElementValue(2, 2) = "A2"    'Industry Code
                                oSegment.DataElementValue(2, 3) = "D8"    'Date Time Period Format Qualifier
                                oSegment.DataElementValue(2, 4) = "19911101"    'Date Time Period
                                oSegment.DataElementValue(3, 1) = "BH"    'Code List Qualifier Code
                                oSegment.DataElementValue(3, 2) = "B1"    'Industry Code
                                oSegment.DataElementValue(3, 3) = "D8"    'Date Time Period Format Qualifier
                                oSegment.DataElementValue(3, 4) = "19261111"     'Date Time Period
                                oSegment.DataElementValue(4, 1) = "BH"    'Code List Qualifier Code
                                oSegment.DataElementValue(4, 2) = "B2"    'Industry Code
                                oSegment.DataElementValue(4, 3) = "D8"    'Date Time Period Format Qualifier
                                oSegment.DataElementValue(4, 4) = "19870101"    'Date Time Period
                                 
                                'HI - HEALTH CARE INFORMATION CODES
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\HI(5)")
                                oSegment.DataElementValue(1, 1) = "BE"    'Code List Qualifier Code
                                oSegment.DataElementValue(1, 2) = "A2"    'Industry Code
                                oSegment.DataElementValue(1, 5) = "15.31"    'Monetary Amount
                                 
                                'HI - HEALTH CARE INFORMATION CODES
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\HI(6)")
                                oSegment.DataElementValue(1, 1) = "BG"    'Code List Qualifier Code
                                oSegment.DataElementValue(1, 2) = "09"    'Industry Code
                                 
                                'NM1 - INDIVIDUAL OR ORGANIZATIONAL NAME
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\NM1\NM1")
                                oSegment.DataElementValue(1) = "71"      'Entity Identifier Code
                                oSegment.DataElementValue(2) = "1"      'Entity Type Qualifier
                                oSegment.DataElementValue(3) = oRsX096Claims("AttendingLastName").Value       'Name Last or Organization Name
                                oSegment.DataElementValue(4) = oRsX096Claims("AttendingFirstName").Value       'Name First
                                oSegment.DataElementValue(5) = "J"      'Name Middle
                                oSegment.DataElementValue(8) = "XX"      'Identification Code Qualifier
                                oSegment.DataElementValue(9) = oRsX096Claims("AttendingID").Value       'Identification Code
                                 
                                'PRV - PROVIDER INFORMATION
                                Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\NM1\PRV")
                                oSegment.DataElementValue(1) = "AT"      'Provider Code
                                oSegment.DataElementValue(2) = "ZZ"      'Reference Identification Qualifier
                                oSegment.DataElementValue(3) = "363LP0200N"      'Reference Identification
                                
                                    
                                'SERVICE LINE INFO
                                oRsX096ServiceInfo.Open "select * from X096ServiceInfo where ClaimKey = " & Trim(Str(oRsX096Claims("ClaimKey").Value)), oConn
                                Do While Not oRsX096ServiceInfo.EOF
            
                                    'LX - ASSIGNED NUMBER
                                    Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\LX\LX")
                                    oSegment.DataElementValue(1) = oRsX096ServiceInfo("ServiceLine").Value       'Assigned Number
                                     
                                    'SV2 - INSTITUTIONAL SERVICE
                                    Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\LX\SV2")
                                    oSegment.DataElementValue(1) = oRsX096ServiceInfo("ServiceID").Value       'Product/Service ID
                                    oSegment.DataElementValue(2, 1) = "HC"    'Product/Service ID Qualifier
                                    oSegment.DataElementValue(2, 2) = "SV12"    'Product/Service ID
                                    oSegment.DataElementValue(3) = oRsX096ServiceInfo("ServiceAmount").Value       'Monetary Amount
                                    oSegment.DataElementValue(4) = "UN"      'Unit or Basis for Measurement Code
                                    oSegment.DataElementValue(5) = "1"      'Quantity
                                     
                                    'DTP - DATE OR TIME OR PERIOD
                                    Set oSegment = oTransactionset.CreateDataSegment("HL\CLM\LX\DTP")
                                    oSegment.DataElementValue(1) = "472"      'Date/Time Qualifier
                                    oSegment.DataElementValue(2) = "D8"      'Date Time Period Format Qualifier
                                    oSegment.DataElementValue(3) = oRsX096ServiceInfo("ServiceDate").Value      'Date Time Period
                                     
                                    oRsX096ServiceInfo.MoveNext
                                Loop    'Not oRsX096ServiceInfo.EOF
                                oRsX096ServiceInfo.Close
                                    
                                oRsX096Claims.MoveNext
                            Loop    'Not oRsX096Claims.EOF
                            oRsX096Claims.Close
                            
                            oRsX096DependentInfo.MoveNext
                        Loop  'Not oRsX096DependentInfo.EOF
                        oRsX096DependentInfo.Close
                        
                        
                        oRsX096SubscriberInfo.MoveNext
                    Loop  'Not oRsX096SubscriberInfo.EOF
                    oRsX096SubscriberInfo.Close
                
                    oRsX096ProviderInfo.MoveNext
                 
                Loop  'Not oRsX096ProviderInfo.EOF
                oRsX096ProviderInfo.Close
                
                
                oRsX096Header.MoveNext
            Loop  'Not oRsX096Header.EOF
            oRsX096Header.Close
            
            oRsFuncGroup.MoveNext
        Loop  'Not oRsFuncGroup.EOF Then
        oRsFuncGroup.Close
        
        oRsInterchange.MoveNext
    Loop  'Not oRsInterchange.EOF
    oRsInterchange.Close
    
    'TRAILING SEGMENTS ARE AUTOMATICALLY CREATED WHEN FREDI SAVES THE EDI DOCUMENT OBJECT INTO AN EDI FILE.
    oEdiDoc.Save sPath & sEdiFile
    
    'DESTROYS OBJECTS
    Set oSegment = Nothing
    Set oTransactionset = Nothing
    Set oGroup = Nothing
    Set oSchema = Nothing
    Set oSchemas = Nothing
    Set oInterchange = Nothing
    Set oEdiDoc = Nothing

    MsgBox "Done"


End Sub

    Click here to download a trial version of the Framework EDI