'This is just an example program to show how to generate an EDI X12 276 X093 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 nServiceProviders As Integer
    Dim nServiceProviderCounter 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 nHlServiceProviderParent As Integer
    Dim nHlSubscriberParent As Integer
    Dim nHlDependentParent As Integer
    
    Me.MousePointer = vbHourglass
    sPath = App.Path & "\"
    
    sSefFile = sPath & "276_X093.sef"
    sEdiFile = sPath & "276OUTPUT.x12"
    
    'instantiate edi document object
    Set oEdiDoc = New Fredi.ediDocument
    
    'ENABLES FORWARD WRITE, AND INCREASES BUFFER I/O TO IMPROVE PERFORMANCE
    oEdiDoc.CursorType = Cursor_ForwardWrite
    
    '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
        
    'SET TERMINATORS
    oEdiDoc.SegmentTerminator = "~" & vbCrLf
    oEdiDoc.ElementTerminator = "*"
    oEdiDoc.CompositeTerminator = ":"
    
    'LOADS THE SEF FILE
    Set oSchema = oEdiDoc.ImportSchema(sSefFile, 0)
      
    'CREATES THE ISA SEGMENT
    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) = ":"
    
    'CREATES THE GS SEGMENT
    Set oGroup = oInterchange.CreateGroup("004010X093")
    Set oSegment = oGroup.GetDataSegmentHeader
    oSegment.DataElementValue(1) = "HR"
    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) = "004010X093"
    
    'CREATES THE ST SEGMENT
    Set oTransactionset = oGroup.CreateTransactionSet("276")
    Set oSegment = oTransactionset.GetDataSegmentHeader
    oSegment.DataElementValue(2) = "00001"
    
    'BHT - BEGINNING OF HIERARCHICAL TRANSACTION
    Set oSegment = oTransactionset.CreateDataSegment("BHT")
    oSegment.DataElementValue(1) = "0010"
    oSegment.DataElementValue(2) = "13"
    oSegment.DataElementValue(4) = "19961115"
    
    
    nInfoSources = 1
    nInfoSourceCounter = 1
    nInfoReceivers = 1
    nInfoReceiverCounter = 1
    nServiceProviders = 1
    nServiceProviderCounter = 1
    nSubscribers = 3
    nSubscriberCounter = 1
    nDependents = 1
    nDependentCounter = 1
    nHlCounter = 0
    
    '*************************************************************************************************
    'DETAIL INFORMATION SOURCE LEVEL
    Do While nInfoSourceCounter <= nInfoSources
    
        nHlCounter = nHlCounter + 1
        nHlInfoReceiverParent = nHlCounter
    
        'INFO SOURCE LEVEL
        Set oSegment = oTransactionset.CreateDataSegment("HL\HL")
        oSegment.DataElementValue(1) = nHlCounter
        oSegment.DataElementValue(3) = "20"
        oSegment.DataElementValue(4) = "1"
    
        Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\NM1")
        oSegment.DataElementValue(1) = "PR"
        oSegment.DataElementValue(2) = "2"
        oSegment.DataElementValue(3) = txtPayerName.Text
        oSegment.DataElementValue(8) = "PI"
        oSegment.DataElementValue(9) = txtPayerID.Text
        
        
        '*************************************************************************************************
        'DETAIL INFORMATION RECEIVER LEVEL
        Do While nInfoReceiverCounter <= nInfoReceivers
        
            nHlCounter = nHlCounter + 1
            nHlServiceProviderParent = nHlCounter
        
            'INFO RECEIVER LEVEL
            Set oSegment = oTransactionset.CreateDataSegment("HL\HL")
            oSegment.DataElementValue(1) = nHlCounter
            oSegment.DataElementValue(2) = nHlInfoReceiverParent
            oSegment.DataElementValue(3) = "21"
            oSegment.DataElementValue(4) = "1"
        
            Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\NM1")
            oSegment.DataElementValue(1) = "41"
            oSegment.DataElementValue(2) = "2"
            oSegment.DataElementValue(3) = txtReceiverName.Text
            oSegment.DataElementValue(8) = "46"
            oSegment.DataElementValue(9) = txtReceiverETIN.Text
            
            
            '*************************************************************************************************
            'DETAIL SERVICE PROVIDER LEVEL
            Do While nServiceProviderCounter <= nServiceProviders

                nHlCounter = nHlCounter + 1
                nHlSubscriberParent = nHlCounter
            
                'SERVICE PROVIDER LEVEL
                Set oSegment = oTransactionset.CreateDataSegment("HL\HL")
                oSegment.DataElementValue(1) = nHlCounter
                oSegment.DataElementValue(2) = nHlServiceProviderParent
                oSegment.DataElementValue(3) = "19"
                oSegment.DataElementValue(4) = "1"
        
                Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\NM1")
                oSegment.DataElementValue(1) = "1P"
                oSegment.DataElementValue(2) = "2"
                oSegment.DataElementValue(3) = txtProviderName(0).Text
                oSegment.DataElementValue(8) = "SV"
                oSegment.DataElementValue(9) = txtProviderNo(0).Text
                
            
                '*************************************************************************************************
                'DETAIL SUBSCRIBER LEVEL
                Do While nSubscriberCounter <= nSubscribers

                    nHlCounter = nHlCounter + 1
                    nHlDependentParent = nHlCounter
                       
                    Dim sDateRange As String
                    Dim sHLCount As String
                    
                    sDateRange = Format(txtPatientDateStart(nSubscriberCounter - 1).Text, "YYYYMMDD") & "-" & Format(txtPatientDateEnd(nSubscriberCounter - 1).Text, "YYYYMMDD")
                    nDependents = txtNoDependent(nSubscriberCounter - 1).Text
                    
                    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) = "0"
                    Else
                        oSegment.DataElementValue(4) = "0"
                    End If
                    
                    
                    Set oSegment = oTransactionset.CreateDataSegment("HL\DMG")
                    oSegment.DataElementValue(1) = "D8"
                    oSegment.DataElementValue(2) = Format(txtInsuredDOB(nSubscriberCounter - 1).Text, "YYYYMMDD")
                    oSegment.DataElementValue(3) = txtInsuredSex(nSubscriberCounter - 1).Text
                    
                    Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\NM1")
                    If nDependents = 0 Then
                        oSegment.DataElementValue(1) = "QC"
                    Else
                        oSegment.DataElementValue(1) = "IL"
                    End If
                    oSegment.DataElementValue(2) = "1"
                    oSegment.DataElementValue(3) = txtInsuredLastname(nSubscriberCounter - 1).Text
                    oSegment.DataElementValue(4) = txtInsuredFirstname(nSubscriberCounter - 1).Text
                    oSegment.DataElementValue(8) = "MI"
                    oSegment.DataElementValue(9) = txtInsuredID(nSubscriberCounter - 1).Text
                    
                    Set oSegment = oTransactionset.CreateDataSegment("HL\TRN\TRN")
                    oSegment.DataElementValue(1) = "1"
                    oSegment.DataElementValue(2) = txtPatientTraceNo(nSubscriberCounter - 1).Text
                    
                    Set oSegment = oTransactionset.CreateDataSegment("HL\TRN\REF")
                    oSegment.DataElementValue(1) = "BLT"
                    oSegment.DataElementValue(2) = "111"
                    
                    Set oSegment = oTransactionset.CreateDataSegment("HL\TRN\AMT")
                    oSegment.DataElementValue(1) = "T3"
                    oSegment.DataElementValue(2) = txtPatientTotalCharges(nSubscriberCounter - 1).Text
                    
                    Set oSegment = oTransactionset.CreateDataSegment("HL\TRN\DTP")
                    oSegment.DataElementValue(1) = "232"
                    oSegment.DataElementValue(2) = "RD8"
                    oSegment.DataElementValue(3) = sDateRange
            
                     If nDependents = 0 Then
                     
                        'SVC Service Information
                        Set oSegment = oTransactionset.CreateDataSegment("HL\TRN\SVC\SVC")
                        oSegment.DataElementValue(1, 1) = "AD"     'Product/Service ID Qualifier
                        oSegment.DataElementValue(1, 2) = "CD"     'Product/Service ID
                        oSegment.DataElementValue(2) = "820"      'Monetary Amount
    
                        'REF - REFERENCE IDENTIFICATION
                        Set oSegment = oTransactionset.CreateDataSegment("HL\TRN\SVC\REF")
                        oSegment.DataElementValue(1) = "FJ"      'Reference Identification Qualifier
                        oSegment.DataElementValue(2) = "78"      'Reference Identification
    
                        'DTP - DATE OR TIME OR PERIOD
                        Set oSegment = oTransactionset.CreateDataSegment("HL\TRN\SVC\DTP")
                        oSegment.DataElementValue(1) = "472"      'Date/Time Qualifier
                        oSegment.DataElementValue(2) = "RD8"      'Date Time Period Format Qualifier
                        oSegment.DataElementValue(3) = "19970219-19971103"      'Date Time Period
                        
                    End If
                    
                    
                    '*************************************************************************************************
                    'DETAIL DEPENDENT LEVEL
                    Do While nDependentCounter <= nDependents

                        nHlCounter = nHlCounter + 1
                
                        'DEPENDENT
                        Set oSegment = oTransactionset.CreateDataSegment("HL\HL")
                        oSegment.DataElementValue(1) = nHlCounter
                        oSegment.DataElementValue(2) = nHlDependentParent
                        oSegment.DataElementValue(3) = "23"
                        
                        Set oSegment = oTransactionset.CreateDataSegment("HL\DMG")
                        oSegment.DataElementValue(1) = "D8"
                        oSegment.DataElementValue(2) = Format(txtDependentDOB.Text, "YYYYMMDD")
                        oSegment.DataElementValue(3) = txtDependentSex.Text
                        
                        Set oSegment = oTransactionset.CreateDataSegment("HL\NM1\NM1")
                        oSegment.DataElementValue(1) = "QC"
                        oSegment.DataElementValue(2) = "1"
                        oSegment.DataElementValue(3) = txtDependentLastname.Text
                        oSegment.DataElementValue(4) = txtDependentFirstname.Text
                        oSegment.DataElementValue(8) = "MI"
                        oSegment.DataElementValue(9) = txtDependentID.Text
                        
                        Set oSegment = oTransactionset.CreateDataSegment("HL\TRN\TRN")
                        oSegment.DataElementValue(1) = "1"
                        oSegment.DataElementValue(2) = txtDependentTraceNo.Text
                        
                        Set oSegment = oTransactionset.CreateDataSegment("HL\TRN\REF")
                        oSegment.DataElementValue(1) = "1K"
                        oSegment.DataElementValue(2) = txtDependentClaimNo.Text
                        
                        Set oSegment = oTransactionset.CreateDataSegment("HL\TRN\REF(2)")
                        oSegment.DataElementValue(1) = "BLT"
                        oSegment.DataElementValue(2) = "131"
                        
                        Set oSegment = oTransactionset.CreateDataSegment("HL\TRN\AMT")
                        oSegment.DataElementValue(1) = "T3"
                        oSegment.DataElementValue(2) = txtDependentCharges.Text
                        
                        'SVC Service Information
                        Set oSegment = oTransactionset.CreateDataSegment("HL\TRN\SVC\SVC")
                        oSegment.DataElementValue(1, 1) = "AD"     'Product/Service ID Qualifier
                        oSegment.DataElementValue(1, 2) = "CD"     'Product/Service ID
                        oSegment.DataElementValue(2) = "820"      'Monetary Amount

                        'REF - REFERENCE IDENTIFICATION
                        Set oSegment = oTransactionset.CreateDataSegment("HL\TRN\SVC\REF")
                        oSegment.DataElementValue(1) = "FJ"      'Reference Identification Qualifier
                        oSegment.DataElementValue(2) = "78"      'Reference Identification

                        'DTP - DATE OR TIME OR PERIOD
                        Set oSegment = oTransactionset.CreateDataSegment("HL\TRN\SVC\DTP")
                        oSegment.DataElementValue(1) = "472"      'Date/Time Qualifier
                        oSegment.DataElementValue(2) = "RD8"      'Date Time Period Format Qualifier
                        oSegment.DataElementValue(3) = "19970219-19971103"      'Date Time Period
                                                
                        nDependentCounter = nDependentCounter + 1
                    Loop    'nDependents
                    
                    nSubscriberCounter = nSubscriberCounter + 1
                Loop    'nSubscribers
                                    
                nServiceProviderCounter = nServiceProviderCounter + 1
            Loop    'nServiceProviders
                
            nInfoReceiverCounter = nInfoReceiverCounter + 1
        Loop    'nInfoReceivers
    
        nInfoSourceCounter = nInfoSourceCounter + 1
    Loop    'nInfoSources
    
    'save edi document object to file
    oEdiDoc.Save sEdiFile
    
    Me.MousePointer = vbNormal
    MsgBox ("Done. File created = " & sPath & "276Output.x12")
    cmdGenerate.Enabled = False
    
End Sub