标题: [求助]記錄集為什麼不能被更新(附代碼) [打印本页] 作者: HG 时间: 2002-10-26 18:33 标题: [求助]記錄集為什麼不能被更新(附代碼) Public Function ufn_modify_idcrad()
Dim cm As New ADODB.Command
Dim cm1 As New ADODB.Command
Dim rs As New ADODB.Recordset
Dim rs1 As New ADODB.Recordset
With cm
.ActiveConnection = CurrentProject.Connection
.CommandText = "employee"
.CommandType = adCmdTable
End With
With cm1
.ActiveConnection = CurrentProject.Connection
.CommandText = "employee1"
.CommandType = adCmdTable
End With
Set rs = cm.Execute
Set rs1 = cm1.Execute
Do Until rs1.EOF
Do Until rs.EOF
If rs.Fields(0) = rs1.Fields(0) Then
rs.Fields(3) = rs1.Fields(3)
rs.Update
End If
rs.MoveNext
Loop
rs1.MoveNext
Loop
rs.Close
rs1.Close
Set rs = Nothing
Set rs1 = Nothing
Set cm = Nothing
Set cm1 = Nothing
End Function
------------記錄集為什麼不能被更新--------------------------------[em16][em16]作者: yuab 时间: 2002-10-26 19:41
提点意见,你这种做法是将两个表记录集一齐打开。然后循环遍历rs1表中的每一笔记录,当循环遍历rs1记录时你会到rs记录集中找一次相同的栏位Fields(0),如果相同,将rs.field(3)=rs1.field(3).这种做法以前我做过,不过有一次我没事的时候做了一次测试我就再也不用了。原因是还有比这更快的方法。我用了三种方法来测试的。
例如:表1=employee 表2=employee1 表1有8000笔资料。(估计数。)
测试1:最快花了56秒(根据记录集的栏位数量时间有所不同)
先打开rs记录集,然后再循环遍历一次rs记录集,到每一笔时,就打开rs1记录集,这个rs1记录集是根据rs记录集的当前记录条件打开的。打开rs1记录集再循环更改内容。完成rs1的更改后关闭rs1记录集。再进行rs的下一笔,如此类推。
dim cn=adodb.connection
dim rs,rs1 as adodb.recordcount
set rs=new adodb.recordcount
set rs1=new adodb.recordcount
rs.open "select * from employee",cn,adopenkeyset,ad....
do until rs.eof
rs1.open "select * from employee1 where 第一个栏位名='"rs(0)"'",cn,adopenkeyset,ad....
do until rs1.eof
rs1(0)=rs(0)
rs1.movenext
loop
rs1.close
rs.movenext
loop
rs.close