【发布时间】:2020-07-13 19:35:49
【问题描述】:
由于导入过程中的不一致以及人们不遵守协议,保存相同数据的多个表之间的命名约定是不正确的。我需要一种方法来查询数据库中的所有表,如果前三个字段没有命名
emp_id, emp_name, emp_address
然后将该字段重命名为上述命名约定。如何使用 VBA Access 2013 实现这一点?
【问题讨论】:
标签: ms-access vba ms-access-2013
由于导入过程中的不一致以及人们不遵守协议,保存相同数据的多个表之间的命名约定是不正确的。我需要一种方法来查询数据库中的所有表,如果前三个字段没有命名
emp_id, emp_name, emp_address
然后将该字段重命名为上述命名约定。如何使用 VBA Access 2013 实现这一点?
【问题讨论】:
标签: ms-access vba ms-access-2013
这将重命名所有非系统表中的前 3 个字段,但在重命名 all 表中的字段时您可能应该更加小心。它确实测试表中是否至少有 3 个字段,但如果少于 3 个字段,它目前不会添加任何字段。
Sub Rename()
Dim db As Database
Set db = CurrentDb()
Dim tdf As TableDef
For Each tdf In db.TableDefs
'Skip the system tables
If Left(tdf.Name, 4) <> "MSys" Then
Dim requiredNames() As Variant
requiredNames = Array("emp_id", "emp_name", "emp_address")
With tdf.Fields
Dim fieldCounter
For fieldCounter = LBound(requiredNames) To UBound(requiredNames)
'Check the table has as many fields as we expect
If fieldCounter < .Count Then
'Check the field name isn't already the name we require
If .Item(fieldCounter).Name <> requiredNames(fieldCounter) Then
.Item(fieldCounter).Name = requiredNames(fieldCounter)
End If
End If
Next fieldCounter
End With
End If
Next tdf
End Sub
【讨论】: