ACCESS数据库中Field对象的caption属性(也就是标题)是用来设置数据字段的标题,在正常的数据库设计中为了保持维护的便利性,许多开发者都将字段名与标题做了分别设置,标题往往比字段名更友好,更能说明字段的用途。本篇从另一个角度来说明如何用VBA读写该属性。
Field对象的CAPTION属性并不是ADO原生对象,而是“可由ADO访问的ACCESS属性”,在帮助文档中介绍了两种访问这个属性的方法,一种利用ADO,一种利用DAO,由于在ACCESS2003及以前的版本中Field对象并不是ACCESSObject对象,因而也就没有AccessObjectProperties 属性,所以我们也就不能在ADO中去解决这个问题,现在用另一种方式来解决DAO的代码。
Sub SetProperty(dbsTemp As DAO.Field, strName As String, _
booTemp As String)
Dim prpNew As DAO.Property
Dim errLoop As Error
Attempt to set the specified property.
On Error GoTo Err_Property
dbsTemp.Properties(strName) = booTemp
On Error GoTo 0
Exit Sub
Err_Property:
Error 3270 means that the property was not found.
If DBEngine.Errors(0).Number = 3270 Then
Create property, set its value, and append it to the
Properties collection.
Set prpNew = dbsTemp.CreateProperty(strName, _
dbText, booTemp)
dbsTemp.Properties.Append prpNew
Resume Next
Else
If different error has occurred, display message.
For Each errLoop In DBEngine.Errors
MsgBox "Error number: " &. errLoop.Number &. vbCr &. _
errLoop.Description
Next errLoop
End
End If
End Sub
Sub DisplayClumCaption(ByVal tbname As String,
ByVal fldIndex As Integer)
Dim dset As DAO.TableDef) //*****必须使用TableDef对象
Dim i As Integer
Dim tmpProp As DAO.Property //强制使用DAO类型
Dim fld As DAO.Field //强制使用DAO类型
Dim tmpTxt As String