Detect if connected to the network

R

Rod

I have an Access vba applications that picks up a small text file from
across a local network. If the user is not connected to the network I want
to do something else.

The trouble is that if I try and get the file, or even just check that it is
there by using FileSystemObjects the user's computer spends a long time
trying to access the network, when it fails I can catch the error. However
by then the user has got bored and probably assumed the program has crashed.

Is there a way in VBA of quickly detecting if you are connected to the
network, of if a particular network drive is available.

Thanks in advance.

Rod
 
R

Rod

Thanks this looks to work well. Not sure what happens if there is a LAN but
it is not connected to the Internet.
 
J

John Spencer

Is the file stored on drive somewhere on the network? You can check for
it's existence using the DIR function. That is pretty quick

If Len(Dir("J:\shared\spencer\ICD9.mdb")) = 0 then
'No such file available - although the network could be up
Else
'The file exists and is available
End IF


You can also check for a directory existing or even a mapped drive
IF Len(Dir("R:",vbDirectory)) = 0 then

Else

End If
--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 
K

Karl E. Peterson

John Spencer said:
Is the file stored on drive somewhere on the network? You can check for
it's existence using the DIR function. That is pretty quick

Just buttin' in to say the commonly accepted practice is to use Attrib() rather than
Dir() for simple file existence checks, because using Dir releases any existing open
search handle that Dir may have created elsewhere in the application. (Kinda like
using FreeFile rather than hardcoding a specific file handle.) Ex:

Public Function FileExists(ByVal FileName As String) As Boolean
Dim nAttr As Long
' Grab this files attributes, and make sure it isn't a folder.
' This test includes cases where file doesn't exist at all.
On Error GoTo NoFile
nAttr = GetAttr(FileName)
If (nAttr And vbDirectory) <> vbDirectory Then
FileExists = True
End If
NoFile:
End Function

Public Function FolderExists(ByVal PathName As String) As Boolean
Dim nAttr As Long
' Grab the attributes, test valid values for folder bit.
On Error GoTo NoFolder
nAttr = GetAttr(PathName)
If (nAttr And vbDirectory) = vbDirectory Then
FolderExists = True
End If
NoFolder:
End Function

Carry on...
 
R

Rod

Thanks, that's neat.


Karl E. Peterson said:
Just buttin' in to say the commonly accepted practice is to use Attrib()
rather than Dir() for simple file existence checks, because using Dir
releases any existing open search handle that Dir may have created
elsewhere in the application. (Kinda like using FreeFile rather than
hardcoding a specific file handle.) Ex:

Public Function FileExists(ByVal FileName As String) As Boolean
Dim nAttr As Long
' Grab this files attributes, and make sure it isn't a folder.
' This test includes cases where file doesn't exist at all.
On Error GoTo NoFile
nAttr = GetAttr(FileName)
If (nAttr And vbDirectory) <> vbDirectory Then
FileExists = True
End If
NoFile:
End Function

Public Function FolderExists(ByVal PathName As String) As Boolean
Dim nAttr As Long
' Grab the attributes, test valid values for folder bit.
On Error GoTo NoFolder
nAttr = GetAttr(PathName)
If (nAttr And vbDirectory) = vbDirectory Then
FolderExists = True
End If
NoFolder:
End Function

Carry on...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top