根据具有唯一标识符的列名更新MS Access中的表

时间:2017-06-22 15:50:17

标签: vba ms-access

我正在跟进我asked about earlier的项目,其中我有两个表:表A包含来自外部源的考试结果,我想将其更新到表B.例如:< / p>

TableA:
UniqueIdentifier
Item01
Item02
etc...

TableB:
UniqueIdentifier
NameofIteminTableA
Result

因此,如果TableA中的第一条记录是UniqueIdenifier = 10,Item01 = 1,而Item02 = 3,那么我试图将其作为两条记录放入TableB:第一条记录将是uniqueIdentifier = 10,NameofIteminTableA = Item01,Result = 1. TableB中的第二条记录是uniqueIdentifier = 10,NameofIteminTableA = Item02,Result = 3.

此代码非常接近:

Dim rsA as dao.recordset
Dim rsB as dao.recordset
dim fld as dao.field

Set rsA = CurrentDB.openRecordset("TableA", dbopendynaset)
Set rsB = CurrentDB.openRecordset("TableB", dbopendynaset)

Do Until rsA.EOF
   For each fld in rsA.Fields
       rsB.AddNew
       rsB!NameofFieldinTableA = fld.Name
       rsB!Result = fld.Value
       rsB.Update
   Next
   rsA.MoveNext
Loop

rsA.close
rsB.Close

Set rsA = Nothing
Set rsB = Nothing

但在我意识到我需要拥有uniqueidentifier字段之前,我从mbizup获得了代码。

很抱歉,如果我没有很好地解释这一点,但任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:0)

Dim rsA as dao.recordset
Dim rsB as dao.recordset
dim fld as dao.field

Set rsA = CurrentDB.openRecordset("TableA", dbopendynaset)
Set rsB = CurrentDB.openRecordset("TableB", dbopendynaset)

Do Until rsA.EOF
   For each fld in rsA.Fields
       If fld.Name <> "UniqueIdentifier" Then
           rsB.AddNew
           rsB!UniqueIdentifier = rsA!UniqueIdentifier '<<<<<<<<
           rsB!NameofFieldinTableA = fld.Name
           rsB!Result = fld.Value
           rsB.Update
       End If
   Next
   rsA.MoveNext
Loop

rsA.close
rsB.Close

Set rsA = Nothing
Set rsB = Nothing