Search code examples
vb.netms-accessdelete-row

Cannot delete a record from MS Access specifically of the type "TEXT"


I am trying to delete a record on the basis of my primary key tid from MS Access of the DataType TEXT.But the problem is, it doesn't get deleted.Where as there is no problem deleting a field of DataType int.Any reasons? the field tid is my primary key.

I made a mistake by making it of the type TEXT, now would like to convert it to int (in programmatic way) ? Without making changes to MS Access DB and bind it again?

Ask for the code if required. Any kind of help will be appreciated.

EDIT as requested by @Scotch : This is the code snip giving me problems.

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim delcmd As New OleDbCommand("delete from teacher where tid=" & TextBox1.Text & " ", conaccess)
        delcmd.ExecuteNonQuery()
        MsgBox("Record is deleted")
        loadGrid()
        TextBox1.Clear()
        TextBox2.Clear()

        loadGrid()
    End Sub

Were as, other insert operations are running just fine:

Example:

   Dim access As String = String.Format("INSERT INTO teacher (tid,tname,dept,type) VALUES('{0}','{1}','{2}','{3}')", TextBox1.Text, TextBox2.Text, ComboBox2.Text, ComboBox1.Text)
    concmd.Connection = conaccess
    concmd.CommandText = access
    concmd.ExecuteNonQuery()
    MsgBox("Record Successfully Saved")
    loadGrid()
    TextBox1.Clear()
    TextBox2.Clear()

Solution

  • Since your tid field is text, you need quotes in your delete statement.

      Dim delcmd As New OleDbCommand("delete from teacher where tid='" & TextBox1.Text & "'", conaccess)
    

    If you want to change the field type to a number, you can do that. It will warn you, but if you just have numeric characters in every record's tid field, it will work fine.