Module: JetDatabase in Category Access/Jet Databases : Database from Total Visual SourceBook

Creating and setting primary Access database characteristics in VBA and VB6.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the modJetDatabase module.
CreateNewDatabase Procedure Create a new database with options to specify the database format, adding a password and whether it should be encrypted. If the Access MDB is opened with a system workgroup, the new database automatically inherits those security settings.
DatabaseCompactGeneral Procedure Compact the specified database. Note that Access cannot compact the current database from VBA code. Set the database's Compact on Close option to do that.
DatabaseCompactBasic Procedure Compact a standard database that is not under workgroup security to itself.
DatabaseCompactNew Procedure Compact a standard database that is not under workgroup security to a new database name.
DatabaseDecrypt Procedure Decrypts the named database or a copy of the database.
DatabaseEncrypt Procedure Encrypts the named database to itself or to a new database.
DatabasePasswordChange Procedure Change the password of a database or create a copy of the database with the new password.
DatabasePasswordRemove Procedure Remove the password for a database or create a copy of the database without a database password.
ArchiveAccessObjects Procedure Create archival copies of all objects from the source database into a new database. This procedure uses the DAO CreateDatabase method to create a new empty database. It then uses the TransferDatabase action to copy the objects.
ArchiveJetTables Procedure Create archival copies of all tables in the database into a new database. This is similar to the ArchiveAccessObjects() function except that it only copies tables and their data.
FileOverwrite Procedure See if a file exists, and if so, optionally prompt the user to delete it, then deletes it.
RefreshContainers Procedure Refreshes all DAO containers in the specified database. Because of the way DAO maintains its containers, you should generally refresh a container before relying on the accuracy of its contents. This function handles refreshing all DAO containers, and optionally (if the fAccessContainers parameter is set to True) the Forms, Reports, Scripts (Macros), and Modules containers.
DatabasePropertyExists Procedure Does this Jet/DAO database property exist?
DatabasePropertyGet Procedure Get the value of a Jet/DAO database property.
DatabasePropertySet Procedure Set a Jet/DAO database property value.
DatabasePropertyCreate Procedure Create a new Jet/DAO database property.
Database_AllowBypassKey Procedure Set the database's AllowBypassKey property. If it's set to False, users can't hold the shift key down when the database loads to prevent the startup routines from running.
' Example of modJetDatabase
'
' To use this example:
' 1. Create a new form.
' 2. Create the following command buttons, and set their "On Click" event to :
'     cmdCreateDB
'     cmdCompact
'     cmdEncrypt
'     cmdDecrypt
'     cmdAddPwd
'     cmdRemovePwd
'     cmdArchiveObjs
'     cmdArchiveTbls
'     cmdDelDB
'     cmdPropExists
'     cmdPropVal
'     cmdCreateProp
'     cmdSetProp
'     cmdBypass
' 3. Paste the entire contents of this module into the new form's module.
' 4. Paste the entire contents of this module into the new form's module.
' 5. Adjust the constant mcstrDBName to be the location of the database that you want to create and work with.
'    Optionally change the constant mfACCDB, to use ACCDB database format.

Private Const mcstrDBName As String = "C:\Total Visual SourceBook 2013\Samples\mytest.mdb"
Private Const mcstrBypass As String = "C:\Total Visual SourceBook 2013\Samples\bypass.mdb"
Private Const mfACCDB As Boolean = False

Private Sub cmdBypass_Click()
  ' Set the database's AllowBypassKey property.  If it's set to False, users can't hold the shift key down when the database loads to prevent the startup routines from running.

  Dim lngResponse As VbMsgBoxResult
  Dim dbs As DAO.Database

  lngResponse = MsgBox("Allow the user to hold the Shift key to bypass startup routines in the " & mcstrBypass & " database?", vbYesNoCancel)

  If lngResponse <> vbCancel Then
    Set dbs = OpenDatabase(mcstrBypass, Options:=False, ReadOnly:=False)

    Select Case lngResponse
      Case vbYes
        If Database_AllowBypassKey(dbs, True) Then
          MsgBox "AllowBypassKey set to True."
        End If
      Case vbNo
        If Database_AllowBypassKey(dbs, False) Then
          MsgBox "AllowBypassKey set to False."
        End If
      Case vbCancel
        MsgBox "No change made to AllowBypassKey"
    End Select

    dbs.Close
    Set dbs = Nothing
  End If

End Sub

