Reading from a Text File Backwards...

LaBam

Registered User.
Local time
Today, 22:00
Joined
Mar 21, 2002
Messages
48
I need help here. I'm writing a program to be able to read a text file. I have been able to open and read the file correctly thanks to the search utility in this forum.

However, I want it to read the previous line whenever a condition is not met. Could someone please show me the code for this.

Here is part of my code. I want the system to move back to the previous line in the text file whenever the Else case is executed. I have tried "Input #-1, MyLine" but it is not working.

Code:
Private Sub cmdRead_Click()
    On Error GoTo Read_Err
    
    Dim MyLine As String
    Dim tIMSI As String
    Dim tMSISDN As String
    Dim tVID As String
    Dim tCAT As String
    Dim tTBS1 As String
    Dim tTBS2 As String
   
    Dim rst As Recordset
    
    Set rst = CurrentDb.OpenRecordset("tblHLRDUMP", dbOpenDynaset)
    Open "C:\HLRDUMP20050604.txt" For Input As #1
    Input #1, MyLine
    Do While Not EOF(1)
        'This checks if it's the start of a new profile
        If Mid(Trim(MyLine), 2, 8) = "SUBBEGIN" Then
            'Skips the first line
            Input #1, MyLine 'line 1
            'Starts storing values for current profile
                If Left(MyLine, 4) = "IMSI" Then
                    tIMSI = Mid(MyLine, 6, 45)
                Else
                    Input #-1, MyLine
                End If
            Input #1, MyLine 'line 2
                If Left(MyLine, 6) = "MSISDN" Then
                    tMSISDN = Mid(MyLine, 8, 45)
                Else
                    Input #-1, MyLine
                End If
            Input #1, MyLine 'line 3
                If Left(MyLine, 3) = "VID" Then
                    tVID = Mid(MyLine, 5, 45)
                Else
                    Input #-1, MyLine
                End If
            Input #1, MyLine 'line 4
                If Left(MyLine, 3) = "CAT" Then
                    tCAT = Mid(MyLine, 5, 45)
                Else
                    Input #-1, MyLine
                End If
            Input #1, MyLine ' line 5
                If Left(MyLine, 3) = "TBS" Then
                    tTBS1 = Mid(MyLine, 5, 45)
                Else
                    Input #-1, MyLine
                End If
            Input #1, MyLine ' line 6
                If Left(MyLine, 3) = "TBS" Then
                    tTBS2 = Mid(MyLine, 5, 45)
                Else
                    Input #-1, MyLine
                End If
            'Now it stores into the database
            With rst
                .AddNew
                rst("IMSI") = tIMSI
                rst("MSISDN") = tMSISDN
                rst("VID") = tVID
                rst("CAT") = tCAT
                rst("TBS1") = tTBS1
                rst("TBS2") = tTBS2
                .Update
            End With
                tIMSI = ""
                tMSISDN = ""
                tVID = ""
                tCAT = ""
                tTBS1 = ""
                tTBS2 = ""
        Else
            Input #1, MyLine
        End If
        Loop
    Close #1
    rst.Close

MsgBox "All records read and loaded successfully.", vbInformation, "HLR Profile Reader"

Exit_Read:
    Exit Sub

Read_Err:
    MsgBox Err.Description, vbOKOnly, "HLR Profile Reader"
    Resume Exit_Read

End Sub
 
How about just saving the prior line instead?
just create a var call it PriorLine, and before reading the new line into the variable, copy the old line to priorline and you have it. Less resouorces needed than reading a line from a file again.
 
Thanks for the quick response. You see, the thing is I want the cursor to move to the previous line in the text file only when the current condition is met. If the condition is not met, it should stay with the current line.
 
Well typically a text file is a forward only file. So if you only need to look at the last record, you can store it and look at it when you are required.
Maybe if you explain why you need the prior record it would be of benefit.
 

Users who are viewing this thread

Back
Top Bottom