2016-02-02 58 views
0

我需要打開一個表(不匹配);創建一個新字段(ID);使用現有字段創建一個新的唯一字符串(赫茲);然後,最後,將這個新字符串插回到表格的新字段中。我被卡住了,因爲我是新人。任何微調前鋒將不勝感激。在表中插入計算字段作爲一個字符串VBA - MS Access

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 
+1

rst。編輯,rst.Update(請見在線幫助) – Andre

+0

@Andre,謝謝。你是對的。 –

回答

1
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 

這是我會怎麼做的代碼。如果您不需要動態創建字段,則更新查詢會更容易。

+0

謝謝@ JJ32的答案和糾正我的代碼。你已經幫了我很大的忙。我在學習,兄弟,但這並不容易。再次感謝你。 –