通过VBA更改访问列标签

时间:2014-04-09 08:01:08

标签: vba ms-access label ms-access-2007 title

我已经搜索了这个问题的解决方案一段时间了,似乎找不到任何相关的东西。是否可以通过VBA更改表中的列标签(标题)。我发现这样做的唯一方法是基于表创建查询,参数为SQL别名。然而,它为我已经很复杂的数据库增加了一些元素,这是我想要避免的。我不想更改列名。有没有解决这个问题的方法,还是我现在应该保留它?

感谢您花时间阅读本文并获得答案!

1 个答案:

答案 0 :(得分:1)

我认为这就是你想要的,但我绝不赞成向可能会混淆未来用户的表格添加信息。

Option Compare Database
Option Explicit

Sub Usage()
    SetProperty "Table1", "ID", "Caption", "This is an ID"
End Sub

Sub SetProperty(TableName As String, Fieldname As String, _
    PropertyName As String, PropertyValue As Variant, _
    Optional PropertyType As Variant = dbText)

Dim db As DAO.Database
Dim fld As DAO.Field
Dim prp As DAO.Property

    On Error GoTo Err_Property

    Set db = CurrentDb
    Set fld = db.TableDefs(TableName).Fields(Fieldname)
    fld.Properties(PropertyName) = PropertyValue

    ''Debug.Print fld.Properties(PropertyName)

    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 prp = fld.CreateProperty(PropertyName, _
            PropertyType, PropertyValue)
        fld.Properties.Append prp
        Resume Next
    Else
        MsgBox Err.Description
    End If

End Sub