在 Table 中插入计算字段作为一个字符串 VBA - MS Access

Insert Calculated Field in Table as one String VBA - MS Access

我需要开一个Table(不匹配);创建一个新字段(ID);使用现有的 Fields 创建一个新的唯一 String (hertz);然后,最后,将这个新字符串插入回 Table 的新字段中。我被卡住了,因为我是新来的。任何向前推动将不胜感激。

Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim Hertz As String
Dim rst As DAO.Recordset

Set db = CurrentDb()
Set tdf = db.TableDefs("Unmatched")
Set fld = tdf.CreateField("ID")
Set rst = db.OpenRecordset("Unmatched", dbOpenTable)

Do Until rst.EOF
hertz = rst![Accounting Document Item] & Mid(rst![JE Line Description], 20, 2) & Round(Abs(rst![Transaction Amount]), 0)

Debug.Print hertz 'immediate window check
---> DoCmd.RunSQL "?!?!?"
rst.MoveNext
Loop

Application.RefreshDatabaseWindow

Set fld = Nothing
Set tdf = Nothing
Set db = Nothing
End Sub
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim Hertz As String
Dim rst As DAO.Recordset

Set db = CurrentDb()
Set tdf = db.TableDefs("Unmatched")
Set fld = tdf.CreateField("ID", dbText, 255)
tdf.Fields.Append fld

Set rst = db.OpenRecordset("Unmatched")

Do Until rst.EOF
    Hertz = rst![Accounting Document Item] & Mid(rst![JE Line Description], 20, 2) & Round(Abs(rst![Transaction Amount]), 0)
    Debug.Print Hertz
    rst.Edit
    rst!ID = Hertz
    rst.Update
    rst.MoveNext
Loop

rst.Close
Set rst = Nothing
Set db = Nothing

这就是我在代码中的做法。如果您不需要即时创建字段,更新查询会更容易。