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

Access
高手支招ASP及Access的安全隐患及对策
如何设置ACCESS2003(运行时)的宏安全性级别
在窗体间传递参数的几种常用办法
完美表格:使用表层属性来提高效率
设计完美表格:适当地命名域
Access应用:仔细地选择索引
Access应用的几个技巧
ASP实现access随机显示不重复记录解决方案
巧用Access逐条输出Excel中的记录
Access数据库安全中常见问题汇总
在VB中动态创建数据库
在Access中使用系统表保存应用程序变量
使ACCESS数据库保持同步
当Access文件大于指定的大小时就自动压缩
数据库并发问题详述
ACCESS集锦
以独占方式打开Access数据库
数据在Access与Office组件间自由流动
基于Access数据库的抽奖系统设计
转换Access数据库以获得更优性能

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 55 ::
收藏到网摘: 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中多走弯路。