要用vba修改access数据库中表的字段名称,需要用到ADOX.Catalog对象。
ADOX.Catalog是专门用来遍历数据库中的表、索引、键值、列、视图、以及属性的对象。
修改数据库中表的字段名称,需要先访问具体的表对象Table。然后访问表对象的Column对象(也就是字段对象),最后设置Column对象的Name属性值即可。
如下图所示为某个Access数据库中的一个表和该表的所有字段:

如果要修改其中的字段“预留8”为一个新的字段名称,可以使用如下的vba代码:
Sub QQ1722187970()
Dim objCatalog
Set objCatalog = VBA.CreateObject("ADOX.Catalog")
Dim sConStr As String
Dim sPath As String
sPath = Excel.ThisWorkbook.Path
Dim oTable
Dim oColumn
With objCatalog
sConStr = "Provider='Microsoft.ACE.OLEDB.12.0';Data Source=" & sPath & "\ywglb.accdb"
.ActiveConnection = sConStr
For Each oTable In .tables
With oTable
If .Type = "TABLE" And .Name Like "*员工信息表*" Then
With oTable
For Each oColumn In .Columns
With oColumn
If .Name = "预留8" Then
.Name = "新字段名称"
End If
End With
Next
End With
End If
Debug.Print .Name, .Type
End With
Next
End With
End Sub


发表评论