Reputation: 367
I have searched for solution for this problem for a while now and can't seem to find anything relevant. Is it possible to change column labels (titles) in table via VBA. The only way I find to do it is to create query based on table with parameters as SQL aliases. However it adds some more elements to my already complex data base, it is something I want to avoid. I do not want to change column names. Is there any solution to this problem or should I leave it as it is now?
Thanks for the time you took reading this and for answers!
Upvotes: 1
Views: 1014
Reputation: 91326
I think that is what you want, but I am never greatly in favour of adding information to a table that could confuse future users.
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
Upvotes: 1