'This is just an example program to show how to translate an EDI X12 277 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 oSegment As Fredi.ediDataSegment

Private Sub cmdTranslate_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 sHLCount As String
    Dim sHLParent As String
    Dim sHLLevel As String
    Dim nProviderCount As Integer
    Dim nPatientCount As Integer
    Dim sDateRange As String
    
    Dim nArea As Integer
    Dim sLoopSection As String
    Dim sSegmentID As String
    
    Me.MousePointer = vbHourglass
    
    sPath = Trim(App.Path) & "\"
    sSefFile = sPath & "277_X093.SEF"
    sEdiFile = sPath & "277Output.x12"
    
    'instantiate edi document object
    Set oEdiDoc = New Fredi.ediDocument
    
    'change cursor type to forwardonly to save RAM and improve performance
    oEdiDoc.CursorType = Cursor_ForwardOnly
    
    'disable standard reference library to save RAM and improve performance
    Set oSchemas = oEdiDoc.GetSchemas
    oSchemas.EnableStandardReference = False
    
    'Load SEF file
    oEdiDoc.ImportSchema sSefFile, 0
    
    'Load EDI file
    oEdiDoc.LoadEdi sEdiFile
    
    'Get first data segment
    Set oSegment = oEdiDoc.FirstDataSegment
    
    'disable standard reference library to save RAM and improve performance
    Do While Not oSegment Is Nothing
    
        'Identify segments by their Area, Loop section and ID
        nArea = oSegment.Area
        sLoopSection = oSegment.LoopSection
        sSegmentID = oSegment.ID
        
        If nArea = 1 Then
            If sLoopSection = "" Then
                If sSegmentID = "ST" Then
                    nProviderCount = -1
                    nPatientCount = -1
                End If
            End If
        
        ElseIf nArea = 2 Then
            If sSegmentID = "HL" Then
                sHLCount = oSegment.DataElementValue(1)
                sHLParent = oSegment.DataElementValue(2)
                sHLLevel = oSegment.DataElementValue(3) 'get the value of HL Level to identify the different instances of the HL loop
            End If
            
            If sHLLevel = "20" Then 'information source
                If sLoopSection = "HL;NM1" Then
                    If sSegmentID = "NM1" Then
                        txtPayerName.Text = oSegment.DataElementValue(3)
                        txtPayerID.Text = oSegment.DataElementValue(9)
                    End If
                End If
            
            ElseIf sHLLevel = "19" Then 'information receiver
                If sLoopSection = "HL;NM1" Then
                    If sSegmentID = "NM1" Then
                        nProviderCount = nProviderCount + 1
                        txtProviderName(nProviderCount).Text = oSegment.DataElementValue(3)
                        txtProviderNo(nProviderCount).Text = oSegment.DataElementValue(9)
                    End If
                End If
            
            ElseIf sHLLevel = "21" Then 'provider of service
                If sLoopSection = "HL;NM1" Then
                    If sSegmentID = "NM1" Then
                        txtReceiverName.Text = oSegment.DataElementValue(3)
                        txtReceiverETIN.Text = oSegment.DataElementValue(9)
                    End If
                End If
            
            ElseIf sHLLevel = "22" Then 'subscriber
                If sHLParent = "3" Then
                    If sLoopSection = "HL" Then
                        If sSegmentID = "DMG" Then
                            nPatientCount = nPatientCount + 1
                            txtPatientDOB(nPatientCount).Text = oSegment.DataElementValue(2)
                            txtPatientSex(nPatientCount).Text = oSegment.DataElementValue(3)
                        End If
                        
                    ElseIf sLoopSection = "HL;NM1" Then
                        If sSegmentID = "NM1" Then
                            txtPatientLastname(nPatientCount).Text = oSegment.DataElementValue(3)
                            txtPatientFirstname(nPatientCount).Text = oSegment.DataElementValue(4)
                            txtPatientID(nPatientCount).Text = oSegment.DataElementValue(9)
                        End If
                        
                    ElseIf sLoopSection = "HL;TRN" Then
                        If sSegmentID = "TRN" Then
                            txtPatientTraceNo(nPatientCount).Text = oSegment.DataElementValue(2)
                            
