Valhalla Legends Forums Archive | Visual Basic Programming | Question: "File Already Open" Error

AuthorMessageTime
DarkOne
I've tried countless methods to try and eliminate this problem such as using the FreeFile function to retrieve available file handles and still got this error.

I have looked over and over all my procedures for opening and closing files and I have closed every file that I originally opened. I have also tried setting procedures to load/save settings from files I am working with sequentially and I am still getting the error.

Any help would be appreciated.
February 27, 2004, 11:42 PM
Tasha
DarkOne
Newbie

Right there says it all Dan :P

And i've read your post like 3 times, wtf are you talking about? -.-

EDIT: :P Nevermind I see what you're talking about. You're trying to open the file, and it says the file is all ready running, but like it doesn't appear anywhere? If that's the case just control alt delete and find the filename you're trying to open, close that and it should work.

If you're talking about the same thing I had problems with last week about opening like most of my programs like aim, winamp, starcraft, I found out it was a virus though and removed it, dunno if it's the same problem though :)
February 28, 2004, 12:11 AM
Stealth
Make sure your code doesn't try to open any files that are already open within your program. ie:

[code]
Open fileX.txt for Input as #f
'do stuff
call OpenNewFile()
Close #f

'in OpenNewFile()
Open fileX.txt for input as #otherf 'this will cause the error, even though
'the file has a new FreeFile number, you have it open already in the calling procedure.
[/code]
February 28, 2004, 12:22 AM
Newby
Here is some coding that helps me.

[code]

Dim FNUM as Integer
FNUM = FreeFile()

Open App.Path & "\Boobies.txt" for Output As #FNUM
Print #FNUM, "( . ) ( . )"
Close #FNUM

[/code]

