|
我需要用下面这段代码,来实现按数量生成单个记录以备它用:
Private Sub CmdAdd_Click()
On Error GoTo Err_CmdAdd_Click
Dim con As Object
Dim rst As Recordset
Dim strsql As String
Set con = Application.CurrentProject.Connection
strsql = "SELECT * FROM [OrderDetails]"
Set rst = New ADODB.Recordset
rst.Open strsql, con, 1
Dim con1 As Object
Dim rst1 As Recordset
Dim strsql1 As String
Set con1 = Application.CurrentProject.Connection
strsql1 = "SELECT * FROM [DoorRecord]"
Set rst1 = New ADODB.Recordset
rst1.Open strsql1, con1, adOpenKeyset, adLockOptimistic
If (rst.EOF) Then
MsgBox "No Record In Form OrderDetals"
Else
While (Not (rst.EOF))
For i = 1 To rst("Quantity")
rst1.AddNew
rst1("OrderDetailID") = rst("OrderDetailID")
rst1.Update
rst1.MoveNext
Next i
rst.MoveNext
Wend
End If
rst.Close
Set rst = Nothing
Set con = Nothing
rst1.Close
Set rst1 = Nothing
Set con1 = Nothing
Exit_CmdAdd_Click:
Exit Sub
Err_CmdAdd_Click:
MsgBox Err.Description
Resume Exit_CmdAdd_Click
End Sub
这段代码是站上某现成作品的,但是因为每次点击按钮,都会把全部都插入一遍,会造成重复记录.
于是我就想在每次开始运行的时候,先把[DoorRecord]清空,这个可以用delete * from [DoorRecord]来实现.但另一个问题又出现了: 因为我的[DoorRecord]的记录号是用的AutoNumber,每次删完,不能(我不会)从0开始,我就试图每次先把表[DoorRecord]删除(DROP TABLE [DoorRecord]),然后再造一遍,再插入记录.但Access总说我的[DoorRecord] "被别的用户或进程使用,数据库引擎无法锁定它",就我一人使用,而且就算把所有其他代码都删掉也不行.那位知道为什么吗? 或者还有什么别的方法可以实现我说的要求?不胜感激![em06][em06][em03][em03][em13][em15] |
|