Problem with deleting records

HI,

I am using the following function to delete a record from sysDeptDef table which is not present in ups_new table. I have two rows for the same deptid but diff attid. when i use the delete command the two rows get deleted but when the loop executes it cannot find the deptid so it fails.can please anyone suggest me how to handle this situation.

Dim CurrentLevel As Integer
Dim CurrentDeptId As Variant

Dim LevelUp1 As String
Dim LevelUp2 As String
Dim LobId As String
Dim AttId As Integer
Dim Oc_Id As Integer
Dim LevelCheck As Boolean


Set rst_local = dbs.OpenRecordset("SELECT * FROM sysDeptDef")

Do Until rst_local.EOF

CurrentDeptId = rst_local("DeptId") (Error record not found)
CurrentLevel = rst_local("DeptLevel")

LevelUp1 = "L" & CurrentLevel - 1 & "_DEPTID"
LevelUp2 = "L" & CurrentLevel - 2 & "_DEPTID"

Set rst_pplops = dbs.OpenRecordset("SELECT " & LevelUp1 & "," & LevelUp2 & ",Dept_Level,EFF_STATUS,DeptId FROM ups_new WHERE DEPTID = '" & CurrentDeptId & "'")

If rst_pplops.RecordCount <> 0 Then

If rst_pplops(LevelUp1) = rst_local("DeptId_Up1") And _
rst_pplops(LevelUp2) = rst_local("DeptId_Up2") And _
rst_pplops("Dept_Level") = CurrentLevel And rst_pplops("EFF_STATUS") = "A" And rst_pplops("DeptId") = CurrentDeptId Then

rst_local.Edit
rst_local("LevelCheck") = True
rst_local.Update

End If

Else
Set rst_check = dbs.OpenRecordset("select Deptid,AttId from sysDeptDef where Deptid='" & CurrentDeptId & "'")
If rst_check.RecordCount > 0 Then

MsgBox "Deptid '" & CurrentDeptId & "' to be deleted since record not found in pplops and "

dbs.Execute ("delete AttId from sysDeptDef where DeptId='" & CurrentDeptId & "'")
End If
End If

rst_pplops.Close

rst_local.MoveNext
End If
Loop

rst_local.Close

Thanks for your reply



Answer this question

Problem with deleting records

  • mryufy

    Thanks so much for your suggestion it worked.I am so thankful to you...i have been struggling with this since so many days
  • samspade

    Try replacing

    dbs.Execute ("delete AttId from sysDeptDef where DeptId='" & CurrentDeptId & "'")

    with

    rst_local.delete

    This should delete each of the entries as encountered. I think you have deleted the current and next record in rst_local with the above, so movenext moves onto a deleted record.


  • Problem with deleting records