Solved

Access database won't remove 2nd rows

Posted on 2013-01-14
4
186 Views
Last Modified: 2013-01-17
I have a datset that I go through the rows, check the status of the row and do the appropriate change in the code attached.  I have a stored procedure that I send parameters to, when I go through the code, the first record is deleted, but as I go through the 2nd row to be deleted or any rows after that, it doesnt delete the other rows.  When I look at the parameter values they do look correct, but the row doesnt get deleted nor do I get a error.  Any suggestions would help.  i have attached the vb code as well as the access procedure.

Access procedure, again the first record works the 2nd doesnt.

PARAMETERS [@WONumber] Text ( 255 ), [@ItemID] Long;
DELETE *
FROM InspectionsDetails
WHERE (((InspectionsDetails.WO_Number)=[@WONumber]) AND ((InspectionsDetails.ItemID)=[@ItemID]));


thanks
SaveInspectionDetails.txt
0
Comment
Question by:mgmhicks
  • 2
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 38775153
>When I look at the parameter values they do look correct, but the row doesnt get deleted nor do I get a error

Does it work if you directly execute the stored procedure with those values?


By the way, you have some very inefficient code where you not only have duplicate code but you are also opening and closing database connections in a loop.
0
 

Author Comment

by:mgmhicks
ID: 38775371
I'll try and do it directly running the procedure in access.  Could you give me an example on how you would handle it.  I thought about filtering the row states instead.

thanks
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 500 total points
ID: 38775671
Without changing the logic, I would rewrite the code as below


Private Function SaveInspectionDetails(ByRef ds As DataSet) As Boolean
        Dim myCMD As New OleDbCommand
        Dim myDA As New OleDbDataAdapter
        Dim i As Integer = 0
        Dim myRow As DataRow
        myCMD.Connection = AccessConnection
        myCMD.CommandType = CommandType.StoredProcedure
        Try
            AccessConnection.Open()
            For i = 0 To ds.Tables(1).Rows.Count - 1
                myRow = ds.Tables(1).Rows(i)
                Select Case ds.Tables(1).Rows(i).RowState
                    Case Is = DataRowState.Added
                        With myCMD
                            .CommandText = "AddInspectionDetails"
                            .Parameters.Clear()
                            .Parameters.Add(New OleDbParameter("@ID", myRow.Item(0)))
                            .Parameters.Add(New OleDbParameter("@WO_Number", myRow.Item(1)))
                            .Parameters.Add(New OleDbParameter("@ItemID", myRow.Item(2)))
                            .Parameters.Add(New OleDbParameter("@ItemGroup", myRow.Item(3)))
                            .Parameters.Add(New OleDbParameter("@ItemDesciption", myRow.Item(4)))
                            .Parameters.Add(New OleDbParameter("@ItemQty", myRow.Item(5)))
                            .Parameters.Add(New OleDbParameter("@ItemCost", myRow.Item(6)))
                            .Parameters.Add(New OleDbParameter("@ItemPrice", myRow.Item(7)))
                            .Parameters.Add(New OleDbParameter("@ResidentCharge", myRow.Item(8)))
                            .Parameters.Add(New OleDbParameter("@LastEditDate", myRow.Item(9)))
                            .Parameters.Add(New OleDbParameter("@LastEditBy", myRow.Item(10)))
                            .Parameters.Add(New OleDbParameter("@ItemHours", myRow.Item(11)))
                        End With
                        Try
                            myCMD.ExecuteNonQuery()
                        Catch ex As Exception
                            MsgBox(ex.Message)
                            SaveInspectionDetails = False
                            Exit Function
                        End Try
                    Case Is = DataRowState.Modified
                        MsgBox("row modified")
                    Case Is = DataRowState.Deleted
                        '            MsgBox("row deleted")
                        With myCMD
                            .CommandText = "RemoveInspectionDetail"
                            .Parameters.Clear()
                            .Parameters.Add(New OleDbParameter("@WONumber", Trim(myRow.Item(1, DataRowVersion.Original))))
                            .Parameters.Add(New OleDbParameter("@ItemID", myRow.Item(2, DataRowVersion.Original)))
                        End With
                        myCMD.ExecuteNonQuery()
                        '  myRow.AcceptChanges()
                    Case Is = DataRowState.Unchanged
                        '            MsgBox("row unchanged")
                End Select
            Next
            SaveInspectionDetails = True

        Catch ex As Exception

            If AccessConnection.State = ConnectionState.Open Then
                AccessConnection.Close()
            End If
            MsgBox(ex.Message)
            SaveInspectionDetails = False
        End Try
    End Function

Open in new window

0
 

Author Closing Comment

by:mgmhicks
ID: 38787472
thanks
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question