0
由於導入過程和未遵循協議的人員不一致,因此保存相同數據的多個表之間的命名約定不正確。我需要一種方法來查詢數據庫中的所有表,如果前三個字段沒有命名訪問VBA重命名錶中前3個字段
emp_id, emp_name, emp_address
則字段重命名爲命名約定以上。如何使用VBA Access 2013實現這一點?
由於導入過程和未遵循協議的人員不一致,因此保存相同數據的多個表之間的命名約定不正確。我需要一種方法來查詢數據庫中的所有表,如果前三個字段沒有命名訪問VBA重命名錶中前3個字段
emp_id, emp_name, emp_address
則字段重命名爲命名約定以上。如何使用VBA Access 2013實現這一點?
這將重命名所有非系統表中的前3個字段,但是您應該對所有表中的字段進行重命名時更加小心。它確實測試以查看錶中是否至少有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