有2组一样的数据更新过程中,一个可以更新一个不可以更新。
错误是:
Run-time error '-2147217864(80040e38)':
Row cannot be located for updating. some values may bave been changed since it was last read

解决方案 »

  1.   

    Public Function Adjust(ByVal vInventoryID, ByVal vNotes As String, ByVal vQuantity As Double, Optional ByVal vUnitCost As Currency = -1, Optional ByVal vFixcost As Boolean = False) As CostingResult
    Dim sCmd            As String
    Dim oRs             As ADODB.RecordsetDim lAdjustID       As Long
    Dim nRealCutQty     As Double
    Dim cRealCutAmt     As CurrencyDim dPQty           As Double   'nid    If Not vFixcost Then
            If vInventoryID <= 0 Or vQuantity = 0 Then
                Adjust = INCORRECT_PARAMETER
                Exit Function
            End If
        Else
            If vInventoryID <= 0 Or vUnitCost < 0 Then
                Adjust = INCORRECT_PARAMETER
                Exit Function
            End If
        End If
        
        sCmd = sCommandText & " Where InventoryID = " & vInventoryID
        Set oRs = New ADODB.Recordset
        oRs.Open sCmd, DCN, adOpenDynamic, adLockOptimistic, adCmdText
        If oRs.RecordCount = 0 Then
            If vQuantity < 0 Then GoTo ErrHandle
            
            sCmd = "INSERT INTO InventoryCostAverage "
            sCmd = sCmd & "(InventoryID,PQuantity,PAmount,CutQuantity,UnitCost,CutAmount) "
            sCmd = sCmd & "VALUES ("
            sCmd = sCmd & vInventoryID & ","
            sCmd = sCmd & " 0 ,"
            sCmd = sCmd & " 0 ,"
            sCmd = sCmd & " 0 ,"
            sCmd = sCmd & " 0 ,"
            sCmd = sCmd & " 0 " & ")"
                        
            oCmn.ExecSql sCmd, DCN
            
            sCmd = sCommandText & " Where InventoryID = " & vInventoryID
            Set oRs = New ADODB.Recordset
            oRs.Open sCmd, DCN, adOpenDynamic, adLockOptimistic, adCmdText
            If oRs.RecordCount = 0 Then GoTo ErrHandle
        End If
        
        If vQuantity > 0 Then
            dPQty = oRs("PQuantity")    'nid
            oRs("PQuantity") = oRs("PQuantity") + vQuantity
            If vUnitCost <> 0 Then
                oRs("PAmount") = oRs("PAmount") + vQuantity * vUnitCost
            End If
            If (oRs("PQuantity") - oRs("CutQuantity")) <> 0 Then
                If vUnitCost <> 0 Then
                    'oRs("UnitCost") = Abs((oRs("PAmount") - oRs("CutAmount")) / (oRs("PQuantity") - oRs("CutQuantity")))   'nid hide for correcting UnitCost  (Original)
                    'oRs("UnitCost") = Abs((((oRs("PQuantity") - oRs("CutQuantity")) * oRs("UnitCost")) + (vQuantity * vUnitCost)) / ((oRs("PQuantity") - oRs("CutQuantity")) + vQuantity)) 'nid (rough)
                    oRs("UnitCost") = Abs((((dPQty - oRs("CutQuantity")) * oRs("UnitCost")) + (vQuantity * vUnitCost)) / ((dPQty - oRs("CutQuantity")) + vQuantity)) 'nid modified
                Else
                    oRs("UnitCost") = 0
                End If
            End If
            oRs.Update2组数据分别做,一组可以更新成功。一组就出现上面那个错误。
    数据类型是都是DECIMAL(19,4)
      

  2.   

    建议用conn.execute "update 表名 set ... where ..."这种方式更新