Title | Use DAO to set, get, and delete database properties |
Description | This example shows how to use DAO to set, get, and delete database properties in Visual Basic 6. |
Keywords | DAO, database, property, database property, database properties |
Categories | Database |
|
|
To set a property value, the program first uses the Database object's Properties collection to see if the property already exists. If it does, the program sets its new value.
If the property does not exist, the program creates it and appends it to the database's Properties collection.
|
|
Private Sub cmdSet_Click()
Dim db As DAO.Database
Dim new_property As DAO.Property
' Open the database.
Set db = DBEngine.Workspaces(0).OpenDatabase( _
txtDatabase.Text, ReadOnly:=False)
' See if the property is already present.
On Error Resume Next
Set new_property = db.Properties(txtPropertyName.Text)
If Err.Number = 0 Then
On Error GoTo 0
' Set the new value.
new_property.Value = txtPropertyValue.Text
Else
On Error GoTo 0
' Create a new property.
Set new_property = db.CreateProperty( _
Name:=txtPropertyName.Text, _
Type:=dbText, _
Value:=txtPropertyValue.Text)
' Save the property.
db.Properties.Append new_property
End If
db.Properties(txtPropertyName.Text).Value = _
txtPropertyValue.Text
db.Close
txtPropertyValue.Text = ""
End Sub
|
|
To get a property value, the program uses the database's Properties collection. It uses an On Error statement to protect itself in case the property doesn't exist.
Note also that some properties don't support Value so trying to access their Value raises an error (I guess they like their privacy). Try searching for the "Connection" property.
|
|
Private Sub cmdGet_Click()
Const ERR_PROPERTY_NOT_FOUND As Long = 3270
Dim db As DAO.Database
Dim new_property As DAO.Property
' Open the database.
Set db = DBEngine.Workspaces(0).OpenDatabase( _
txtDatabase.Text, ReadOnly:=False)
' Get the property.
On Error Resume Next
txtPropertyValue.Text = _
db.Properties(txtPropertyName.Text).Value
If Err.Number = ERR_PROPERTY_NOT_FOUND Then
txtPropertyValue.Text = "<not found>"
ElseIf Err.Number <> 0 Then
txtPropertyValue.Text = "<" & Err.Description & ">"
End If
db.Close
End Sub
|
|
To delete a property, the program uses the database's Properties.Delete method. It uses On Error to protect itself in case the property doesn't exist in the collection.
|
|
Private Sub cmdDelete_Click()
Const ERR_PROPERTY_NOT_FOUND As Long = 3270
Dim db As DAO.Database
Dim new_property As DAO.Property
' Open the database.
Set db = DBEngine.Workspaces(0).OpenDatabase( _
txtDatabase.Text, ReadOnly:=False)
' Get the property.
On Error Resume Next
db.Properties.Delete txtPropertyName.Text
If Err.Number = 0 Then
MsgBox "OK"
Else
MsgBox Err.Description
End If
db.Close
End Sub
|
|
|
|