Try that.
February 28, 2004, 12:38 AM
Dyndrilliac
[quote author=Newby link=board=31;threadid=5489;start=0#msg46427 date=1077928687]
Here is some coding that helps me.

[code]

Dim FNUM as Integer
FNUM = FreeFile()

Open App.Path & "\Boobies.txt" for Output As #FNUM
Print #FNUM, "( . ) ( . )"
Close #FNUM

[/code]

Try that.
[/quote]

You don't need the App.Path & "\Boobies.xt", as the program assumes if you only have the file name it's the same directory as the app anyway, so "Boobies.txt." would work just fine.
February 28, 2004, 1:11 AM
Adron
[quote author=Dyndrilliac link=board=31;threadid=5489;start=0#msg46433 date=1077930692]
You don't need the App.Path & "\Boobies.xt", as the program assumes if you only have the file name it's the same directory as the app anyway, so "Boobies.txt." would work just fine.
[/quote]

I think it will assume the current directory, which may or may not be the same as the application directory.
February 28, 2004, 1:14 AM
K
[quote author=Adron link=board=31;threadid=5489;start=0#msg46435 date=1077930855]
[quote author=Dyndrilliac link=board=31;threadid=5489;start=0#msg46433 date=1077930692]
You don't need the App.Path & "\Boobies.xt", as the program assumes if you only have the file name it's the same directory as the app anyway, so "Boobies.txt." would work just fine.
[/quote]

I think it will assume the current directory, which may or may not be the same as the application directory.
[/quote]

Correct - for example, if you start a program from a shortcut, the working directory is often not the application directory.
February 28, 2004, 1:37 AM
Newby
[quote author=Dyndrilliac link=board=31;threadid=5489;start=0#msg46433 date=1077930692]
[quote author=Newby link=board=31;threadid=5489;start=0#msg46427 date=1077928687]
Here is some coding that helps me.

[code]

Dim FNUM as Integer
FNUM = FreeFile()

Open App.Path & "\Boobies.txt" for Output As #FNUM
Print #FNUM, "( . ) ( . )"
Close #FNUM

[/code]

Try that.
[/quote]

You don't need the App.Path & "\Boobies.xt", as the program assumes if you only have the file name it's the same directory as the app anyway, so "Boobies.txt." would work just fine.
[/quote]

Hehe, I did not know that. I've always used App.Path & Filename. Thanks! :)
February 28, 2004, 1:53 AM
Spht
[quote author=Newby link=board=31;threadid=5489;start=0#msg46442 date=1077933221]
Hehe, I did not know that. I've always used App.Path & Filename. Thanks! :)
[/quote]

You may want to read the posts following that one...
February 28, 2004, 4:24 AM
DarkOne
Stealth, I have ensured that is not happening. Like I said, I even tried opening the files sequentially so one file is being opened/closed in order to prevent two files to open on the same file handle.

Also, Newby I have tried your method and still no sucess.

I'll try to figure it out, there 'must' be something I have overlooked obviously.

I just recently tried assigned unique file handle numbers to each and every file open/close operation in my program and I noticed that I always get an error on the LAST file handle. If I increase the file handle number, the same thing occurs, it says that file is already open.

Any idea?
February 28, 2004, 5:42 AM
o.OV
Here is a possible scenario:

You "Open" a file.
You process a few lines..
and somewhere between "Open" and "Close"..
you have EXIT/GOTO setup that skips "Close".

that might be the cause of your problem :-\
Check your coding for anything resembling that scenario..

You can give it a TEMPORARY fix
by placing "Close" before every "Open".

Add-On:

Give us the code where it errors at.
It would be easier for us to deduce the problem
if we had actual code to examine.
February 28, 2004, 6:22 AM
LoRd
[quote]
You can give it a TEMPORARY fix
by placing "Close" before every "Open".
[/quote]
If the file was already closed it would just give you another error.
February 28, 2004, 6:43 AM
DarkOne
Here I am creating files required to store user's settings and in this case for the sake of debugging, I am loading my 'LoadFilters' function, shown below this snippet.

[code]
Private Sub Form_Load()
Open "Config.txt" For Append As #1
Close #1
Open "UserFilters.txt" For Append As #1
Close #1
Open "TalkFilters.txt" For Append As #1
Close #1

'LoadConfig
LoadFilters
.
.
.
End Sub
[/code]

Here is the 'LoadFilters' function:
[code]
Public Function LoadFilters()
Dim line As String

Open "UserFilters.txt" For Input As #1
Do While Not EOF(1)
Input #1, line
frmFilters.lstUsers.ListItems.Add , , line
Loop
Close #1

Open "TalkFilters.txt" For Input As #1
Do While Not EOF(1)
Input #1, line
frmFilters.lstText.ListItems.Add , , line
Loop
Close #1
End Function
[/code]

When calling the LoadFilters function, I get an error reporting that the "file already open" on line #3 of this function, "Open "UserFilters.txt" For Input As #1".
February 28, 2004, 6:49 AM
Newby
[quote author=LoRd[nK] link=board=31;threadid=5489;start=0#msg46481 date=1077950591]
[quote]
You can give it a TEMPORARY fix
by placing "Close" before every "Open".
[/quote]
If the file was already closed it would just give you another error.
[/quote]

No, that's untrue.
February 28, 2004, 7:26 AM
o.OV
Well Dark.. the code you pasted runs fine.

I started a new project and pasted that code in
and voided out the object items..

Ran it with items in file and without items in file.

It ran fine.

"While Not" should be changed to "Until"
"Public Function LoadFilters()" should be changed to "Public Sub LoadFilters()"

Let's get this straight..
It errors at.. "Open "UserFilters.txt" For Input As #1" ?
I'll make an assumption in your actual code
you have it as:

[code]
LoadConfig
[/code]
and not

[code]
'LoadConfig
[/code]
If so.. then the problem is somewhere in LoadConfig.
February 28, 2004, 12:21 PM
DarkOne
I continued my debugging this morning and found the problem. There was actually a problem in my SaveFilters procedure in which I was referring to a non-existent object. I have resolved this problem by ensuring that these objects are loaded.

I appreciate your help o.OV and everyone else to tried to help!
February 28, 2004, 6:21 PM
Stealth
[quote author=o.OV link=board=31;threadid=5489;start=0#msg46505 date=1077970881]
"While Not" should be changed to "Until"
[/quote]

My CS teacher disagrees. He considers "While Not" better form. Perhaps from his background in C/C++?
February 28, 2004, 8:09 PM
Newby
I use Loop until EOF(1) -_-
February 28, 2004, 8:21 PM
Grok
[code]Do While EOF(FileNum) = False 'or NOT EOF(FileNum)
'
Loop[/code]

That is the form I always use for reading files in VB. Even when using Scripting.TextStream object.

[code]Set TS = fso.OpenTextStream(...)
Do While TS.AtEndOfStream = False
'
Loop[/code]

February 28, 2004, 8:24 PM
DarkOne
Is there any advantage of using one over the other, other than typing less characters? Just a matter of preference?
February 28, 2004, 10:44 PM
Imperceptus
Now would be an excellent opportunity for you to Sharpen up on how to debug a program. Step Into, Step over, variable/expression/control watches and so forth.
February 28, 2004, 11:53 PM
o.OV
[quote author=DarkOne link=board=31;threadid=5489;start=15#msg46536 date=1077992461]
I continued my debugging this morning and found the problem. There was actually a problem in my SaveFilters procedure in which I was referring to a non-existent object. I have resolved this problem by ensuring that these objects are loaded.

I appreciate your help o.OV and everyone else to tried to help!
[/quote]

oh.. strange.
I don't understand object oriented programming very well. :-\
How did a non-existent object cause a file to remain open?
February 29, 2004, 3:16 AM
o.OV
[quote author=Stealth link=board=31;threadid=5489;start=15#msg46553 date=1077998987]
[quote author=o.OV link=board=31;threadid=5489;start=0#msg46505 date=1077970881]
"While Not" should be changed to "Until"
[/quote]

My CS teacher disagrees. He considers "While Not" better form. Perhaps from his background in C/C++?
[/quote]

I did try testing this.

Note:
I use two different projects
because I found the arrangement of code
could actually make a difference in speed.
(slower/faster)
Can someone give an explanation as to why?

Don't forget to compile into an executable ~ !!

Do While Not

[code]

Private Declare Function GetTickCount Lib "kernel32.dll" () As Long
Dim T As Long, X As Long, XX As Long
Private Sub Form_Load()

Unload Me

XX = 100000000
X = 0
T = GetTickCount
Do While Not X
XX = XX - 1
If XX = 0 Then X = -1
Loop
MsgBox "Do While Not " & GetTickCount - T

End Sub

[/code]

and

Do Until

[code]

Private Declare Function GetTickCount Lib "kernel32.dll" () As Long
Dim T As Long, X As Long, XX As Long
Private Sub Form_Load()

Unload Me

XX = 100000000
X = 0
T = GetTickCount
Do Until X
XX = XX - 1
If XX = 0 Then X = -1
Loop
MsgBox "Do Until " & GetTickCount - T

End Sub

[/code]
February 29, 2004, 4:11 AM
DarkOne
[quote author=o.OV link=board=31;threadid=5489;start=15#msg46635 date=1078024562]
[quote author=DarkOne link=board=31;threadid=5489;start=15#msg46536 date=1077992461]
I continued my debugging this morning and found the problem. There was actually a problem in my SaveFilters procedure in which I was referring to a non-existent object. I have resolved this problem by ensuring that these objects are loaded.

I appreciate your help o.OV and everyone else to tried to help!
[/quote]

oh.. strange.
I don't understand object oriented programming very well. :-\
How did a non-existent object cause a file to remain open?
[/quote]

I referred to the object while the file was open, but I don't quite understand why it didn't prompt me about the object. Odd.
February 29, 2004, 5:54 AM
o.OV
[quote author=DarkOne link=board=31;threadid=5489;start=15#msg46669 date=1078034066]
I referred to the object while the file was open, but I don't quite understand why it didn't prompt me about the object. Odd.
[/quote]

Are you using "On Error .." ?
February 29, 2004, 6:45 AM
DarkOne
Nope.

If there are errors, I like to see what they are and fix them. I don't usually like to depend on "On Error Resume Next".
February 29, 2004, 9:14 AM
SPY-3
if you called a on error goto event where it opens the file then add Close#1 or w/e ur integer or number is
August 27, 2004, 1:47 PM

Search