所以这次我的问题是我的do循环似乎没有在记录集中进行,而是对循环中的每个SQL事件使用相同的条件。我希望它通过表单的rs循环,使用每条记录中的条件来确定单独表上的匹配项,并在那里相应地调整值。
对测试数据运行循环时的立即窗口如下所示……
UPDATE [Caliper Master] SET [Bid] = [Bid] - 281, [Sold] = [Sold] + 281 WHERE [A1CardoneItemNo] = '16-4021A';
UPDATE [Caliper Master] SET [Bid] = [Bid] - 281, [Sold] = [Sold] + 281 WHERE [A1CardoneItemNo] = '16-4021A';
UPDATE [Caliper Master] SET [Bid] = [Bid] - 281, [Sold] = [Sold] + 281 WHERE [A1CardoneItemNo] = '16-4021A';
UPDATE [Caliper Master] SET [Bid] = [Bid] - 281, [Sold] = [Sold] + 281 WHERE [A1CardoneItemNo] = '16-4021A';
UPDATE [Caliper Master] SET [Bid] = [Bid] - 281, [Sold] = [Sold] + 281 WHERE [A1CardoneItemNo] = '16-4021A';
每个记录的数字和末尾的“ 16-4021A”都应更改。
这是我的代码。
Private Sub btnConfirm_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
On Error GoTo EH
If Me.Dirty Then Me.Dirty = False
If MsgBox("All records have been saved. Do you wish to exit?/ Todos los registros han sido guardados. ¿Deseas salir?", vbYesNo) = vbYes Then
Set db = CurrentDb
Set rs = Me.RecordsetClone
DoCmd.DeleteObject acTable, "tblCalOrderShorts"
rs.MoveFirst
Do Until rs.EOF
If Me.QuantityHv <> 0 Then
If Me.WithBracket = -1 Then
strSQL = "UPDATE [Caliper Master] SET [Bid] = [Bid] - " & Me.QuantityHv & ", [Sold] = [Sold] + " & Me.QuantityHv & " WHERE [A1CardoneItemNo] = '" & Me.Cardone & "';"
Debug.Print strSQL
db.Execute strSQL, dbFailOnError
ElseIf Me.WithBracket = 0 Then
strSQL = "UPDATE [Caliper Master] SET [WOBracketBid] = [WOBracketBid] - " & Me.QuantityHv & ", [WOBracketSold] = [WOBracketSold] + " & Me.QuantityHv & " WHERE [A1CardoneItemNo] = '" & Me.Cardone & "';"
Debug.Print strSQL
db.Execute strSQL, dbFailOnError
ElseIf Me.WithBracket = Null Then
strSQL = "UPDATE [Caliper Master] SET [BracketBid] = [BracketBid] - " & Me.QuantityHv & ", [BracketSold] = [BracketSold] + " & Me.QuantityHv & " WHERE [A1CardoneItemNo] = '" & Me.Cardone & "';"
Debug.Print strSQL
db.Execute strSQL, dbFailOnError
ElseIf Me.WithBracket <> -1 Or 0 Or Null Then
MsgBox "Invalid value for bracket status./ No esta correcto el estado por bracket."
Exit Sub
End If
ElseIf Me.QuantityHv = 0 Then
End If
If Me.QuantityHv < Me.QuantityNd Then
If Not IsNull(DLookup("tblCalOrderShorts", "MSysObjects", "Name = 'tblCalOrderShorts'")) Then
db.Execute "CREATE TABLE tblCalOrderShorts(Cardone CHAR, QuantityNd INTEGER, QuantityHv INTEGER, Short INTEGER) "
End If
strSQL = "INSERT INTO tblCalOrderShorts(Cardone, QuantityNd, QuantityHv, Short) VALUES('" & Me.Cardone & "', " & Me.QuantityNd & ", " & Me.QuantityHv & ", " & Me.QuantityNd - Me.QuantityHv & ");"
Debug.Print strSQL
db.Execute strSQL, dbFailOnError
End If
rs.MoveNext
Loop
DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenForm "subCaliperOrderDetails", , , "[Cardone] =" & Me!subCaliperOrderDetails.Form!Cardone, , acDialog
Forms!subCaliperOrderDetails.Form.Requery
DoCmd.Close acForm, "frmCaliperPack"
Else
Exit Sub
End If
EH:
Select Case Err
Case 3376
Resume Next
Case 7874
Resume Next
Case 2465
Resume Next
Case Else
MsgBox "Run Time Error " & Err.Number & ", " & Err.DESCRIPTION
End Select
End Sub
答案 0 :(得分:3)
您循环了记录集try("a" + 1, silent = T) #returns nothing
,但是在创建sql字符串时使用了rs
之类的表单数据。
这将始终使用表单上当前记录的数据,即每次循环迭代使用相同的值。
改用Me.QuantityHv
中的值,例如rs
。