循环访问Access中的两个记录集并更新条目

时间:2014-03-21 15:35:15

标签: sql vba ms-access-2007

我的数据库中有两个名为Book和ReferencesNum的表,它们具有一对多的关系,其中Book是主表。我想遍历两个记录,当Book中的主键(Title)等于ReferencesNum中具有相同Title的记录时,我想从Book中的记录中取出ID号并将相同的值放入ReferencesNum中的ID字段。

我想通过使用SQL Statments来做到这一点。我得到了VBA,但我正在尝试使用SQL语句。

VBA代码:

Sub GoThroughX()

Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset

Set db = CurrentDb()
Set rs1 = db.OpenRecordset("Book")
Set rs2 = db.OpenRecordset("ReferencesNum")

  rs1.MoveFirst
  Do Until rs1.EOF
    rs2.MoveFirst
     Do Until rs2.EOF
       If rs1![Title] = rs2![Title] Then
         rs2.Edit
         rs2![ID] = rs1![ID]
         rs2.Update
       End If
      rs2.MoveNext
    Loop
 rs1.MoveNext
 Loop

rs1.Close
rs2.Close
Set rs1 = Nothing
Set rs2 = Nothing
Set db = Nothing

MsgBox "Done"

End Sub

使用SQL:

Sub UpdateX1()

Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset

Set db = CurrentDb()
Set rs1 = db.OpenRecordset("Book")
Set rs2 = db.OpenRecordset("ReferencesNum")

rs2.MoveFirst

 Do Until rs2.EOF

    SQLstr = "UPDATE ReferencesDoc " & _
             "SET ID = rs1![ID] " & _
             "WHERE [Title] = rs1![Title];"
    DoCmd.RunSQL SQLstr

 rs2.MoveNext
 Loop

 MsgBox "Done"

 End Sub

带有SQL的代码可以工作,但它会一直提示我必须输入ID和标题的框,然后它会更改ID值。

我怎样才能让它停止询问ID和Title值,只是自己查看表格,就像VBA代码一样。

先谢谢。

2 个答案:

答案 0 :(得分:1)

您需要重新评估您的设计。为什么你首先使用记录集?

您正在创建一个O(N ^ 2)进程,您可以使用单个SQL语句更新所有记录,从而将其完成为O(1)。如果您打算使用DAO,那么请查看DAO.QueryDef对象而不是DAO.Recordset对象来完成SQL解决方案。

但是,在回答上述问题时,您会收到提示,因为您在引用记录集中的字段时使用了错误的语法,因此Access认为您要提供参数。已经很长一段时间但我相信正确的语法是第一个。(" fieldName")或者使用rst.fields(" fieldName")来引用一个列你的记录集。

但实际上,你想远离这里的记录集。

答案 1 :(得分:0)

考虑解决方案,将访问警告设置为关闭: 最好的问候

'With SQL:

Sub UpdateX1()

Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset

Set db = CurrentDb()
Set rs1 = db.OpenRecordset("Tabl1")
Set rs2 = db.OpenRecordset("Tabl2")


'dans un tel cas, ne pas oublier de régler les warnings à false
DoCmd.SetWarnings False
rs1.MoveFirst
Do Until rs1.EOF

rs2.MoveFirst

 Do Until rs2.EOF

    sqlstr = "UPDATE Tabl2 " & _
             "SET fld2 = '" & rs1![fld2] & "'" & _
             "WHERE [fld1] = '" & rs1![fld1] & "';"
    DoCmd.RunSQL sqlstr

 rs2.MoveNext
 Loop
 rs1.MoveNext
Loop
DoCmd.SetWarnings True

rs1.Close
rs2.Close
Set rs1 = Nothing
Set rs2 = Nothing
Set db = Nothing

 MsgBox "Done"

End Sub

'使用SQL: '考虑有限的记录, '在你的recordset1和你的记录集2之间 '以这种方式更新,因为仍在运行 '很长时间 '现在,你可以写更多的仿制药! “祝你好运!

Sub UpdateX2()

Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset

Dim strSQL1 As String
Dim strSQL2 As String


strSQL1 = "SELECT Top 10 fld1, fld2 FROM Tabl1;"


Set db = CurrentDb()
Set rs1 = db.OpenRecordset(strSQL1, dbOpenSnapshot)
Set rs2 = db.OpenRecordset("Tabl2")


'dans un tel cas, ne pas oublier de régler les warnings à false
DoCmd.SetWarnings False
rs1.MoveFirst
Do Until rs1.EOF

rs2.MoveFirst

 Do Until rs2.EOF

    sqlstr2 = "UPDATE Tabl2 " & _
             "SET fld2 = '" & rs1![fld2] & "'" & _
             "WHERE [fld1] = '" & rs1![fld1] & "';"
    DoCmd.RunSQL sqlstr2

 rs2.MoveNext
 Loop
 rs1.MoveNext
Loop
DoCmd.SetWarnings True

rs1.Close
rs2.Close
Set rs1 = Nothing
Set rs2 = Nothing
Set db = Nothing

 MsgBox "Done"

End Sub