MS-Access / Getting Started

Ensuring an Application Runs Locally

For certain applications, you may want to specify a requirement that the application be run from the local machine. Such applications might include a split database where a copy of the front-end is available somewhere, but is also installed on the local machine. To prevent users from stumbling across the copy on the server, you might include some code in a startup form that detects where the database is being launched from.

Begin by creating a new form called frmCheckLocal. Set the form properties according to your preferences. The form is used to check the path to the current database and display a message to the user if there is a problem.

There are two checks we need to do to determine whether the file is opened locally. The first is a check to see whether the file is opened from a UNC path. A UNC path will begin with two backslash characters: \\. The other test we'll do is to determine whether the drive letter for the path is a local drive. To do this, we'll use the GetDriveType API function.

Add the following enumeration and declaration to the code behind the form.

Private Enum DriveType
    DRIVE_UNKNOWN = 0
    DRIVE_NO_ROOT_DIR = 1
    DRIVE_REMOVABLE = 2
    DRIVE_FIXED = 3
    DRIVE_REMOTE = 4
    DRIVE_CDROM = 5
    DRIVE_RAMDISK = 6
End Enum
Private Declare Function GetDriveType Lib "kernel32.dll" Alias "GetDriveTypeA" _
  (ByVal lpRootPathName As String) As DriveType

Next, add the following helper routine to determine whether the file is opened locally.

Private Function IsOpenedLocally() As Boolean
    Dim stDrive As String

    stDrive = Left(CurrentProject.FullName, 2)
    If (stDrive = "\\") Then
	IsOpenedLocally = False ' Opened from UNC path
    ElseIf (GetDriveType(stDrive & "\") <> DRIVE_FIXED) Then
	IsOpenedLocally = False
    Else
	IsOpenedLocally = True
    End If
End Function

Using the Load event of the form, call the IsOpenedLocally helper function. If this function returns False, we need to close the application. Because our form contains a label that displays a message to the user, we're setting the TimerInterval property of the form to give the form a chance to display onscreen. If the database is opened locally then we'll open a form called frmStartup.

Private Sub Form_Load()
    If (Not IsOpenedLocally()) Then
	Me.TimerInterval = 5000
    Else
	' // close the form
	DoCmd.Close acForm, Me.Name

	' // Open the startup form
	DoCmd.OpenForm "frmStartup"
    End If
End Sub

Last, we need to define the Timer event that will be called by the form.

Private Sub Form_Timer()
    Application.Quit
End Sub
The code shown here exits the application. The code included with the custom progress bar code to increment the width of the rectangle using the Timer event. The name of the sample database that contains this code is OpenLocalOnly.accdb.
[Previous] [Contents]