I want to check if we should close backend database after every SELECT / INSERT / UPDATE OR can leave it open and should close when user close the application.
I'm checking if all databases are available using below code. If any of database is not available it'll prompt the user.
I'm checking if all databases are available using below code. If any of database is not available it'll prompt the user.
Code:
If SecuredDB.ConnectSecuredDatabase() = False Then
iOk = MsgBox("Unable to open Secured Database", vbCritical, "Secured Database not found")
cmdLogin.Visible = False
Exit Sub
End If
Code:
Function is used to connect local ACCESS Database and extract database information from local database and assign True value to global variable
Public Function ConnectSecuredDatabase() As Boolean
Dim sDBNAME As String
Dim sDBPassword As String
On Error GoTo ErrConnectDB
ConnectSecuredDatabase = False
'Global Vairable "gsAccessSecDBPath" will contain the Secured MS Access Database file name
sDBNAME = GetDBPath() & "\" & GetDBName()
sDBPassword = GetDBPassword()
'The DBEngine property of the Application object represents the Microsoft Jet database engine.
'The DBEngine object is the top-level object in the Data Access Objects (DAO) model and it contains
'and controls all other objects in the hierarchy of Data Access Objects.
'Use the Workspace object to manage the current session or to start an additional session.
'When you first refer to or use a Workspace object, you automatically create the default workspace, DBEngine.Workspaces(0).
' Create Microsoft Jet Workspace object.
Set gCurWS = DBEngine.Workspaces(0)
Set gsSecuredDatabase = gCurWS.OpenDatabase(sDBNAME, False, False, "MS Access;PWD=" & sDBPassword)
ConnectSecuredDatabase = True
gsSecuredDatabaseOpened = True 'Assign TRUE once MS Access Secured Database is Initialised
Exit Function
ErrConnectDB:
gsDBErr = Err.Description
End Function
Code:
'Disconnect Secured Access Database
Public Sub DisconnectSecuredDatabase()
If gsSecuredDatabaseOpened = True Then 'If MS Access Secured Database is opened
gsSecuredDatabase.Close
gsSecuredDatabaseOpened = False
End If
Set gsSecuredDatabase = Nothing
End Sub