Click here to Skip to main content
15,891,713 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a process that will update the records of the material. but first it will get the existing records of the material like avail quantity before updating and when i try to update it with the new one. i get an error saying "Run-time error ‘-2147217864(80040e38)’: Row cannot be located for updating. Some values may have been changed since it was last read."

heres my code:

Dim rs As New ADODB.Recordset
Set rs = New ADODB.Recordset
Dim coy As String
If Left(Text4.Text, 3) = "800" Then
For i = 1 To ListView2.ListItems.Count
With ListView2.ListItems(i)
If rs.State <> 0 Then rs.Close
     rs.Open "Select * from Inventory where InventoryID = " & Val(.ListSubItems(6).Text), db, 3, 3
     If rs.RecordCount <> 0 Then
       'get average cost start
        Dim avgcost, Stockqty, newstockqty As Double
        Stockqty = CDbl(rs!AvailQty) * CDbl(rs!unitprice)
        newstockqty = CDbl(.ListSubItems(7).Text) * CDbl(.ListSubItems(8).Text)
        avgcost = (Stockqty + newstockqty) / CDbl(CDbl(rs!AvailQty) + 
CDbl(.ListSubItems(7).Text))
       'get average cost end

       rs!avecost = FormatNumber(avgcost, 3)
       avaiqtyparam = CDbl(rs!AvailQty) + CDbl(.ListSubItems(7).Text)
      rs!AvailQty = CDbl(rs!AvailQty) + CDbl(.ListSubItems(7).Text)
       rs!unitprice = FormatNumber(CDbl(.ListSubItems(8).Text), 5)
       rs!lastupdate = FormatDateTime(DTPicker1.Value, vbShortDate)

       Dim AvgVat, stockvat, nestockvat As Double

       stockvat = CDbl(rs!AvailQty) * CDbl(rs!AvailVAT)
       nestockvat = CDbl(.ListSubItems(7).Text) * CDbl(.ListSubItems(11).Text)
       AvgVat = (stockvat + nestockvat) / CDbl(CDbl(rs!AvailQty) + 
 CDbl(.ListSubItems(11).Text))
       rs!AvailVAT = CDbl(.ListSubItems(11).Text)
       rs!AveVAT = FormatNumber(AvgVat, 3)
       rs.Update
     End If
End With
rs.Close
Set rs = Nothing
Next i


What I have tried:

i have a primary key on my table. and i dont know why this error is ocurring.
Posted
Updated 13-Oct-17 3:47am
Comments
Member 13462396 13-Oct-17 2:08am    
p

1 solution

Seems, you're trying to update row which is not in edit mode.

There might be 2 reasons:
1) a recordset is open in static mode
2) you didn't added .Edit instruction.

Here is suggested code:

Case 1 - multiple records
VB
rs.Open "Select * from Inventory where InventoryID = " & Val(.ListSubItems(6).Text)
With rs
    Do While Not .EOF
        .Edit
        'your edits here
        .Update
        .MoveNext
    Loop
End With


Case 2 - single record
VB
rs.Open "Select * from Inventory where InventoryID = " & Val(.ListSubItems(6).Text)
With rs
    .Edit
    'your edits here
    .Update
End With



For further details, please check the documentation:
Open Method (ADO Recordset) | Microsoft Docs[^]
Recordset.Update Method (DAO)[^] - with working example
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900