error handling

  • Thread starter Thread starter Alan
  • Start date Start date
A

Alan

I am trying to test for the presence of a Network drive by
locating a file on it. I would like different forms opened
depending on the presence of the Network.
However the problem is that I cannot get the error handler
to deal with the error message when I deliberately set the
code to find a non-existant file. The runtime 53 error
message comes up and the code stops.
I have listed the sample code here, and obviously I have
made a simple error, but as yet I am not able to find it,
despite looking through the helpfiles and Tech knowledge
base- please make me feel foolish!! ;-)

Sub helper()
On Err GoTo helper_err
Dim result As Integer
FileLen "T:/Fantasy.xls"
If result <> -1 Then
MsgBox "File found"
DoCmd.OpenForm "Network Tasks"
End If
Exit Sub
helper_err:
MsgBox "You are not attached to the Network"
DoCmd.OpenForm "Local Updates Only"
End Sub
 
I am trying to test for the presence of a Network drive by
locating a file on it.

Use the FileSystemObject of the Scripting model and you can look at them
directly.

Tim F
 
I don't really want to look at them, just test for their
presence- this is to create an "on-line" and "off-line"
behaviour mode in a C drive database with regard to
updating network based databases.
There is no help file entry for FileSystemObject, so if
this really is the best way, and I can't test a file and
handle the error, can you give me some more information
on how to achieve- either by code example or by reference
to articles?
 
Your code does not look right. Try:

If Dir("T:/Fantasy.xls") <>"" Then
msgbox "FileFound"
Else
msgbox "You are not attached to the network"
end if

This won't create an error if the file isn't there.
 
I don't really want to look at them, just test for their
presence- this is to create an "on-line" and "off-line"
behaviour mode in a C drive database with regard to
updating network based databases.

The FSO gets over a problem with Dir() when pointed at a non-existent
drive, because it causes a non-trappable (system level) error. The FSO has
a Drives collection, that you can scan without actually accessing the
hardware.
There is no help file entry for FileSystemObject, so if
this really is the best way, and I can't test a file and
handle the error, can you give me some more information
on how to achieve- either by code example or by reference
to articles?
The help file is available on the Microsoft web site, and any book or
article on VBS scripting will detail it well. In the meantime, here is one
I prepared earlier:... Sorry: the commenting is crap but I wrote it for
practise only, and I think it's fairly self explanatory.

Hope it helps


Tim F



' testing the drives collection and the dictionary object
Option Explicit


EnumerateDrives

If CreateObject("Scripting.FileSystemObject").DriveExists("S:") Then
MsgBox "Drive S: already exists"

Else

Dim net

Set net = CreateObject("WScript.Network")
net.MapNetworkDrive "S:", "\\brundle\datadrive"

EnumerateDrives

End If

'============================================================

Sub EnumerateDrives()

Dim dict

Dim fso, drv

Dim w, arrKeys, strKey, arrDrv, strOutput

Set dict = CreateObject("Scripting.Dictionary")

Set fso = CreateObject("Scripting.FileSystemObject")

For Each drv In fso.Drives
dict.Add drv.DriveLetter, Array(drv.Path, _
DriveTypeName( drv.DriveType ), _
IsReadyString( drv.IsReady ) )

Next

arrKeys = dict.Keys()

For w = 0 to dict.Count - 1
If Len(strOutput) > 0 Then strOutput = strOutput & vbCrLf
strKey = arrKeys(w)
arrDrv = dict.Item(strKey)
strOutput = strOutput & arrDrv(0) & _
" [" & arrDrv(1) & "] " & _
vbTab & arrDrv(2)

Next

WScript.Echo strOutput

End Sub

Function DriveTypeName( wDriveType )

Dim a_strType

a_strType = Array("Unknown", "Removable", "Fixed", _
"Network", "CD ROM", "RAM disk" )

If -1 < wDriveType And wDriveType < 6 Then
DriveTypeName = a_strType( wDriveType )

Else
DriveTypeName = "Error"

End If

End Function

Function IsReadyString( fIsReady )

If fIsReady Then
IsReadyString = "Ready"

Else
IsReadyString = "Not Ready"

End If

End Function
 
Back
Top