Open files and recordsets

  • Thread starter Thread starter Tcs
  • Start date Start date
Hi Tom,

How can I check to see if a text file is open?

One way is to try to open it exclusively yourself. Air code:

Function TextFileOpen(FileSpec as String) As Boolean
If Len(Dir(FileSpec)) > 0 Then 'make sure file exists
lngFileNum = FreeFile()
On Error Resume Next
Open FileSpec For Input Lock Read Write As #lngFileNum
If Err.Number = 0 Then 'file opened successfully
TextFileOpen = False
Close #lngFileNum
Else 'file could not be opened
TextFileOpen = True
End If
On Error GoTo 0
End If
End Function

As well as checking that the file exists, you may also want to check the
permissions on it; if you don't, the function will misleadingly return
True when the file isn't actually open but the current user doesn't have
permission to open it.

Also, remember that the fact that a text file is "open" in an editor
such as Notepad does not mean that the file is open from the operating
system's point of view. When Notepad (and many others) "open" a text
file, they open the actual disk file, read its contents into memory, and
immediately close it. The actual editing is done in memory and the file
on disk isn't touched until you save your work.

And a DAO recordset?

Try to read a property of the recordset and trap any resulting error. If
you can read a property such as RecordCount the recordset is open. If
the recordset has never been opened or has been closed and the object
variable set to nothing, AFAIK you'll get Error 91. If the recordset has
been opened and closed but the object variable is still pointing at the
closed recordset, you'll get Error 3420.
 
Thanks a lot!

Hi Tom,



One way is to try to open it exclusively yourself. Air code:

Function TextFileOpen(FileSpec as String) As Boolean
If Len(Dir(FileSpec)) > 0 Then 'make sure file exists
lngFileNum = FreeFile()
On Error Resume Next
Open FileSpec For Input Lock Read Write As #lngFileNum
If Err.Number = 0 Then 'file opened successfully
TextFileOpen = False
Close #lngFileNum
Else 'file could not be opened
TextFileOpen = True
End If
On Error GoTo 0
End If
End Function

As well as checking that the file exists, you may also want to check the
permissions on it; if you don't, the function will misleadingly return
True when the file isn't actually open but the current user doesn't have
permission to open it.

Also, remember that the fact that a text file is "open" in an editor
such as Notepad does not mean that the file is open from the operating
system's point of view. When Notepad (and many others) "open" a text
file, they open the actual disk file, read its contents into memory, and
immediately close it. The actual editing is done in memory and the file
on disk isn't touched until you save your work.



Try to read a property of the recordset and trap any resulting error. If
you can read a property such as RecordCount the recordset is open. If
the recordset has never been opened or has been closed and the object
variable set to nothing, AFAIK you'll get Error 91. If the recordset has
been opened and closed but the object variable is still pointing at the
closed recordset, you'll get Error 3420.
 
Back
Top