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

Access
防止ACCESS数据库被下载的9种方法
ACCESS转化成SQL2000要注意的问题
优化Microsoft Access提高速度
在Access数据库中实现密码管理的另一种方式
Microsoft Access秘密、技巧和陷阱
Access数据库开发技巧(一)
Access数据库开发技巧(二)
Access数据库开发技巧(三)
Access数据库开发技巧(四)
用Access设计客观试卷(1)
用Access设计客观试卷(2)
用Access设计客观试卷(3)
将Access数据库移植到SQLServer
Access命令行参数
如何远程调用ACCESS数据库
Access在调整工资上的应用
ASP+Access的安全隐患及对策
Excel和Access间复制、和导出数据
如何防止ACCESS数据库被下载
怎样在vc、delphi中使用mysql

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


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