1
我想將所有的電話線從一個記錄集移到另一個記錄集,並想知道某個特定的方法是否可以做到這一點。這是我的代碼。訪問DAO將整個記錄插入另一個記錄集
Dim maxDate As Variant
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim destin As DAO.Recordset
maxDate = DMax("[Eff Date]", "400_CF_BREAK_LOG")
Set db = CurrentDb
Set rs = db.OpenRecordset("860_APPEND_DIFFERENCES") 'myTable is a MS-Access table created previously
Set destin = db.OpenRecordset("400_CF_BREAK_LOG")
'populate the table
rs.MoveFirst
Do While Not rs.EOF
If (rs![Eff Date] > maxDate) Then
destin.
Debug.Print (rs!myField) 'myField is a field name in table myTable
rs.MoveNext
Loop
我陷入了目的地。 < ----在這裏添加整個記錄。
編輯。所選的答案雖然正確,但可能不會考慮表格之間不同數量的字段。我發現自己使用這個設置來添加三個不同的記錄集。從Insert Complete RecordSet to another table in other database MS Access
maxDate = DMax("[Eff Date]", "400_CF_BREAK_LOG")
Set db = CurrentDb
Set rs = db.OpenRecordset("mytable") 'myTable is a MS-Access table created previously
'populate the table
rs.MoveFirst
Do While Not rs.EOF
If (rs![Eff Date] > maxDate) Then
sqlinsert = "INSERT INTO 400_CF_BREAK_LOG (Eff Date, PrimarySecurity ID Number, CUSIP(Aladdin ID), IsrName, Asset Type, Metlife Port Code, Business Unit, Principal Difference, Total PAM Principal, Total Aladdin Principal,Income Difference, Total PAM Interest,Total Aladdin Interest,Total CF Difference,Total PAM CF,PAM Coupon)" & _
" VALUES ('" & rs("Eff Date") & "', '" & rs("PrimarySecurity ID Number") & "', '" & rs("CUSIP(Aladdin ID)") & "', '" & rs("IsrName") & "', '" & rs("Asset Type") & "', '" & rs("Metlife Port Code") & "', '" & rs("Business Unit") & "', '" & rs("Principal Difference") & "', '" & rs("Total PAM Principal") & "', '" & rs("Total Aladdin Principal") & "', & '" & rs("Income Difference") & "', '" & rs("Total PAM Interest") & "', '" & rs("Total Aladdin Interest") & "', '" & rs("Total CF Difference") & "', '" & rs("Total PAM CF") & "', '" & rs("PAM Coupon") & "')"
DoCmd.RunSQL (sqlinsert)
rs.MoveNext
Loop
我想跟進的問題是這樣的。如果我在表1中有x個字段,但在表2中有x + y個字段,這是否仍然有效。我發現自己使用手動插入,因爲字段數量不相交。請參閱編輯問題。 – FamousFrik
整個想法是爲了避免多次運行緩慢的SQL _Insert .._。在編輯的答案中顯示了向目標表的額外字段添加值的一種方法。 – Gustav