ACCESS數(shù)據(jù)庫中Field對象的caption屬性(也就是標題)是用來設置數(shù)據(jù)字段的標題,在正常的數(shù)據(jù)庫設計中為了保持維護的便利性,許多開發(fā)者都將字段名與標題做了分別設置,標題往往比字段名更友好,更能說明字段的用途。本篇從另一個角度來說明如何用VBA讀寫該屬性。
Field對象的CAPTION屬性并不是ADO原生對象,而是“可由ADO訪問的ACCESS屬性”,在幫助文檔中介紹了兩種訪問這個屬性的方法,一種利用ADO,一種利用DAO,由于在ACCESS2003及以前的版本中Field對象并不是ACCESSObject對象,因而也就沒有AccessObjectProperties 屬性,所以我們也就不能在ADO中去解決這個問題,現(xiàn)在用另一種方式來解決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
Field對象的CAPTION屬性并不是ADO原生對象,而是“可由ADO訪問的ACCESS屬性”,在幫助文檔中介紹了兩種訪問這個屬性的方法,一種利用ADO,一種利用DAO,由于在ACCESS2003及以前的版本中Field對象并不是ACCESSObject對象,因而也就沒有AccessObjectProperties 屬性,所以我們也就不能在ADO中去解決這個問題,現(xiàn)在用另一種方式來解決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