'                        ElseIf sSegmentID = "AMT" Then
'                            txtPatientTotalCharges(nPatientCount).Text = oSegment.DataElementValue(2)
                            
                        ElseIf sSegmentID = "DTP" Then
                            sDateRange = oSegment.DataElementValue(3)
                            txtPatientDateStart(nPatientCount) = GetStartDate(sDateRange)
                            txtPatientDateEnd(nPatientCount) = GetEndDate(sDateRange)
                            
                        ElseIf sSegmentID = "REF" Then
                            If oSegment.DataElementValue(1) = "1K" Then
                                txtPatientClaimNo(nPatientCount).Text = oSegment.DataElementValue(2)
                            End If
                            
                        ElseIf sSegmentID = "STC" Then
                            txtPatientCatCode(nPatientCount).Text = oSegment.DataElementValue(1, 1)
                            txtPatientStatusCode(nPatientCount).Text = oSegment.DataElementValue(1, 2)
                            txtPatientTotalCharges(nPatientCount).Text = oSegment.DataElementValue(4)
                            txtPatientPaid(nPatientCount).Text = oSegment.DataElementValue(5)
                        End If
                    End If
                    
                ElseIf sHLParent = "6" Then
                    If sLoopSection = "HL" Then
                        If sSegmentID = "DMG" Then
                            txtInsuredDOB.Text = oSegment.DataElementValue(2)
                            txtInsuredSex.Text = oSegment.DataElementValue(3)
                        End If
                        
                    ElseIf sLoopSection = "HL;NM1" Then
                        If sSegmentID = "NM1" Then
                            txtInsuredLastname.Text = oSegment.DataElementValue(3)
                            txtInsuredFirstname.Text = oSegment.DataElementValue(4)
                            txtInsuredID.Text = oSegment.DataElementValue(9)
                        End If
                    End If
                End If
            
            ElseIf sHLLevel = "23" Then 'dependent
                If sLoopSection = "HL" Then
                    If sSegmentID = "DMG" Then
                        txtDependentDOB.Text = oSegment.DataElementValue(2)
                        txtDependentSex.Text = oSegment.DataElementValue(3)
                    End If
                    
                ElseIf sLoopSection = "HL;NM1" Then
                    If sSegmentID = "NM1" Then
                        txtDependentLastname.Text = oSegment.DataElementValue(3)
                        txtDependentFirstname.Text = oSegment.DataElementValue(4)
                        txtDependentID.Text = oSegment.DataElementValue(9)
                    End If
                    
                ElseIf sLoopSection = "HL;TRN" Then
                    If sSegmentID = "TRN" Then
                        txtDependentTraceNo.Text = oSegment.DataElementValue(2)
                        
                    ElseIf sSegmentID = "DTP" Then
                        sDateRange = oSegment.DataElementValue(3)
                        txtDependentDateStart.Text = GetStartDate(sDateRange)
                        txtDependentDateEnd.Text = GetEndDate(sDateRange)
                        
                        
                    ElseIf sSegmentID = "REF" Then
                        If oSegment.DataElementValue(1) = "1K" Then
                            txtDependentClaimNo.Text = oSegment.DataElementValue(2)
                        End If
                        
                    ElseIf sSegmentID = "STC" Then
                        txtDependentCatCode.Text = oSegment.DataElementValue(1, 1)
                        txtDependentStatusCode.Text = oSegment.DataElementValue(1, 2)
                        txtDependentCharges.Text = oSegment.DataElementValue(4)
                        txtDependentPaid.Text = oSegment.DataElementValue(5)
                    End If
                End If
            End If
        End If
    
        'get next data segment
        Set oSegment = oSegment.Next
    Loop
    

    Me.MousePointer = vbNormal
    cmdTranslate.Enabled = False
    MsgBox ("Done")
    
End Sub