Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Access database won't remove 2nd rows

Posted on 2013-01-14
4
Medium Priority
?
193 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 2000 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

885 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