|
小弟我 刚学ACCESS不久,对代码也不算了解,写了如下代码,总是不理想,请各位高手帮帮忙, 谢谢啦
Dim rst1 As ADODB.Recordset
Dim rst2 As ADODB.Recordset
Set rst1 = New ADODB.Recordset
Set rst2 = New ADODB.Recordset
Dim str1, str2 As String
str1 = "select tblorder.partinfoID,sum(tblorder.partsum) as totalsum from tblorder "
str1 = str1 + "group by tblorder.partinfoID"
With rst1
.ActiveConnection = CurrentProject.Connection
.CursorType = adOpenForwardOnly
.LockType = adLockReadOnly
.Open str1, options:=adCmdText
End With
str2 = "select totalparts.partinfoID,totalparts.partsum from totalparts "
With rst2
.ActiveConnection = CurrentProject.Connection
.CursorType = adOpenDynamic
.LockType = adLockPessimistic
.Open str2, options:=adCmdText
End With
Do Until rst1.EOF
rst2.Find "[partinfoID]=" & rst1!partinfoID
If rst2.EOF Then
With rst2
.AddNew
!partinfoID = rst1!partinfoID
!partsum = rst1!totalsum
.Update
End With
Else
rst2!partsum = rst1!totalsum
End If
rst1.MoveNext
Loop
rst1.Close
rst2.Close
Set rst1 = Nothing
Set rst2 = Nothing
此代码作用是:将tblorder的记录赋值给rst1;把tbltotalparts的记录赋值给rst2;
然后验证rst2与rst1是否含有"partinfoID"字段相同的记录;
如果rst2中没有,则新添加记录,并且给相应字段赋值;
如果rst2中存在记录,则直接给partsum字段赋值。
此代码作为过程使用,第一此更新rst2时,不存在问题,可是再次点击该命令时:
rst2中的记录数与rst1中的记录数量不同(rst2中记录有重复的,但只是个别现象),
请各位大大们帮忙!
谢谢各位啦!
PS:还请各位帮我想个更好的办法
[ 本帖最后由 effytears 于 2008-12-1 22:12 编辑 ] |
|