Article ID: 170549
Article Last Modified on 1/20/2007
Function GetFieldProperty(F As Field, _
ByVal PropName As String) As Variant
'
' Returns NULL if the property doesn't exist
'
On Error Resume Next
GetFieldProperty = F.Properties(PropName)
End Function
Sub ModifyFieldProperty(F As Field, ByVal PropName As String, _
ByVal PropType As Long, _
ByVal NewVal As Variant)
Dim P As Property
On Error Resume Next
Set P = F.Properties(PropName)
If Err Then
'
' Add property (as long as NewVal isn't Null)
'
If Not IsNull(NewVal) Then
On Error Goto 0 ' fail if can't add
Set P = F.CreateProperty(PropName, PropType, NewDesc)
F.Properties.Append P
End If
ElseIf IsNull(NewVal) Then
'
' Delete property
'
On Error Goto 0 ' fail if can't delete
F.Properties.Delete PropName
Else
'
' Modify property
'
On Error Goto 0 ' fail if can't alter
P.Value = NewDesc
End If
Set P = Nothing
End Sub
The code can be called as follows:
Sub Test()
Dim db As Database, F As Field
Dim v As Variant
v = "This is a description"
Set db = DBEngine(0).OpenDatabase("NWIND.MDB") ' change name/path
Set F = db!Employees!Title
' Get existing description
Debug.Print "Existing Title Description is: ";
Debug.Print GetFieldProperty(F, "Description")
' Delete description
ModifyFieldProperty F, "Description", dbText, v
Debug.Print "After deleting Description: ";
Debug.Print GetFieldProperty(F, "Description")
' Add description
ModifyFieldProperty F, "Description", dbText, "Employee's Title"
Debug.Print "After adding new Description: ";
Debug.Print GetFieldProperty(F, "Description")
' Modify existing title
ModifyFieldProperty F, "Description", dbText, "Emp Title"
Debug.Print "After modifying Description: ";
Debug.Print GetFieldProperty(F, "Description")
' Clean-up
Set F = Nothing
db.Close
End Sub
Keywords: kbhowto kbprogramming KB170549