vba - Text Reader class

AshleyKitsune

New Member
Joined
Nov 30, 2021
Messages
12
Office Version
  1. 2016
Platform
  1. Windows
tldr version:

Trying to fix someone else's code who isn't here anymore. This hasn't been working for over a year and I have been tasked to fix it.

This is a class called cbTextReader, and is called to handle the lines of a text file for checking and manipulation. I've dug into what's happening when it's being used, and it appears to be reading the entire text file as one line of text, and so it instantly thinks it's reached the end of the file.

VBA Code:
Option Compare Database
Option Explicit

Private FILE_HANDLE As Long
Private PREV_LINE_POS As Long
'

Private Sub Class_Terminate()
    If FILE_HANDLE > 0 Then
        CloseFile
    End If
End Sub

Public Function OpenFile(TextFileName As String) As Boolean
    FILE_HANDLE = FreeFile
    
    Open TextFileName For Input As #FILE_HANDLE
End Function

Public Sub CloseFile()
    Close #FILE_HANDLE
    
    FILE_HANDLE = 0
End Sub

Public Sub GoBack()
    Seek #FILE_HANDLE, PREV_LINE_POS
End Sub

Public Function GetNextLine() As String
    PREV_LINE_POS = Seek(FILE_HANDLE)
    Line Input #FILE_HANDLE, GetNextLine
End Function

Public Function PeekNextLine() As String
    PeekNextLine = Me.GetNextLine
    Me.GoBack
End Function

Public Property Get EndOfFile() As Boolean
    EndOfFile = EOF(FILE_HANDLE)
End Property

This is the snippet where it is being used:

VBA Code:
    Dim textFile As cbTextReader
    Dim curLine As String     ' the current text line's text

    Set textFile = New cbTextReader
    
    textFile.OpenFile FilePRD
    
    Do While Not textFile.EndOfFile    ' Supposed to loop through the text file one line at a time
        curLine = textFile.GetNextLine               ' until it reaches the end of the file
        MsgBox curLine                           'me testing the values of the line and the value of the end of file method.
        MsgBox textFile.EndOfFile           ' curLine returned the entire string of the file instead of just one line
                                                             'EndOfFile returned True when I expected False since there are well over 100 lines in the file.

The person who created this moved on to bigger and better things, and I'm new here. I'm also the person in the office with the most experience with programming and vba in general, though it's mostly just dabbling whenever I get the chance.
I've looked into the Seek function and I'm wondering if perhaps it had an update that caused the class to work incorrectly. If anyone has any ideas on what I need to look into it would help me out. It took me forever to troubleshoot this database just to get to this point, and now I don't see why it's working the way that it is.
~Ashley
 
Ok, so you guys are probably going to laugh. Considering the conversation we had yesterday, I decided to do something different. Instead of getting the PRD file from the users, I went to the source and found that there is two ways to retrieve the file. One, you can Download it directly to your computer. The other, you can "View" the file in a web browser. I first downloaded the file, tested it, and the database / functions worked like they were supposed to. So I viewed the file, COPIED THE TEXT and pasted it over the data in the file I had downloaded, and then tried again, and it went back to reading the entire text file as one line.

So I think that about wraps it up for this one. Hopefully this is the only problem with the database. I just shake my head thinking about this one.
Thank you everyone,
~Ashley
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Glad it's sorted and thanks for the follow-up (y)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top