问题
I have inherited a MS Access project that is way beyond me: Table A has exam data, so each field in A is the name of an exam item and the value is the points earned on that item. My task is to convert those results to Table B, which has one field for the item name and one field for the corresponding value.
So it looks like this:
Table A:
A.Item01
A.Item02
etc.
Table B:
NameofFieldinTableA
Result
...so if A.Item01 = 1, I'd like that to update to Table B as:
NameOfFieldinTableA = Item01 Result = 1
My apologies for not being able to better explain this, but this has got me completely stumped. My only guess is to write something in VBA?
Any help is appreciated!
回答1:
Yes, you'd use VBA to loop through the fields in your table - using the NAME property to get the NameofFieldinTableA and the VALUE property to get the Result. So the VBA would look something like this:
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
来源:https://stackoverflow.com/questions/44288678/updating-a-table-in-ms-access-based-upon-column-names