访问 DAO 将整个记录插入另一个记录集
Posted
技术标签:
【中文标题】访问 DAO 将整个记录插入另一个记录集【英文标题】:Access DAO Inserting whole record into Another Recordset 【发布时间】:2017-05-30 13:15:06 【问题描述】:我正在尝试将所有线从一个记录集移动到另一个记录集,并且想知道是否有特定方法可以做到这一点。这是我的代码。
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】:您可以根据需要进行修改(这里源和目标是同一个表):
Public Sub CopyRecords()
Dim rstSource As DAO.Recordset
Dim rstInsert As DAO.Recordset
Dim fld As DAO.Field
Dim strSQL As String
Dim lngLoop As Long
Dim lngCount As Long
strSQL = "SELECT * FROM tblStatus WHERE Location = '" & _
"DEFx" & "' Order by Total"
Set rstInsert = CurrentDb.OpenRecordset(strSQL)
Set rstSource = rstInsert.Clone
With rstSource
lngCount = .RecordCount
For lngLoop = 1 To lngCount
With rstInsert
.AddNew
For Each fld In rstSource.Fields
With fld
If .Attributes And dbAutoIncrField Then
' Skip Autonumber or GUID field.
ElseIf .Name = "Total" Then
' Insert default job code.
' datNow = Now
rstInsert.Fields(.Name).Value = 0
ElseIf .Name = "PROCESSED_IND" Then
rstInsert.Fields(.Name).Value = vbNullString
Else
' Copy field content.
rstInsert.Fields(.Name).Value = .Value
End If
End With
Next
' Insert fields not existing in source table
rstInsert!SomeField.Value = SomeValue
rstInsert!SomeOtherField.Value = SomeOtherValue
rstInsert!YetAField.Value = ThirdValue
.Update
End With
.MoveNext
Next
rstInsert.Close
.Close
End With
Set rstInsert = Nothing
Set rstSource = Nothing
End Sub
【讨论】:
我想这是一个后续问题。如果我在表 1 中有 x 字段,但在表 2 中有 x + y 字段,这仍然有效。由于字段数量不相交,我发现自己使用手动插入。请参阅问题上的编辑。 整个想法是避免多次运行缓慢的 SQL Insert ..。编辑后的答案中显示了一种向目标表的额外字段添加值的方法。以上是关于访问 DAO 将整个记录插入另一个记录集的主要内容,如果未能解决你的问题,请参考以下文章
将 DAO 记录集转换为断开连接的 ADO 记录集 dbDecimal 问题