Checking Access Database Properties using VBA

Recently I had a need to set up some properties for a database I've been working on. Rather than doing it the easy way (i.e. setting them manually), I decided to write a few procedures to check and set different properties. Note that these are the file properties (Title, Subject, Author, Manager, Company, etc) found by going to File » Database Properties, not the properties found at Tools » Startup. The difference being that database properties are part of the Properties Collection, whereas the file properties are part of the Documents Collection.

All of the following procedures are based on the code found here (originally from the MS newsgroups but I couldn't find a proper link).

Check if a file property already exists

Function FilePropertyExists(propertyName As String) As Boolean

  Dim dbs As DAO.Database
  Dim cnt As DAO.Container
  Dim doc As DAO.Document
  Dim prp As DAO.Property

  Set dbs = GetCurrentDB
  Set cnt = dbs.Containers!Databases
  Set doc = cnt.Documents!SummaryInfo
  Set prp = doc.Properties(propertyName)

  If Not prp Is Nothing Then
    FilePropertyExists = True
  End If

End Function

If it exists, set it

Function SetFileProperty(propertyName As String, _
    propertyValue As String)

  Dim dbs As DAO.Database
  Dim cnt As DAO.Container
  Dim doc As DAO.Document
  Dim prp As DAO.Property

  Set dbs = GetCurrentDB
  Set cnt = dbs.Containers!Databases
  Set doc = cnt.Documents!SummaryInfo
  Set prp = doc.Properties(propertyName)

  If Not prp Is Nothing Then
    prp.Value = propertyValue
  End If

End Function

Otherwise, add it and set its value

Function AddFileProperty(propertyName As String, _
     propertyType As DataTypeEnum, propertyValue As String)

  Dim dbs As Database
  Dim cnt As Container
  Dim doc As Document
  Dim prp As Property

  Set dbs = GetCurrentDB
  Set cnt = dbs.Containers!Databases
  Set doc = cnt.Documents!SummaryInfo
  Set prp = _
    doc.CreateProperty(propertyName, propertyType, propertyValue)

  doc.Properties.Append prp

End Function

And whenever we want, check the value if we need it

Function GetFileProperty(propertyName As String) As String

  Dim dbs As DAO.Database
  Dim cnt As DAO.Container
  Dim doc As DAO.Document
  Dim prp As DAO.Property

  Set dbs = GetCurrentDB
  Set cnt = dbs.Containers!Databases
  Set doc = cnt.Documents!SummaryInfo
  Set prp = doc.Properties(propertyName)

  GetFileProperty = prp.Value

End Function

Related Articles:

About JP

I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space to learn more about VBA. Keep Reading »

Share This Article:

Share and bookmark this articledelicious buttonfacebook buttonlinkedin buttonstumbleupon buttontwitter button
Comments on this article are closed. Why?

Site last updated: February 12, 2012