当前位置: 首页 > 图文教程 > 数据库 > Access > 另类读写:ACCESS中Field对象的标题属性

Access
获取access数据库中表的个数及名称
安全的ACCESS加密方法
ACCESS如何打印窗体中当前显示的记录
Access2K中的查询分析器
Access下如何使用通用对话框
Access与Flash的结合应用
防止Access 2000密码被破译的方法
在Recordset对象中查询记录的方法
Excel和Access之间的数据交换
如何为Access数据库表添加日期或时间戳
Access:数据转换问题
如何在Access 2007数据库中添加附件
几种修复ACCESS数据库的实用方法
ACCESS中Field对象的标题属性
Access数据库出现0x80004005问题的解决方法
实例讲解Access数据库在线压缩的实现方法
带你深入了解Access数据库的4种安全方式
三大措施设置数据库安全 保障网站安全运营
Microsoft Access项目不能压缩的原因
详细讲解如何删除Access数据库中的空记录

Access 中的 另类读写:ACCESS中Field对象的标题属性


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 53 ::
收藏到网摘: n/a

ACCESS数据库中Field对象的caption属性(也就是标题)是用来设置数据字段的标题,在正常的数据库设计中为了保持维护的便利性,许多开发者都将字段名与标题做了分别设置,标题往往比字段名更友好,更能说明字段的用途。本篇从另一个角度来说明如何用VBA读写该属性。

以下为引用的内容:
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中多走弯路。