'This is a an example program to demonstrate how one can create an application
'that translates an EDI 837 X098 EDI file using the FREDI component
Option Explicit
Private oEdiDoc As Fredi.ediDocument
Private oSchema As Fredi.ediSchema
Private oSchemas As Fredi.ediSchemas
Private oSegment As Fredi.ediDataSegment
Private oElement As Fredi.ediDataElement

Private Sub cmdTranslate_Click()

    Dim i As Integer
    Dim nCount As Integer
    Dim sPath As String
    Dim sHlEntity As String
    Dim sNm1Entity As String
    Dim sQlfr As String
    Dim nLineCount As Integer
    Dim sSefFile As String
    Dim sEdiFile As String
    Dim sSegmentID As String
    Dim nArea As Integer
    Dim sLoopSection As String
    Dim sValue As String
    Dim bSelfPatient As Boolean
    
    Me.MousePointer = vbHourglass
    
    sPath = Trim(App.Path) & "\"
    sSefFile = "837_X098.SEF"
    sEdiFile = "837.x12"
    
    'instantiate edi document object
    Set oEdiDoc = New Fredi.ediDocument
    
    'don't load entire edi file into RAM memory.  Improves performance significantly
    oEdiDoc.CursorType = Cursor_ForwardOnly
    
    'disables the internal standard reference library to make certain sef files are used only to improve performance.
    Set oSchemas = oEdiDoc.GetSchemas
    oSchemas.EnableStandardReference = False
    
    'load sef file
    oEdiDoc.ImportSchema sPath & sSefFile, 0
    
    'load edi file
    oEdiDoc.LoadEdi sPath & sEdiFile
    
    'get first data segment
    Set oSegment = oEdiDoc.FirstDataSegment
    
    'traverse through edi file a segment at a time
    While Not oSegment Is Nothing
    
        'identifies segment uniquely by its area, the loopsection it is in, and its segment id.
        sSegmentID = oSegment.ID
        nArea = oSegment.Area
        sLoopSection = oSegment.LoopSection
        
        If nArea = 0 Then
            If sSegmentID = "ISA" Then
            ElseIf sSegmentID = "GS" Then
            End If
        
        ElseIf nArea = 1 Then
            If sLoopSection = "" Then
                If sSegmentID = "BHT" Then
                ElseIf sSegmentID = "REF" Then
                End If
                
            ElseIf sLoopSection = "NM1" Then
                If sSegmentID = "NM1" Then
                    sNm1Entity = oSegment.DataElementValue(1)
                End If
                If sNm1Entity = "41" Then
                    If sSegmentID = "NM1" Then  'SUBMITTER
                        txtBillingProviderName.Text = oSegment.DataElementValue(3)
                        txtBillingProviderEDI.Text = oSegment.DataElementValue(9)
                        
                    ElseIf sSegmentID = "PER" Then  'SUBMITTER EDI CONTACT INFORMATION
                        sValue = oSegment.DataElementValue(2)
                        sValue = oSegment.DataElementValue(4)
                        sValue = oSegment.DataElementValue(6)
                    End If
                    
                ElseIf sNm1Entity = "40" Then
                    If sSegmentID = "NM1" Then  'RECEIVER
                        txtReceiverName.Text = oSegment.DataElementValue(3)
                        txtReceiverEDINo.Text = oSegment.DataElementValue(9)
                    End If
                End If
            End If  'sLoopSection
                
        ElseIf nArea = 2 Then
            If sLoopSection = "HL" And sSegmentID = "HL" Then
                sHlEntity = oSegment.DataElementValue(3)
            End If
            If sHlEntity = "20" Then  'BILLING PROVIDER
                If sLoopSection = "HL" Then
                ElseIf sLoopSection = "HL;NM1" Then
                    If sSegmentID = "NM1" Then
                        sNm1Entity = oSegment.DataElementValue(1)
                    End If
                    If sNm1Entity = "85" Then    'BILLING PROVIDER NAME
                        If sSegmentID = "NM1" Then
                            txtBillingProviderName.Text = oSegment.DataElementValue(3)
                            txtBillingProviderTIN.Text = oSegment.DataElementValue(9)
                                                
                        ElseIf sSegmentID = "N3" Then
                            txtBillingProviderAddress.Text = oSegment.DataElementValue(1)
                            
                        ElseIf sSegmentID = "N4" Then
                            txtBillingProviderCity.Text = oSegment.DataElementValue(1)
                            txtBillingProviderState.Text = oSegment.DataElementValue(2)
                            txtBillingProviderZip.Text = oSegment.DataElementValue(3)
                            
                        ElseIf sSegmentID = "REF" Then
                            If oSegment.DataElementValue(1) = "G2" Then
                                txtBillingProviderKeyIns.Text = oSegment.DataElementValue(2)
                            End If
                        End If
                        
                    ElseIf sNm1Entity = "87" Then
                        If sSegmentID = "NM1" Then  'PAY-TO PROVIDER NAME
                            txtPayToName.Text = oSegment.DataElementValue(3)
                            txtPayToTIN.Text = oSegment.DataElementValue(9)
                                                
                        ElseIf sSegmentID = "N3" Then
                            txtPayToAddress.Text = oSegment.DataElementValue(1)
                            
                        ElseIf sSegmentID = "N4" Then
                            txtPayToCity.Text = oSegment.DataElementValue(1)
                            txtPayToState.Text = oSegment.DataElementValue(2)
                            txtPayToZip.Text = oSegment.DataElementValue(3)
                            
                        ElseIf sSegmentID = "REF" Then
                            txtPayToKeyIns.Text = oSegment.DataElementValue(2)
                        End If
                    End If  'sNm1Entity
                End If  'sLoopSection
                
            ElseIf sHlEntity = "22" Then  'SUBSCRIBER
                If sLoopSection = "HL" Then
                    If sSegmentID = "SBR" Then    'SUBSCRIBER INFORMATION
                        If oSegment.DataElementValue(2) = "18" Then 'self
                            bSelfPatient = True
                        End If
                        txtSubscriberGroupNo.Text = oSegment.DataElementValue(3)
                    End If
                    
                ElseIf sLoopSection = "HL;NM1" Then
                    If sSegmentID = "NM1" Then
                        sNm1Entity = oSegment.DataElementValue(1)
                    End If
                    If sNm1Entity = "IL" Then    'SUBSCRIBER NAME
                        If sSegmentID = "NM1" Then
                            txtSubscriberLastname.Text = oSegment.DataElementValue(3)
                            txtSubscriberFirstname.Text = oSegment.DataElementValue(4)
                            txtSubscriberSSN.Text = oSegment.DataElementValue(9)
                            
                        ElseIf sSegmentID = "N3" Then
                            txtSubscriberAddress.Text = oSegment.DataElementValue(1)
                            
                        ElseIf sSegmentID = "N4" Then
                            txtSubscriberCity.Text = oSegment.DataElementValue(1)
                            txtSubscriberState.Text = oSegment.DataElementValue(2)
                            txtSubscriberZip.Text = oSegment.DataElementValue(3)
                            
                        ElseIf sSegmentID = "DMG" Then  'SUBSCRIBER DEMOGRAPHIC INFORMATION
                            txtSubscriberDOB.Text = oSegment.DataElementValue(2)
                            txtSubscriberSex.Text = oSegment.DataElementValue(3)
                            
                        ElseIf sSegmentID = "REF" Then
                            If oSegment.DataElementValue(1) = "SY" Then
                                txtSubscriberSSN.Text = oSegment.DataElementValue(2)
                            End If
                        End If
                        
                    ElseIf sNm1Entity = "PR" Then    ' PAYER NAME
                        If sSegmentID = "NM1" Then
                            txtPayerName.Text = oSegment.DataElementValue(3)
                            txtRenderingTIN.Text = oSegment.DataElementValue(9)
                            
                        ElseIf sSegmentID = "N3" Then
                            txtPayerAddress.Text = oSegment.DataElementValue(1)
                            
                        ElseIf sSegmentID = "N4" Then
                            txtPayerCity.Text = oSegment.DataElementValue(1)
                            txtPayerState.Text = oSegment.DataElementValue(2)
                            txtPayerZip.Text = oSegment.DataElementValue(3)
                            
                        End If
                    End If  'sNm1Entity
                    
                ElseIf sLoopSection = "HL;CLM" And bSelfPatient Then
                    If sSegmentID = "CLM" Then
                        nLineCount = 0
                        sValue = oSegment.DataElementValue(1)
                        sValue = oSegment.DataElementValue(2)
                        
                    ElseIf sSegmentID = "DTP" Then
                        sValue = oSegment.DataElementValue(3)
                        
                    ElseIf sSegmentID = "REF" Then
                    ElseIf sSegmentID = "HI" Then
                    End If  'sSegmentID
                    
                ElseIf sLoopSection = "HL;CLM;NM1" And bSelfPatient Then
                    If sSegmentID = "NM1" Then
                        sNm1Entity = oSegment.DataElementValue(1)
                    End If
                    If sNm1Entity = "82" Then    'RENDERING PROVIDER NAME
                        If sSegmentID = "NM1" Then
                            sValue = oSegment.DataElementValue(3)
                            sValue = oSegment.DataElementValue(4)
                            sValue = oSegment.DataElementValue(9)
                            
                        ElseIf sSegmentID = "PRV" Then
                        End If
                        
                    ElseIf sNm1Entity = "77" Then
                        If sSegmentID = "NM1" Then
                            sValue = oSegment.DataElementValue(3)
                            sValue = oSegment.DataElementValue(9)
                            
                        ElseIf sSegmentID = "N3" Then
                            sValue = oSegment.DataElementValue(1)
                            
                        ElseIf sSegmentID = "N4" Then
                            sValue = oSegment.DataElementValue(1)
                            sValue = oSegment.DataElementValue(2)
                            sValue = oSegment.DataElementValue(3)
                        End If
                   End If   'sNm1Entity
                   
                ElseIf sLoopSection = "HL;CLM;LX" And bSelfPatient Then
                    If sSegmentID = "LX" Then
                        nLineCount = nLineCount + 1
                        
                    ElseIf sSegmentID = "SV1" Then
                        sValue = oSegment.DataElementValue(1, 2)
                        sValue = oSegment.DataElementValue(2)
                        sValue = oSegment.DataElementValue(7, 1)
                        
                    ElseIf sSegmentID = "DTP" Then
                        sValue = oSegment.DataElementValue(3)
                    End If
                End If  'sLoopSection
                
            ElseIf sHlEntity = "23" Then  'DEPENDENT
                If sLoopSection = "HL" Then
                    If sSegmentID = "PAT" Then    'PATIENT INFORMATION
                        sValue = oSegment.DataElementValue(1)
                    End If
                    
                ElseIf sLoopSection = "HL;NM1" Then
                    If sSegmentID = "NM1" Then
                        sNm1Entity = oSegment.DataElementValue(1)
                    End If
                    If sNm1Entity = "QC" Then    'PATIENT NAME
                        If sSegmentID = "NM1" Then
                            txtPatientLastname.Text = oSegment.DataElementValue(3)
                            txtPatientFirstname.Text = oSegment.DataElementValue(4)
                            txtPatientKeyIns.Text = oSegment.DataElementValue(9)
                            
                        ElseIf sSegmentID = "N3" Then
                            txtPatientAddress.Text = oSegment.DataElementValue(1)
                            
                        ElseIf sSegmentID = "N4" Then
                            txtPatientCity.Text = oSegment.DataElementValue(1)
                            txtPatientState.Text = oSegment.DataElementValue(2)
                            txtPatientZip.Text = oSegment.DataElementValue(3)
                            
                        ElseIf sSegmentID = "DMG" Then  'PATIENT DEMOGRAPHIC INFORMATION
                            txtPatientDOB.Text = oSegment.DataElementValue(2)
                            txtPatientSex.Text = oSegment.DataElementValue(3)
                            
                        ElseIf sSegmentID = "REF" Then
                            txtPatientSSN = oSegment.DataElementValue(2)
                        End If
                    End If  'sNm1Entity
                    
                ElseIf sLoopSection = "HL;CLM" Then
                    If sSegmentID = "CLM" Then
                        nLineCount = 0
                        txtPatientAccountNo.Text = oSegment.DataElementValue(1)
                        txtTotalChargeAmount.Text = oSegment.DataElementValue(2)
                        
                    ElseIf sSegmentID = "DTP" Then
                        sQlfr = oSegment.DataElementValue(1)
                        If sQlfr = "938" Then 'Order
                            sValue = oSegment.DataElementValue(3)
                        ElseIf sQlfr = "454" Then 'initial treatment
                            sValue = oSegment.DataElementValue(3)
                        End If
                                                
                    ElseIf sSegmentID = "REF" Then
                    ElseIf sSegmentID = "HI" Then
                    End If  'sSegmentID
                    
                ElseIf sLoopSection = "HL;CLM;NM1" Then
                    If sSegmentID = "NM1" Then
                        sNm1Entity = oSegment.DataElementValue(1)
                    End If
                    If sNm1Entity = "82" Then    'RENDERING PROVIDER NAME
                        If sSegmentID = "NM1" Then
                            txtRenderingLastname.Text = oSegment.DataElementValue(3)
                            txtRenderingFirstname.Text = oSegment.DataElementValue(4)
                            txtRenderingTIN.Text = oSegment.DataElementValue(9)
                            
                        ElseIf sSegmentID = "PRV" Then
                        ElseIf sSegmentID = "REF" Then
                            txtRenderingKeyIns.Text = oSegment.DataElementValue(2)
                        End If
                        
                    ElseIf sNm1Entity = "77" Then
                        If sSegmentID = "NM1" Then
                            txtPayToName.Text = oSegment.DataElementValue(3)
                            txtPayToTIN.Text = oSegment.DataElementValue(9)
                            
                        ElseIf sSegmentID = "N3" Then
                            txtPayToAddress.Text = oSegment.DataElementValue(1)
                            
                        ElseIf sSegmentID = "N4" Then
                            txtPayToCity.Text = oSegment.DataElementValue(1)
                            txtPayToState.Text = oSegment.DataElementValue(2)
                            txtPayToZip.Text = oSegment.DataElementValue(3)
                        End If
                   End If   'sNm1Entity
                   
                ElseIf sLoopSection = "HL;CLM;LX" Then
                    If sSegmentID = "LX" Then
                        nLineCount = nLineCount + 1
                        
                    ElseIf sSegmentID = "SV1" Then
                        txtChargesDesc(nLineCount - 1).Text = oSegment.DataElementValue(1, 2)
                        txtChargesAmount(nLineCount - 1).Text = oSegment.DataElementValue(2)
                        txtChargesDesc(nLineCount - 1).Tag = oSegment.DataElementValue(7, 1)
                        
                    ElseIf sSegmentID = "DTP" Then
                        txtChargeDate(nLineCount - 1).Text = oSegment.DataElementValue(3)
                    End If
                End If  'sLoopSection
            End If
        
        ElseIf nArea = 3 Then
        
        End If
    
        'get next data segment
        Set oSegment = oSegment.Next
    Wend
    
    Me.MousePointer = vbNormal
    MsgBox "Done"
    
End Sub