OK, assuming your original table is called: "Query1" (it can actually be a
query for that matter) and your target table is called "NewTable", you can
use the following subroutine:
'-------------------
Sub FixTable()
Dim db As DAO.Database
Dim rsSource As DAO.Recordset
Dim rsTarget As DAO.Recordset
Set db = CurrentDb
Set rsSource = db.OpenRecordset("Query1", dbOpenDynaset)
Set rsTarget = db.OpenRecordset("NewTable", dbOpenDynaset)
Dim strCriteria As String
Do While Not rsSource.EOF
strCriteria = "[tDate] = #" & rsSource!tdate & "# And [TrapNo] = '"
& rsSource!trapno & "'"
rsTarget.FindFirst strCriteria
If Not rsTarget.NoMatch Then
rsTarget.Edit
rsTarget!tdate = rsSource!tdate
rsTarget!trapno = rsSource!trapno
rsTarget(rsSource!species) = rsSource!amtcoll
rsTarget.Update
Else
rsTarget.AddNew
rsTarget!tdate = rsSource!tdate
rsTarget!trapno = rsSource!trapno
rsTarget(rsSource!species) = rsSource!amtcoll
rsTarget.Update
End If
rsSource.MoveNext
Loop
rsSource.Close
rsTarget.Close
db.Close
End Sub
'------------------
Put this function in a general module, put the cursor anywhere in the module
and click the Run button.
NOTE: This routine requires a Reference set to Microsoft DAO Object Library
3.6 under Tools > Reference in any code window.
--
--Roger Carlson
MS Access MVP
Access Database Samples:
www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L