简体   繁体   中英

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

I need to open a Table(Unmatched); Create a New Field (ID); Use existing Fields to create a new unique String (hertz); then, finally, insert this new string Back into the Table's New Field. I'm stuck 'cause I'm new. Any nudge forward would be greatly appreciated.

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

This is how I would do it in code. An update query would be easier if you did not need to create the field on the fly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM