簡體   English   中英

絕對無法在我的代碼中從Access DB刪除記錄(整行)

[英]Absolutely cannot delete a record(entire row) from Access DB in my code

我刪除了大部分原始帖子,因為我在下面所說的內容使我更加接近了。 但是我跳了一下槍。 以下是我所處的位置,遇到了一個新問題,但我認為這是一個小問題,但似乎看不到它在哪里。 這是一個並發沖突,據我所知,我顯然試圖兩次將其保存到數據庫,但看不到我在哪里執行此操作。 在添加記錄保存期間,我只調用一次update方法。 因此,發生的事情是我添加了一條記錄,然后保存了它,然后刪除了該記錄,只是通過在MS Access中查看該記錄來查看是否正在刪除該記錄,它可以使記錄消失。 然后,我再次添加記錄以查看記錄是否又回來,並得到錯誤消息。

需要注意的一件事是,我正在使用BindingNav來確定要刪除的當前記錄數/位置,我在原始帖子中說,由於輸入錯誤,最后一條記錄很有可能被刪除。

更新:我可以相信! 當我發布這個荒唐的請求尋求幫助之后,我便將其釘牢了。我獲得靈感並找到我真正需要或正在尋找的東西。 這是最終起作用的代碼:(或者我認為)

    Dim row_to_delete As String, Current_Record As Integer

    Current_Record = All649_BS.Count
    row_to_delete = "DELETE FROM [All 649] WHERE(DrawNumber = " & Current_Record & ")"

    All649_BS.RemoveCurrent()
    All_649TableAdapter.Adapter.DeleteCommand.CommandText = row_to_delete

    Try
        All_649TableAdapter.Delete()
        ErrorMsgs.Text = ("Update successful")

    Catch ex As Exception

        ErrorMsgs.Text = ("Update failed")

    End Try

End Sub

這是添加記錄和保存部分的代碼。

Private Sub Add_Rcrd_Click(sender As Object, e As EventArgs) Handles Add_Rcrd.Click

    ErrorMsgs.Text = ""

    WkDay = All649_BS.Current(10)
    All649_BS.AddNew()                'Add the new enry

    DrawNumber.Text = All649_BS.Count
    Actual_Count.Text = All649_BS.Count

    If WkDay = "WED" Then
        Weekday.Text = "SAT"
    Else
        Weekday.Text = "WED"
    End If

    DateField.Focus()

End Sub

Private Sub Save_Record_Click(sender As Object, e As EventArgs) Handles Save_Record.Click

    Dim a1 As Int32
    Dim b2 As Int32
    Dim c3 As Int32
    Dim d4 As Int32
    Dim e5 As Int32
    Dim f6 As Int32

    'Check for entry errors

    If DateField.Text = "" Then              ' Check to see a date was enetered
        ErrorMsgs.Text = "Please enter a date !"
        DateField.Text = ""
        DateField.Focus()
        Exit Sub
    End If

    If Pos1.Text = "" Or Pos1.Text < 1 Or Pos1.Text > 49 Then   ' Check 1st number entry
        ErrorMsgs.Text = "You have forgotten to enter 1st number or the number is invalid !"
        Pos1.Text = ""
        Pos1.Focus()
        Exit Sub
    End If

    If Pos2.Text = "" Or Pos2.Text < 1 Or Pos2.Text > 49 Then   ' Check 2nd number entry
        ErrorMsgs.Text = "You have forgotten the 2nd number or the number is invalid !"
        Pos2.Text = ""
        Pos2.Focus()
        Exit Sub
    End If

    If Pos3.Text = "" Or Pos3.Text < 1 Or Pos3.Text > 49 Then   ' Check 3rd number entry
        ErrorMsgs.Text = "You have forgotten the 3rd number or the number is invalid !"
        Pos3.Text = ""
        Pos3.Focus()
        Exit Sub
    End If

    If Pos4.Text = "" Or Pos4.Text < 1 Or Pos4.Text > 49 Then   ' Check 4th number entry
        ErrorMsgs.Text = "You have forgotten the 4th number or the number is invalid !"
        Pos4.Text = ""
        Pos4.Focus()
        Exit Sub
    End If

    If Pos5.Text = "" Or Pos5.Text < 1 Or Pos5.Text > 49 Then   ' Check 5th number entry
        ErrorMsgs.Text = "You have forgotten the 5th number or the number is invalid !"
        Pos5.Text = ""
        Pos5.Focus()
        Exit Sub
    End If

    If Pos6.Text = "" Or Pos6.Text < 1 Or Pos6.Text > 49 Then   ' Check 6th number entry
        ErrorMsgs.Text = "You have forgotten the 6th number or the number is invalid !"
        Pos6.Text = ""
        Pos6.Focus()
        Exit Sub
    End If

    If Bonus.Text = "" Or Bonus.Text < 1 Or Bonus.Text > 49 Then   ' Check 6th number entry
        ErrorMsgs.Text = "You have forgotten the Bonus number or the number is invalid !"
        Bonus.Text = ""
        Bonus.Focus()
        Exit Sub
    End If

    a1 = Val(Pos1.Text)
    b2 = Val(Pos2.Text)
    c3 = Val(Pos3.Text)
    d4 = Val(Pos4.Text)
    e5 = Val(Pos5.Text)
    f6 = Val(Pos6.Text)
    Sumbox.Text = a1 + b2 + c3 + d4 + e5 + f6

    Try

        Validate()
        All649_BS.EndEdit()
        All_649TableAdapter.Update(All649_DataSet)
        ErrorMsgs.Text = ("Update successful")

    Catch ex As Exception

        ErrorMsgs.Text = ("Update failed")

    End Try

    All649_BS.MoveLast()
    All649Nav.Focus()

End Sub

因此,顯然在此代碼中的某處進行了兩次更新。 記住,我添加了一條記錄,然后立即將其刪除以查看其是否正常運行,然后再次添加該記錄並得到錯誤。

在此先感謝您的任何建議。

感謝您的答復,我確實找到了解決方案。 我在這行中添加了以下內容:All649_DataSet.AcceptChanges(),在嘗試捕獲塊和中提琴的內部delete方法之后!

現在我遇到了自動編號BS的另一個問題,我禁用了它,現在我的記錄保存將不再起作用,因為我的主鍵就是那個,並自動填寫了我需要的第一個字段。 在該字段上使用自動編號並刪除記錄后,編號序列會混亂。 DOH !!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM