Solved

Access database won't remove 2nd rows

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
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…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
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…

760 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

19 Experts available now in Live!

Get 1:1 Help Now