Hey everyone how are you doing.
Anyway how would i tell if a text file is already open. like if i have one program that write to the file and another program that does to. how would i be able to tell if it is open.
thanks for the help
Printable View
Hey everyone how are you doing.
Anyway how would i tell if a text file is already open. like if i have one program that write to the file and another program that does to. how would i be able to tell if it is open.
thanks for the help
well for one thing the second program would get an error trying to open it if its already open.. so if you trap the error using error handling.. that is one way you could tell
yeah that is the way i have it but i want to make the 2nd program to keep trying till it can open it. and i couldnt get that to work with trapping the error. i was hoping maybe there was another way to do it
and thanks for the reply
If u open that Document in Notepad for example, u can't see if the document is open... Notepad reads the Textfile in the memory and then directly closes the file again.
actually the program that is opening it jst has 2 buttons one to open and one to close this program is to test the other program. i want to be able to detect when another vbprogram has the file open and the second one wants to write to the text file.
anyone? please jst tell me if it is possible then tell how and if not jst say no its not possible. thank
Ok...
VB Code:
1 On error goto ErrSub: open... close... msgbox "Opened" exit sub ErrSub: Err.Clear DoEvents ' so u don't freeze? Goto 1
Good luck,
still giving me an error. it give me the error and i have no time to close the file.
well maybe its not possible
:mad: :(
Are you using freefile?
no im not i dont need to i dont think.
What's the error you're receiving?
Run-time Error 70:
Permission Denied
and i know it cause the file is already open too cause i have another program that is running and it has the file open. and i got all the permissions set to work for me cause that was a problem this morning.
Is there no way to check if a file is open. if there is please tell me if not then jst say "no there isnt". and i will drop this question. and try something else. thanks
When you open the file in either place, open it as #1.Quote:
Originally posted by big_k105
Is there no way to check if a file is open. if there is please tell me if not then jst say "no there isnt". and i will drop this question. and try something else. thanks
then you can always tell if it is currently open with:
VB Code:
if FreeFile = 1 then 'File is not open currently Else 'File is already open end if
Of course this isnt the "normal" function of FreeFile, but you could take advantage of it in this way.
will that work if i open it in two completely different programs
sorry ... scratch that ... I just realized you said it is a second app that has it open ...Quote:
Originally posted by Muddy
When you open the file in either place, open it as #1.
then you can always tell if it is currently open with:
VB Code:
if FreeFile = 1 then 'File is not open currently Else 'File is already open end if
Of course this isnt the "normal" function of FreeFile, but you could take advantage of it in this way.
ok, im i stuck using "On Error". i hope not but thats what its looking like.
and if i am stucking using that is there a way to specify to do something on a specified error and then do something else on a different error. if they are in the same sub.
I think trapping an error is the only way. Why not reduce it to an intuitive function though? ieQuote:
Originally posted by big_k105
ok, im i stuck using "On Error". i hope not but thats what its looking like.
and if i am stucking using that is there a way to specify to do something on a specified error and then do something else on a different error. if they are in the same sub.
VB Code:
Public Function FileIsOpen(strFileName) As Boolean Dim i As Integer On Error GoTo errtrap FileIsOpen = False i = FreeFile Open strFileName For Input As #1 Close #1 Exit Function errtrap: FileIsOpen = True End Function
usage:
If FileIsOpen("C:\Test.txt") then
msgbox "it is open"
end if
thanks alot muddy. that worked great on the forum load.