Private Sub cmdCreateDB_Click()
  ' Create Database
  Dim strError As String

  If mfACCDB Then
    ' Create a new database in Access ACCDB format (2007 or later)
    strError = CreateNewDatabase(mcstrDBName, "2007", "", False, True)
    If strError = "" Then
      MsgBox "New database successfully created"
    Else
      MsgBox "New database could not be created: " & strError
    End If
  Else
    ' Create a new database in Access 2000 MDB format which works with all Access versions after 2000
    strError = CreateNewDatabase(mcstrDBName, "2000", "", False, True)
    If strError = "" Then
      MsgBox "New database successfully created"
    Else
      MsgBox "New database could not be created: " & strError
    End If
  End If
End Sub

Private Sub cmdCompact_Click()
  ' Compact database

  Dim strError As String

  If MsgBox("Compact the database using DatabaseCompactBasic?", vbYesNo) Then
    strError = DatabaseCompactBasic(mcstrDBName, mfACCDB)
    If strError <> "" Then
      MsgBox "DatabaseCompactBasic Failed: " & strError
    End If
  End If

  If MsgBox("Compact the database using DatabaseCompactGeneral?", vbYesNo) Then
    strError = DatabaseCompactGeneral(mcstrDBName, "", mfACCDB, "", "", False, False, False)
    If strError <> "" Then
      MsgBox "DatabaseCompactGeneral Failed: " & strError
    End If
  End If

  If MsgBox("Compact the database using DatabaseCompactNew?", vbYesNo) Then
    strError = DatabaseCompactNew(mcstrDBName, Replace(mcstrDBName, ".", "Compacted."), mfACCDB)
    If strError <> "" Then
      MsgBox "DatabaseCompactNew Failed: " & strError
    End If
  End If

End Sub

Private Sub cmdPropVal_Click()
  ' Retrieve a database property value

  Dim strProperty As String
  Dim varValue As Variant

  strProperty = InputBox("Enter property name to test", , "Name")
  If strProperty <> "" Then
    If DatabasePropertyGet(CurrentDb, strProperty, varValue) Then
      MsgBox "The value of Property " & strProperty & " is: " & varValue
    Else
      MsgBox "Property " & strProperty & " does not exist"
    End If
  End If

End Sub

Private Sub cmdCreateProp_Click()
  ' Create a database property and assign a value to it
  ' This example only sets a property of text type (dbText) but other data types can also be created.

  Dim strProperty As String
  Dim strValue As String

  strProperty = InputBox("Enter a new property name to create")
  If strProperty <> "" Then
    If DatabasePropertyExists(CurrentDb, strProperty) Then
      MsgBox "Property " & strProperty & " already exists"
    Else
      strValue = InputBox("Enter the text value to assign to this property")
      If DatabasePropertySet(CurrentDb, strProperty, strValue, dbText) Then
        MsgBox "Property " & strProperty & " was created and assigned to " & strValue
      Else
        MsgBox "Property " & strProperty & " was not created"
      End If
    End If
  End If

End Sub

Private Sub cmdSetProp_Click()
  ' Set a database property value

  Dim strProperty As String
  Dim varValue As String

  strProperty = InputBox("Enter a property name to change")
  If strProperty <> "" Then
    If DatabasePropertyExists(CurrentDb, strProperty) Then
      varValue = InputBox("Enter the new value for this property")
      If DatabasePropertySet(CurrentDb, strProperty, varValue) Then
        MsgBox "Property " & strProperty & " was assigned"
      Else
        MsgBox "Failed: property " & strProperty & " was not assigned"
      End If
    Else
      MsgBox "Property " & strProperty & " does not exist"
    End If
  End If

End Sub

Private Sub cmdEncrypt_Click()
  ' Encrypt a database

  Dim strError As String

  strError = DatabaseEncrypt(mcstrDBName, "", mfACCDB)
  If strError = "" Then
    MsgBox "Database successfully encrypted"
  Else
    MsgBox "Database could not be encrypted: " & strError
  End If
End Sub

Private Sub cmdDecrypt_Click()
  ' Decrypt a database

  Dim strError As String

  strError = DatabaseDecrypt(mcstrDBName, "", mfACCDB)
  If strError = "" Then
    MsgBox "Database successfully decrypted"
  Else
    MsgBox "Database could not be decrypted: " & strError
  End If
End Sub

Private Sub cmdAddPwd_Click()
  ' Add a Password to a database

  Dim strError As String

  strError = DatabasePasswordChange(mcstrDBName, "", mfACCDB, "", InputBox("Enter the new password:"))
  If strError = "" Then
    MsgBox "Password successfully added"
  Else
    MsgBox "Password unsuccessfully added: " & strError
  End If
