ACCESS中Field對象的標題屬性

來源:互聯網
上載者:User

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
'On Error Resume Next
Dim msg As String
Dim cdb As DAO.Database //*****強制使用DAO類型
Set cdb = CurrentDb //****關鍵,確定對當前資料庫的靜態引用
Set dset = cdb.TableDefs(tbname)//*****必須使用TableDef對象
For Each fld In dset.Fields
   tmpTxt = fld.Name
   SetProperty fld, "Caption", tmpTxt
   msg = msg + fld.Properties("Caption")
   msg = msg + Chr(10) + Chr(13)
Next fld
MsgBox msg
End Sub

在以上部分的代碼中有兩個SUB,一個是SetProperty ,用來判斷一個欄位是否有指定的屬性,如果沒有設定,就將相應的數值賦給該屬性。另一個是DisplayClumCaption,這是對指定表中的欄位按欄位名設定其CAPTION屬性的示範代碼。如果有需要,大家可以對SetProperty進行修改,使他變成一個唯讀函數,用來枚舉指定表中每個欄位的CAPTION屬性。DisplayClumCaption代碼中,打“星號”的地方是要重點注意的,不然可能會在MSDN中多走彎路。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.