- '---------------------------------------------------------------------------------------
- ' Procedure : CreateDBStrProp
- ' Purpose : Create a Custom Database Property of dbText (string) type
- ' Arguments : strPropName As String-the Property Name
- ' : strPropValue As String-the Property Value
- '---------------------------------------------------------------------------------------
- Function CreateDBStrProp(strPropName As String, strPropValue As String) As Boolean
- On Error GoTo Err_CreateDBStrProp
- Dim db As DAO.Database
- Dim prp As Property
- Set db = DBEngine(0)(0)
- ' First we verify the Property Exists to avoid an error
- If ExistsDBProperty(strPropName) = False Then
- Set prp = db.CreateProperty(strPropName, dbText, strPropValue)
- db.Properties.Append prp
- Else
- Set prp = db.Properties(strPropName)
- prp.Value = strPropValue
- MsgBox "DBProperty " & strPropName & " already exists. " _
- & vbCrLf & vbCrLf & "Property value was set." _
- , vbExclamation
- End If
- CreateDBStrProp = True
- Exit_CreateDBStrProp:
- Set prp = Nothing
- Set db = Nothing
- Exit Function
- Err_CreateDBStrProp:
- CreateDBStrProp = False
- MsgBox "Error " & Err.Number & " (" & Err.Description & ")" & _
- " In procedure CreateDBStrProp"
- Resume Exit_CreateDBStrProp
- End Function
Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
Custom Database Properties Creation and Use
From Wiki
How to Create and Use Custom DAO Database Properties
Custom Database Properties can be very useful. They "Persist", which means they remain when the database has been closed and opened again. They are like Global Variables that never go away until you delete them. They are handy for keeping your Version or Copyright data among other uses. You may also want to to use them to conceal some sensitive data from the user.
Perhaps more importantly, if you want to use code to set some startup properties, you have to create specific Custom Database Properties. For example to hide the Database Window, disable Access Special Keys, Allow Built-In Toolbars, set the Application Title, Application Icon, or set the Startup Form you have to create a Custom Database Property.
The Custom Database Properties we are using are DAO Workspaces Properties. That is why we use Set db = DBEngine(0)(0). The (0)(0) represents Workspaces(0).Databases(0).
There are three properties you must set when you create a Custom DAO Workspaces Property. They are Name, Type, and Value. The Type will be Boolean for some startup properties, for others it will be String. For our purposes we will be using the String Type.
The following Functions allow you to create, read, set, and delete Custom Database Properties.
Sample Custom Database Functions
Create a Custom Database Property
To Create a Custom Database Property we must first define the Property's Name, Type, and Value using the CreateProperty Method. Then we use the Append Method to add it to the DAO Workspaces Collection.
Get the Value of a Custom Database Property
- '---------------------------------------------------------------------------------------
- ' Procedure : GetDBPropValue
- ' Purpose : Returns the Property Value of the named property(strPropName)
- ' Arguments : strPropName As String-the Property Name
- '---------------------------------------------------------------------------------------
- Function GetDBPropValue(strPropName As String) As Variant
- On Error GoTo Err_GetDBPropValue
- Dim db As DAO.Database
- Dim prp As Property
- Set db = DBEngine(0)(0)
- If ExistsDBProperty(strPropName) = False Then
- MsgBox "DBProperty """ & strPropName & """ does not exist. " _
- , vbExclamation
- Else
- Set prp = db.Properties(strPropName)
- GetDBPropValue = prp.Value
- Debug.Print GetDBPropValue
- End If
- Exit_GetDBPropValue:
- Set prp = Nothing
- Set db = Nothing
- Exit Function
- Err_GetDBPropValue:
- MsgBox "Error " & Err.Number & " (" & Err.Description & ")" & _
- " In procedure GetDBPropValue"
- Resume Exit_GetDBPropValue
- End Function
List All Custom Database Properties and Values
When you create a Custom Database Property it is added to the Collection of "BuiltIn" Database Properties. If your code produces a list of Database Properties it will show both the Built-In Properties and your Custom Properties. There is another collection of Custom Database properties called "UserDefined" Properties. You have to use different code to list these Properties.
- '---------------------------------------------------------------------------------------
- ' Procedure : ListDBPropsAll
- ' Purpose : List all "BuiltIn" and Custom Database Properties-but not "UserDefined"
- '---------------------------------------------------------------------------------------
- Function ListDBPropsAll()
- On Error Resume Next ' An error occurs whenever this is run
- Dim db As DAO.Database
- Dim i As Long
- Set db = DBEngine(0)(0)
- For i = 0 To db.Properties.Count - 1
- If db.Properties(i).Name = "DesignMasterID" Then
- ' Causes an error or long delay so skip it
- Else
- Debug.Print db.Properties(i).Name _
- & " = " & db.Properties(i).Value
- End If
- Next i
- Exit_ListDBProps:
- Set db = Nothing
- Exit Function
- End Function
Set the Value of a Custom Database Property
You will probably at some point need to change your Property's Value. This is how you can do it.
- '---------------------------------------------------------------------------------------
- ' Procedure : SetDBPropValue
- ' Purpose : Set the string Property Value of the named property(strPropName)
- ' Arguments : strPropName As String-the name of the Property
- ' : varPropValue As Variant-the Property Value to be set
- '---------------------------------------------------------------------------------------
- Function SetDBPropValue(strPropName As String, varPropValue As Variant) As Boolean
- On Error GoTo Err_SetDBPropValue
- Dim db As DAO.Database
- Dim prp As Property
- Set db = DBEngine(0)(0)
- If ExistsDBProperty(strPropName) = True Then
- Set prp = db.Properties(strPropName)
- prp.Value = varPropValue
- Else
- Set prp = db.CreateProperty(strPropName, dbText, varPropValue)
- db.Properties.Append prp
- MsgBox "DBProperty " & strPropName & " has been created. " _
- & vbCrLf & vbCrLf & "Property value was set." _
- , vbExclamation
- End If
- If prp.Value = varPropValue Then
- SetDBPropValue = True
- Else
- SetDBPropValue = False
- End If
- Debug.Print prp.Name & " = " & prp.Value
- Exit_SetDBPropValue:
- Set prp = Nothing
- Set db = Nothing
- Exit Function
- Err_SetDBPropValue:
- MsgBox "Error " & Err.Number & " (" & Err.Description & ")" & _
- " In procedure SetDBPropValue"
- Resume Exit_SetDBPropValue
- End Function
Delete a Custom Database Property
- '---------------------------------------------------------------------------------------
- ' Procedure : DBPropDelete
- ' Purpose : Delete a Custom Database Property-"Built In" Database Properties cannot
- ' : be deleted. A custom error message will display if attempted.
- ' Arguments : strPropName As String-the Name of the Property to be deleted
- '---------------------------------------------------------------------------------------
- Function DBPropDelete(strPropName As String) As Boolean
- On Error GoTo Err_DBPropDelete
- Dim db As DAO.Database
- Set db = DBEngine(0)(0)
- If ExistsDBProperty(strPropName) = True Then
- db.Properties.Delete strPropName
- Else
- MsgBox "Property does not exist and cannot be deleted. " _
- , vbExclamation
- DBPropDelete = False
- GoTo Exit_DBPropDelete
- End If
- If ExistsDBProperty(strPropName) = False Then
- DBPropDelete = True
- Else
- DBPropDelete = False
- End If
- Exit_DBPropDelete:
- Set db = Nothing
- Exit Function
- Err_DBPropDelete:
- Debug.Print (Err.Description & " " & Err.Number & _
- " In procedure DBPropDelete")
- If Err.Number = 3384 Then
- MsgBox " An error has occurred." _
- & vbCrLf & vbCrLf & "Built in DB Properties Cannot be Deleted " _
- , vbExclamation, " Operation Failed"
- End If
- Resume Exit_DBPropDelete
- End Function
Determine if a Custom Database Property Exists
This code is very helpful to avoid an error should you try to access a property that does not exist.
- '---------------------------------------------------------------------------------------
- ' Procedure : ExistsDBProperty
- ' Purpose : Determine if a Database Property Exists
- ' Arguments : strPropName As String-the Property Name
- ' Example : If ExistsDBProperty("MyProperty") = True Then . . .
- '---------------------------------------------------------------------------------------
- Function ExistsDBProperty(strPropName As String) As Boolean
- On Error Resume Next
- Dim db As DAO.Database
- Dim prp As DAO.Property
- Set db = DBEngine(0)(0)
- Set prp = db.Properties(strPropName)
- If Not prp Is Nothing Then
- ExistsDBProperty = True
- Else
- ExistsDBProperty = False
- End If
- Set prp = Nothing
- Set db = Nothing
- End Function
Create the StartupForm Custom Database Property
- '---------------------------------------------------------------------------------------
- ' Procedure : CreateStartupFormProp
- ' Purpose : In order to add, delete, or change the Starup Form with code you must
- ' : first create a custom property named "StartupForm" As dbTExt
- ' Arguments : Optional strFormName As String - The name of the Form - Optional
- '---------------------------------------------------------------------------------------
- Function CreateStartupFormProp(Optional strFormName As String) As Boolean
- On Error GoTo Err_CreateStartupFormProp
- Dim db As DAO.Database
- Dim prp As Property
- Set db = DBEngine(0)(0)
- If ExistsDBProperty("StartupForm") = False Then
- If Len(strFormName & vbNullString) = 0 Then
- Set prp = db.CreateProperty("StartupForm", dbText)
- db.Properties.Append prp
- MsgBox " The StartupForm Property was Created. " _
- & vbCrLf & vbCrLf & _
- " There was no startup Form name. A startup form was not set. "
- Else
- Set prp = db.CreateProperty("StartupForm", dbText, strFormName)
- db.Properties.Append prp
- MsgBox " The Property already exists. " _
- & vbCrLf & vbCrLf & _
- " There was no startup Form name. A startup form was not set. "
- CreateStartupFormProp = False
- GoTo Exit_CreateStartupFormProp
- End If
- Else
- If Len(strFormName & vbNullString) = 0 Then
- MsgBox " The StartupForm Property already exists. " _
- & vbCrLf & vbCrLf & _
- " There was no startup Form name. A startup form was not set. "
- CreateStartupFormProp = False
- GoTo Exit_CreateStartupFormProp
- Else
- Set prp = db.Properties("StartupForm")
- prp.Value = strFormName
- MsgBox "DBProperty " & "StartupForm" & " already exists. " _
- & vbCrLf & vbCrLf & "The startup Form was set." _
- , vbExclamation
- CreateStartupFormProp = False
- GoTo Exit_CreateStartupFormProp
- End If
- End If
- CreateStartupFormProp = True
- Exit_CreateStartupFormProp:
- Set prp = Nothing
- Set db = Nothing
- Exit Function
- Err_CreateStartupFormProp:
- CreateStartupFormProp = False
- MsgBox "Error " & Err.Number & " (" & Err.Description & ")" & _
- " In procedure CreateStartupFormProp"
- Resume Exit_CreateStartupFormProp
- End Function
Create a Custom Database Version Property
- '---------------------------------------------------------------------------------------
- ' Procedure : CreateAppVersionProp(strVersion As String)
- ' Purpose : Create a Custom Database Version Property named "AppVersion"
- ' Arguments : strVersion As String-The Version of the Application
- ' Example : Call CreateAppVersionProp("Version 1.0")
- '---------------------------------------------------------------------------------------
- Function CreateAppVersionProp(strVersion As String) As Boolean
- On Error GoTo Err_CreateAppVersionProp
- Dim db As DAO.Database
- Dim prp As Property
- Set db = DBEngine(0)(0)
- If ExistsDBProperty("AppVersion") = False Then
- Set prp = db.CreateProperty("AppVersion", dbText, strVersion)
- db.Properties.Append prp
- Else
- Set prp = db.Properties("AppVersion")
- prp.Value = strVersion
- MsgBox "DBProperty AppVersion already exists. " _
- & vbCrLf & vbCrLf & "Version value was set." _
- , vbExclamation
- End If
- CreateAppVersionProp = True
- Exit_CreateAppVersionProp:
- Set prp = Nothing
- Set db = Nothing
- Exit Function
- Err_CreateAppVersionProp:
- CreateAppVersionProp = False
- MsgBox "Error " & Err.Number & " (" & Err.Description & ")" & _
- " In procedure CreateAppVersionProp"
- Resume Exit_CreateAppVersionProp
- End Function
Set the Value of the Custom Database Version Property
- '---------------------------------------------------------------------------------------
- ' Procedure : SetAppVersion(strVersion As String)
- ' Purpose : Set the Version of the AppVersion Property
- ' Arguments : strVersion As String-The Version of the Application
- ' Example : Call SetAppVersion("Version 2.1")
- '---------------------------------------------------------------------------------------
- Function SetAppVersion(strVersion As String) As Boolean
- On Error GoTo Err_SetAppVersion
- Dim db As DAO.Database
- Dim prop As Property
- Set db = DBEngine(0)(0)
- If ExistsDBProperty("AppVersion") = True Then
- Set prop = db.Properties("AppVersion")
- prop.Value = strVersion
- Else
- Set prop = db.CreateProperty("AppVersion", dbText, strVersion)
- db.Properties.Append prop
- MsgBox "DBProperty AppVersion was created. " _
- & vbCrLf & vbCrLf & "Version value was set." _
- , vbExclamation
- End If
- Debug.Print prop.Name & " = " & prop.Value
- SetAppVersion = True
- Exit_SetAppVersion:
- Set prop = Nothing
- Set db = Nothing
- Exit Function
- Err_SetAppVersion:
- SetAppVersion = False
- MsgBox "Error " & Err.Number & " (" & Err.Description & ")" & _
- " In procedure SetAppVersion"
- Resume Exit_SetAppVersion
- End Function
Create a Custom Database Copyright Property
- '---------------------------------------------------------------------------------------
- ' Procedure : CreateCopyright(strStartYear as String, strOwner As String)
- ' Purpose : Create and set Copyright Property
- ' Arguments : strStartYear as String-Example: "2006"
- ' : strOwner As String-Company or Owner Name
- '---------------------------------------------------------------------------------------
- Function CreateCopyright(strStartYear As String, strOwner As String) As Boolean
- On Error GoTo Err_CreateCopyright
- Dim db As DAO.Database
- Dim prp As Property
- Dim strValue As String
- Set db = DBEngine(0)(0)
- strValue = "(C) " & strStartYear & "-" & Year(Now) & " " & strOwner
- If ExistsDBProperty("Copyright") = True Then
- Set prp = db.Properties("Copyright")
- prp.Value = strValue
- MsgBox "Copyright Property already exists. " _
- & vbCrLf & vbCrLf & "Copyright value was set." _
- , vbExclamation
- Else
- Set prp = db.CreateProperty("Copyright", dbText, strValue)
- db.Properties.Append prp
- End If
- Debug.Print prp.Name & " = " & prp.Value
- CreateCopyright = True
- Exit_CreateCopyright:
- Set prp = Nothing
- Set db = Nothing
- Exit Function
- Err_CreateCopyright:
- CreateCopyright = False
- MsgBox "Error " & Err.Number & " (" & Err.Description & ")" & _
- " In procedure CreateCopyright"
- Resume Exit_CreateCopyright
- End Function
Set the Custom Database Copyright Property Value
- '---------------------------------------------------------------------------------------
- ' Procedure : SetCopyright(strStartYear as String, strOwner As String)
- ' Purpose : Update Copyright Property
- ' Arguments : strStartYear as String-Example: "2006"
- ' : strOwner As String-Company or Owner Name
- '---------------------------------------------------------------------------------------
- Function SetCopyright(strStartYear As String, strOwner As String) As Boolean
- On Error GoTo Err_SetCopyright
- Dim db As DAO.Database
- Dim prp As Property
- Dim strValue As String
- Set db = DBEngine(0)(0)
- strValue = "(C) " & strStartYear & "-" & Year(Now) & " " & strOwner
- If ExistsDBProperty("Copyright") = False Then
- Set prp = db.CreateProperty("Copyright", dbText, strValue)
- db.Properties.Append prp
- MsgBox "Copyright Property was created. " _
- & vbCrLf & vbCrLf & "Copyright value was set." _
- , vbExclamation
- Else
- Set prp = db.Properties("Copyright")
- prp.Value = strValue
- End If
- Debug.Print prp.Name & " = " & prp.Value
- SetCopyright = True
- Exit_SetCopyright:
- Set prp = Nothing
- Set db = Nothing
- Exit Function
- Err_SetCopyright:
- SetCopyright = False
- MsgBox "Error " & Err.Number & " (" & Err.Description & ")" & _
- " In procedure SetCopyright"
- Resume Exit_SetCopyright
- End Function
As you can see you can use these Properties for a variety of needs. If you have a very important Global Variable you cannot afford to loose you can always save it as a Custom Database Property.
You can learn more about working with Custom Database Properties at http://msdn.microsoft.com/en-us/library/bb258165.aspx
You can learn more about setting Database Startup Properties with code at http://msdn.microsoft.com/en-us/library/aa200349.aspx You can also search VBA Help for "Startup Properties."
Submitted by hunterpaw



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.