AshleyKitsune
New Member
- Joined
- Nov 30, 2021
- Messages
- 12
- Office Version
- 2016
- Platform
- 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.
This is the snippet where it is being used:
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
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