What are the disadvantages to referring to a file by an actual number instead of using FreeFile and storing the result in a variable that I then use to refer to the text file?
I encountered a problem appending to a text file that may have been fixed by getting rid of my FreeFile variable and using the number "1"...
This is the code:
FileNum = FreeFile
Open App.Path & "\EXAMPLE.log" For Append Shared As #FileNum
Print #FileNum, "THIS IS WHAT I PRINT"
Close #FileNum
For some reason, an error is raised now on the line beginning with "Open" (55: "File is already open."
. I don't know how this could happen, since the lock-type is "Shared." Anyway, I opened up the immediate window at this point and typed "Close 1" (at this point, both FileNum and FreeFile were equal to 2). After this, ?FreeFile returned 1 (which kind of makes sense -- but this isn't what i'm interested in).
What is weird is that I had completely lost a handle on the original file that was *somehow* still open. In a case like this I would want the error-handling code to do:
If err.number = 55 Then
Close FileNum
Resume
'then i should be able to open the file like normal
Else
'other stuff (probably exit sub)
End If
But obviously this won't work since FileNum is (inherently -- since FreeFile wouldn't have grabbed a file that was 'already open') not the same as the number for the file that is already open.
So here are my questions:
Is it bad to use an actual number to refer to that file? As long as I don't do any other file writing in my app I should be fine, eh?
If I do use FreeFile, how do I deal with errornum 55???
Thanks again, guys&gals for your help. -Brad
ps Any idea how the file didn't close in the first place?
I encountered a problem appending to a text file that may have been fixed by getting rid of my FreeFile variable and using the number "1"...
This is the code:
FileNum = FreeFile
Open App.Path & "\EXAMPLE.log" For Append Shared As #FileNum
Print #FileNum, "THIS IS WHAT I PRINT"
Close #FileNum
For some reason, an error is raised now on the line beginning with "Open" (55: "File is already open."
What is weird is that I had completely lost a handle on the original file that was *somehow* still open. In a case like this I would want the error-handling code to do:
If err.number = 55 Then
Close FileNum
Resume
'then i should be able to open the file like normal
Else
'other stuff (probably exit sub)
End If
But obviously this won't work since FileNum is (inherently -- since FreeFile wouldn't have grabbed a file that was 'already open') not the same as the number for the file that is already open.
So here are my questions:
Is it bad to use an actual number to refer to that file? As long as I don't do any other file writing in my app I should be fine, eh?
If I do use FreeFile, how do I deal with errornum 55???
Thanks again, guys&gals for your help. -Brad
ps Any idea how the file didn't close in the first place?