End Sub

Private Sub cmdRemovePwd_Click()
  ' Remove a database's password

  Dim strError As String

  strError = DatabasePasswordRemove(mcstrDBName, "", mfACCDB, InputBox("Enter the current database password:"))
  If strError = "" Then
    MsgBox "Password successfully removed"
  Else
    MsgBox "Password unsuccessfully removed: " & strError
  End If
End Sub

Private Sub cmdArchiveObjs_Click()
  ' Archive Access Objects

  Err.Clear

  ' Archive objects from the current database into the test datbase
  ArchiveAccessObjects CurrentDb, mcstrDBName, False
  If Err.Number = 0 Then
    MsgBox "Objects archived to " & mcstrDBName
  End If
End Sub

Private Sub cmdArchiveTbls_Click()
  ' Archive Jet Tables

  Err.Clear

  ' Archive objects from the current database into the test datbase
  ArchiveJetTables CurrentDb, mcstrDBName, False
  If Err.Number = 0 Then
    MsgBox "Tables archived to " & mcstrDBName
  End If
End Sub

Private Sub cmdDelDB_Click()
  ' See if a file exists, and if so, delete it (with optionally prompt).

  FileOverwrite (mcstrDBName), True
End Sub

Private Sub cmdPropExists_Click()
  ' See if a database property name exists

  Dim strProperty As String

  strProperty = InputBox("Enter property name to test", , "Name")
  If strProperty <> "" Then
    If DatabasePropertyExists(CurrentDb, strProperty) Then
      MsgBox "Property " & strProperty & " exists"
    Else
      MsgBox "Property " & strProperty & " does not exist"
    End If
  End If

End Sub

Private Sub Form_Load()
  Const cintLeft As Integer = 100
  Const cintWidth As Integer = 3000
  Const cintHeight As Integer = 400

  ' Setup controls
  With Me.cmdCreateDB
    .Top = 100
    .Left = cintLeft
    .Width = cintWidth
    .Height = cintHeight
    .Caption = "Create DB"
  End With
  With Me.cmdCompact
    .Top = 600
    .Left = cintLeft
    .Width = cintWidth
    .Height = cintHeight
    .Caption = "Compact DB"
  End With
  With Me.cmdEncrypt
    .Top = 1100
    .Left = cintLeft
    .Width = cintWidth
    .Height = cintHeight
    .Caption = "Encrypt DB"
  End With
  With Me.cmdDecrypt
    .Top = 1600
    .Left = cintLeft
    .Width = cintWidth
    .Height = cintHeight
    .Caption = "Decrypt DB"
  End With
  With Me.cmdAddPwd
    .Top = 2100
    .Left = cintLeft
    .Width = cintWidth
    .Height = cintHeight
    .Caption = "Add Password"
  End With
  With Me.cmdRemovePwd
    .Top = 2600
    .Left = cintLeft
    .Width = cintWidth
    .Height = cintHeight
    .Caption = "Remove Password"
  End With
  With Me.cmdArchiveObjs
    .Top = 3100
    .Left = cintLeft
    .Width = cintWidth
    .Height = cintHeight
    .Caption = "Archive Objs"
  End With
  With Me.cmdArchiveTbls
    .Top = 3600
    .Left = cintLeft
    .Width = cintWidth
    .Height = cintHeight
    .Caption = "Archive Tbls"
  End With
  With Me.cmdDelDB
    .Top = 4100
    .Left = cintLeft
    .Width = cintWidth
    .Height = cintHeight
    .Caption = "Delete Test DB"
  End With
  With Me.cmdPropExists
    .Top = 4600
    .Left = cintLeft
    .Width = cintWidth
    .Height = cintHeight
    .Caption = "Property Exists"
  End With
  With Me.cmdPropVal
    .Top = 5100
    .Left = cintLeft
    .Width = cintWidth
    .Height = cintHeight
    .Caption = "Property Value"
  End With
  With Me.cmdCreateProp
    .Top = 5600
    .Left = cintLeft
    .Width = cintWidth
    .Height = cintHeight
    .Caption = "Create Property"
  End With
  With Me.cmdSetProp
    .Top = 6100
    .Left = cintLeft
    .Width = cintWidth
    .Height = cintHeight
    .Caption = "Set Property"
  End With
  With Me.cmdBypass
    .Top = 6600
    .Left = cintLeft
    .Width = cintWidth
    .Height = cintHeight
    .Caption = "BypassKey"
  End With

End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS