Solved

Access database won't remove 2nd rows

Posted on 2013-01-14
4
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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…
Suggested Courses

623 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