我有一个包含100个表的MS Access数据库,每个表都有一个名为" M_ID"的字段。 我想要名为" M_ID"的字段中的所有值。在每个表中设置为常量,例如" 1" 我怎么能用VBA做到这一点?
答案 0 :(得分:0)
您可以遍历每个表并使用docmd.ExecuteSQL运行更新语句。像这样:
Option Explicit
Option Compare Database
Sub ClearAllFields()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fdf As DAO.Field
Set db = CurrentDb
DoCmd.SetWarnings False
For Each tdf In db.TableDefs
If Not (tdf.Name Like "MSys*" Or tdf.Name Like "~*") Then
For Each fdf In tdf.Fields
If fdf.Name = "M_ID" Then DoCmd.RunSQL "update " + tdf.Name + " set M_ID = 1"
Next
End If
Next
Set tdf = Nothing
Set db = Nothing
DoCmd.SetWarnings True
End Sub