Option Explicit
'The connection object for the database, used throughout the application
Public objConn As ADODB.Connection
'A function to create the database connection
'-Requires a path to the database (strPath)
'-Requires a password for the database (strDatabasePassword)
'-Returns true if connection made
'-Returns false if no connection as database does not exist
Function ConnectToDatabase(ByVal strPath As String, _
ByVal strDatabasePassword As String) As Boolean
'Change the pointer to an hourglass
Screen.MousePointer = vbHourglass
'If the database exists
If Dir(strPath) <> "" Then
'Make objConn a new instance of the ADODB.Connection class
Set objConn = New ADODB.Connection
'Define the connection string for the database
'-Microsoft Jet 4.0 is the Provider
'-The database path is passed as an argument to the function
'-Access mode is not limited to prevent errors
'-The user connects as Admin to allow any changes to be made
objConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strPath & ";" & _
"Mode=Share Deny None;" & _
"User ID=Admin;"
'The password for the database is passed as an argument to the function
objConn.Properties("Jet OLEDB:Database Password") = strDatabasePassword
'Open the connection object
objConn.Open
'Connection made
ConnectToDatabase = True
Else
'If the database does not exist, return false
ConnectToDatabase = False
End If
'Set the pointer back to normal
Screen.MousePointer = vbDefault
End Function
'Routine to disconnect from the database
Sub DisconnectFromDatabase()
'Check that the connection is not already closed
If objConn.State <> adStateClosed Then
'Close the connection
objConn.Close
End If
'Set it to nothing
Set objConn = Nothing
End Sub