'This is just an example program to show how to generate an EDI X12 270 X092 HIPAA file
'in VB6 with Framework EDI component

Option Explicit
Private oEdiDoc As Fredi.ediDocument
Private oSchema As Fredi.ediSchema
Private oSchemas As Fredi.ediSchemas
Private oInterchange As Fredi.ediInterchange
Private oGroup As Fredi.ediGroup
Private oTransactionset As Fredi.ediTransactionSet
Private oSegment As Fredi.ediDataSegment
Private sSefFile As String
Private sEdiFile As String

Private Sub cmdGenerate_Click()
    Dim i As Integer
    Dim nCount As Integer
    Dim sPath As String
    Dim sEntity As String
    Dim nIndex As Integer
    Dim sSefFile As String
    Dim sEdiFile As String
    Dim iItemCount As Integer
    Dim sInstance As String
    
    Dim nInfoSources As Integer
    Dim nInfoSourceCounter As Integer
    Dim nInfoReceivers As Integer
    Dim nInfoReceiverCounter As Integer
    Dim nSubscribers As Integer
    Dim nSubscriberCounter As Integer
    Dim nDependents As Integer
    Dim nDependentCounter As Integer
    
    Dim nHlCounter As Integer
    Dim nHlInfoReceiverParent As Integer
    Dim nHlSubscriberParent As Integer
    Dim nHlDependentParent As Integer
    
    Me.MousePointer = vbHourglass
    sPath = App.Path & "\"
    
    sSefFile = "270_X092.sef"
    sEdiFile = "270OUTPUT.x12"
    
    'instantiate edi document object
    Set oEdiDoc = New Fredi.ediDocument
    
    'change cursor type from Dynamic (default) to ForwardWrite to improve performance
    oEdiDoc.CursorType = Cursor_ForwardWrite
    
    'disable internal standard reference library so that component will only use SEF files
    'to obtain schema
    Set oSchemas = oEdiDoc.GetSchemas
    oSchemas.EnableStandardReference = False
    Set oSchema = oEdiDoc.ImportSchema(sPath & sSefFile, 0)
    
    'set terminators
    oEdiDoc.SegmentTerminator = "~" & vbCrLf
    oEdiDoc.ElementTerminator = "*"
    oEdiDoc.CompositeTerminator = ":"
    
    'create ISA
    Set oInterchange = oEdiDoc.CreateInterchange("X", "004010")
    Set oSegment = oInterchange.GetDataSegmentHeader
    oSegment.DataElementValue(1) = "00"
    oSegment.DataElementValue(3) = "00"
    oSegment.DataElementValue(5) = "12"
    oSegment.DataElementValue(6) = "Sender"
    oSegment.DataElementValue(7) = "12"
    oSegment.DataElementValue(8) = "ReceiverID"
    oSegment.DataElementValue(9) = "010821"
    oSegment.DataElementValue(10) = "1548"
    oSegment.DataElementValue(11) = "U"
    oSegment.DataElementValue(12) = "00401"
    oSegment.DataElementValue(13) = "000000020"
    oSegment.DataElementValue(14) = "0"
    oSegment.DataElementValue(15) = "T"
    oSegment.DataElementValue(16) = ":"
    
    'create GS
    Set oGroup = oInterchange.CreateGroup("004010X092")
    Set oSegment = oGroup.GetDataSegmentHeader
    oSegment.DataElementValue(1) = "HS"
    oSegment.DataElementValue(2) = "SenderDept"
    oSegment.DataElementValue(3) = "ReceiverDept"
    oSegment.DataElementValue(4) = "20010821"
    oSegment.DataElementValue(5) = "1548"
    oSegment.DataElementValue(6) = "1"
    oSegment.DataElementValue(7) = "X"
    oSegment.DataElementValue(8) = "004010X092"
    
    'HEADER
    'create ST TRANSACTION SET HEADER
    Set oTransactionset = oGroup.CreateTransactionSet("270")
    Set oSegment = oTransactionset.GetDataSegmentHeader
    oSegment.DataElementValue(2) = "1234"
    
    'Beginning Segment
    'create BHT segment
    Set oSegment = oTransactionset.CreateDataSegment("BHT")
    oSegment.DataElementValue(1) = "0022"
    oSegment.DataElementValue(2) = "13"
    oSegment.DataElementValue(3) = "10001234"
    oSegment.DataElementValue(4) = "19990501"
    oSegment.DataElementValue(5) = "1319"

    
    nInfoSources = 1
    nInfoSourceCounter = 1
    nInfoReceivers = 1
    nInfoReceiverCounter = 1
    nSubscribers = 1
    nSubscriberCounter = 1
    nDependents = 1
    nDependentCounter = 1
    
    
    nHlCounter = 0
    
    '*************************************************************************************************
    'DETAIL INFORMATION SOURCE LEVEL
    Do While nInfoSourceCounter <= nInfoSources
    
        nHlCounter = nHlCounter + 1
        nHlInfoReceiverParent = nHlCounter
        
        'DETAIL INFO SOURCE LEVEL
        'create HL segment in HL loop
        Set oSegment = oTransactionset.CreateDataSegment("HL\HL")
        oSegment.DataElementValue(1) = nHlCounter
        oSegment.DataElementValue(3) = "20"
        oSegment.DataElementValue(4) = "1"
        
        'INFORMATION SOURCE NAME
        'create NM1 segment in NM1 loop nested in HL loop
        Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\NM1")
        oSegment.DataElementValue(1) = "PR"
        oSegment.DataElementValue(2) = "2"
        oSegment.DataElementValue(3) = "ABC COMPANY"
        oSegment.DataElementValue(8) = "PI"
        oSegment.DataElementValue(9) = "842610001"
        
        '*************************************************************************************************
        'DETAIL INFORMATION RECEIVER LEVEL
        Do While nInfoReceiverCounter <= nInfoReceivers
        
            nHlCounter = nHlCounter + 1
            nHlSubscriberParent = nHlCounter
        
            'INFORMATION RECEIVER LEVEL
            Set oSegment = oTransactionset.CreateDataSegment("HL\HL")
            oSegment.DataElementValue(1) = nHlCounter
            oSegment.DataElementValue(2) = nHlInfoReceiverParent
            oSegment.DataElementValue(3) = "21"
            oSegment.DataElementValue(4) = "1"
            
            'INFORMATION RECEIVER NAME
            Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\NM1")
            oSegment.DataElementValue(1) = "1P"
            oSegment.DataElementValue(2) = "1"
            oSegment.DataElementValue(3) = "JONES"
            oSegment.DataElementValue(4) = "MARCUS"
            oSegment.DataElementValue(8) = "SV"
            oSegment.DataElementValue(9) = "0202034"
            
            '*************************************************************************************************
            'DETAIL SUBSCRIBER LEVEL
            Do While nSubscriberCounter <= nSubscribers

                nHlCounter = nHlCounter + 1
                nHlDependentParent = nHlCounter
            
                'SUBSCRIBER LEVEL
                'create HL segment in HL loop
                Set oSegment = oTransactionset.CreateDataSegment("HL\HL")
                oSegment.DataElementValue(1) = nHlCounter
                oSegment.DataElementValue(2) = nHlSubscriberParent
                oSegment.DataElementValue(3) = "22"
                If nDependents > 0 Then
                    oSegment.DataElementValue(4) = "1"
                Else
                    oSegment.DataElementValue(4) = "0"
                End If
                
                'SUBSCRIBER TRACE NUMBER
                'create TRN segment in HL loop
                Set oSegment = oTransactionset.CreateDataSegment("HL\TRN")
                oSegment.DataElementValue(1) = "1"
                oSegment.DataElementValue(2) = "93175-012547"
                oSegment.DataElementValue(3) = "9877281234"
                
                'SUBSCRIBER NAME
                'create NM1 segment in NM1 loop nested in HL loop
                Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\NM1")
                oSegment.DataElementValue(1) = "IL"
                oSegment.DataElementValue(2) = "1"
                oSegment.DataElementValue(3) = "SMITH"
                oSegment.DataElementValue(4) = "ROBERT"
                oSegment.DataElementValue(5) = "B"
                oSegment.DataElementValue(8) = "MI"
                oSegment.DataElementValue(9) = "11122333301"
                
                'create N3 segment in NM1 loop nested in HL loop
                Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\N3")
                oSegment.DataElementValue(1) = "12345 HIGHWAY ST"
                
                'create N4 segment in NM1 loop nested in HL loop
                Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\N4")
                oSegment.DataElementValue(1) = "BURBANK"
                oSegment.DataElementValue(2) = "CA"
                oSegment.DataElementValue(3) = "12345"
                
                
                'SUBSCRIBER ADDITIONAL IDENTIFICATION
                Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\REF")
                oSegment.DataElementValue(1) = "1L"
                oSegment.DataElementValue(2) = "599119"
                
                'SUBSCRIBER DEMOGRAPHIC INFORMATION
                Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\DMG")
                oSegment.DataElementValue(1) = "D8"
                oSegment.DataElementValue(2) = "19430519"
                oSegment.DataElementValue(3) = "M"
                
                'SUBSCRIBER DATE
                Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\DTP")
                oSegment.DataElementValue(1) = "472"
                oSegment.DataElementValue(2) = "D8"
                oSegment.DataElementValue(3) = "19990501"
                
                'SUBSCRIBER ELIGIBILITY OR BENEFIT INQUIRY INFORMATION
                Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\EQ\EQ")
                oSegment.DataElementValue(1) = "98"
                oSegment.DataElementValue(3) = "FAM"
            
                '*************************************************************************************************
                'DETAIL DEPENDENT LEVEL
                Do While nDependentCounter <= nDependents

                    nHlCounter = nHlCounter + 1
                    
                    'DEPENDENT LEVEL
                    Set oSegment = oTransactionset.CreateDataSegment("HL\HL")
                    oSegment.DataElementValue(1) = nHlCounter
                    oSegment.DataElementValue(2) = nHlDependentParent
                    oSegment.DataElementValue(3) = "23"
                    oSegment.DataElementValue(4) = "0"
                    
                    'DEPENDENT TRACE NUMBER
                    Set oSegment = oTransactionset.CreateDataSegment("HL\TRN")
                    oSegment.DataElementValue(1) = "1"
                    oSegment.DataElementValue(2) = "93175-012547"
                    oSegment.DataElementValue(3) = "9877281234"
                    
                    'DEPENDENT NAME
                    Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\NM1")
                    oSegment.DataElementValue(1) = "03"
                    oSegment.DataElementValue(2) = "1"
                    oSegment.DataElementValue(3) = "SMITH"
                    oSegment.DataElementValue(4) = "JANE"
                    oSegment.DataElementValue(5) = "C"
                    
                    Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\N3")
                    oSegment.DataElementValue(1) = "12345 HIGHWAY ST"
                    
                    Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\N4")
                    oSegment.DataElementValue(1) = "BURBANK"
                    oSegment.DataElementValue(2) = "CA"
                    oSegment.DataElementValue(3) = "12345"
                    
                    'SUBSCRIBER ELIGIBILITY OR BENEFIT INQUIRY INFORMATION
                    Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\EQ\EQ")
                    oSegment.DataElementValue(1) = "1"
                    oSegment.DataElementValue(3) = "DEP"
            
                    nDependentCounter = nDependentCounter + 1
                Loop    'nDependents
            
                nSubscriberCounter = nSubscriberCounter + 1
            Loop    'nSubscribers
            
            nInfoReceiverCounter = nInfoReceiverCounter + 1
        Loop    'nInfoReceivers
        
        nInfoSourceCounter = nInfoSourceCounter + 1
    Loop    'nInfoSources
                
    'save edi document object to file
    oEdiDoc.Save sPath & sEdiFile
    
    Me.MousePointer = vbNormal
    MsgBox ("Done.  Output = " & sPath & sEdiFile)
    cmdGenerate.Enabled = False
    
End Sub