Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

limiting access to a file open in a text editor

Status
Not open for further replies.

natashaXXX

Programmer
Sep 13, 2002
13
GB
I am creating a text editor using vb6.0. This has a mdiform with child forms. When a text file is opened in the text editor I want to ensure that no other program can open this file or that the text editor cannot open this file again. While it is possible to use lock and unlock between the open and close statements when using FileOpen or FileSave, I still need the file locked out to other users when the text file is on view in the editor.

Any ideas?
 
I haven't used FileSystemObjects but using the old syntax you can lock a file to stop anyone else opening it:

Code:
iFNum=FreeFile()
Open sMyFile For Input Lock Read As #iFNum

Hope that helps

Daren
Must think of a witty signature
 
So I could have five child forms open at the same time, all opened with

Open sMyFile For Input Lock Read As #iFNum

and not use

close #iFNum until I actually exit the file. Or if i amend one of the files and decide to save but not exit the file I would need to

close
open
print

but again not use the close statement until I actually want to exit the file.
 
Do you mean have the same file open in each of the 5 child forms? You wouldn't be able to do that as the first child form would have exclusive access to the file. If you wanted to have the file in each of the 5 windows you would have to copy the contents of the first window to the other windows.

You could certainly have a different file open in in each of the 5 child windows.

To be able to save the file I think you would have to close it and re-open it for writing:
Code:
Open sMyFile For Output Lock Read As #iFNum

You could make a copy the original file as a backup.

I don't know if the FileSystemObjects have a more sophisticated way of handling files?

Hope this helps

Daren
Must think of a witty signature
 
Thanks.
My main interest is making sure only one copy of a text file can be open at a time, tho' other files can be open.

I did try using just the open statement, instead of the open statement and close statement when opening a file, and then for saving, using
close statement
open statement
print statement,

and when exiting the file using close statement.

But I found when using mdi form with several child forms open, and changing the active form and then trying to save, for some reason when I used

close #f

and then

open .... #f

the program would close #f, but then wouldn't open..#f, error would be generated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top