访问 VBA 以重命名 A Table 中的前 3 个字段
Access VBA To Rename First 3 Fields In A Table
由于导入过程中的不一致以及人们不遵守协议,保存相同数据的多个表之间的命名约定不正确。我需要一种查询数据库中所有表的方法,如果前三个字段没有命名
emp_id, emp_name, emp_address
然后将字段重命名为上面的命名约定。如何使用 VBA Access 2013 实现这一点?
这将重命名所有非系统 table 中的前 3 个字段,但在 all table秒。它 确实 测试 table 中是否至少有 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
由于导入过程中的不一致以及人们不遵守协议,保存相同数据的多个表之间的命名约定不正确。我需要一种查询数据库中所有表的方法,如果前三个字段没有命名
emp_id, emp_name, emp_address
然后将字段重命名为上面的命名约定。如何使用 VBA Access 2013 实现这一点?
这将重命名所有非系统 table 中的前 3 个字段,但在 all table秒。它 确实 测试 table 中是否至少有 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