I have a desktop Windows application that is installed in small office environments.
The application uses an .MDB
database file as its database which is
You should use UNC paths, because not everyone will have your drive mapped to the same letter.
First, I would determine the UNC path of your file as it exists on your local computer at F:\data\db.mdb
using one of the techniques found here:
Basically, you look at the way Windows Explorer lists the network mapped drive, then use this to deduce the UNC path.
Assuming the drive is actually mapped on every local computer that plans to use the application, use the Win32_MappedLogicalDisk class to determine availability of the mapped network drive.
I have some sample code here that can be adapted to determine whether a given network drive is available (scroll down to the Mapped Drives Information section). You check .ProviderName
to match the UNC path, so you know which is the correct drive, then check the value of .Availability
to determine if the mapped network drive can be accessed.
Make sure this script is ran right before the application is started:
net use f: \\machine_name\share /user:[username] [password] /persistent:yes
This will map the share drive on the letter you specified !
You should definitely abandon the network drive mapping possibilities:
Though you are on a domain, I would not advise you to use a name, as computers, for multiple reasons, might not always find it 'easily' on the network, specially when its IP is regularly changed.
You should definitely find a way to assign a fixed IP to your disk: it is the most stable and permanent solution you can think about. Ask your domain administrator to arrange it for you.
Testing the presence of your network disk can then be done very easily. There are multiple solutions, including trying to open directly the mdb file. You could also test the existence of the file (through the file object I think) or even use any external program or windows API that you could launch from your code. Please google 'VB test IP' or something similar to find a solution at your convenience.
EDIT: windows has even a proposal to simulate a ping with some VB code. Check it here
EDIT2: I found in one of my apps this VBA code, which allows to quick-check if a file exists (and can ba accessed) somewhere on your network. It was basically set to test if a new version of the user interface is available.
Function fileIsAvailable(x_nom As Variant) As Boolean
On Error GoTo ERREUR
Application.Screen.MousePointer = 11
Dim fso as object
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(x_nom) Then
fileIsAvailable = False
Else
fileIsAvailable = True
End If
Set fso = Nothing
Application.Screen.MousePointer = 0
On Error GoTo 0
Exit Function
ERREUR:
Application.Screen.MousePointer = 0
debug.print Err.Number, Err.description
End Function
You can easily call this function by supplying your file's network name, such as:
if fileIsAvailable("\\192.168.1.110\myFileName.mdb") then ...
You asked, "Would a solution be to use \machine_name\share instead of drive letters?"
I think, yes, it could be. A UNC path avoids 2 problems:
The unknown is whether anything in your application makes a UNC path for the MDB either a complication or a flat out deal-breaker.
You did not make it clear what your application was written in, however before you attempt to connect to the database for the first time, presumably in a splash screen or something of that nature, check that f:\data\db.mdb exists.