Solved

Access database won't remove 2nd rows

Posted on 2013-01-14
4
185 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now