Results 1 to 4 of 4

Thread: [RESOLVED] [2008] Deleting From Access DB

  1. #1

    Thread Starter
    Hyperactive Member Jonny1409's Avatar
    Join Date
    Mar 2005
    Posts
    308

    Resolved [RESOLVED] [2008] Deleting From Access DB

    Hello,

    I'm trying to delete a record from my access db using code and am getting an error message.

    My code is as follows:
    Code:
            Dim mDataPath As String = Application.StartupPath
            Dim strSelectedItem As String = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value
    
            Try
                Using connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & mDataPath & "\DB.mdb")
                    Using command As New OleDbCommand("DELETE * FROM tbl_Data WHERE ID = '" & strSelectedItem & "'", connection)
                        connection.Open()
                        command.ExecuteNonQuery()
                    End Using
                End Using
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
    The error I'm getting is "Data type mismatch in criteria expression." and it's on the line "command.ExecuteNonQuery()"

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2008] Deleting From Access DB

    Is ID a text field or a number? If it is a number remove the single qoutes around strSelectedItem

    Also remove the * should just be DELETE FROM table Where Condition
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [2008] Deleting From Access DB

    Quote Originally Posted by Jonny1409 View Post
    Hello,

    I'm trying to delete a record from my access db using code and am getting an error message.

    My code is as follows:
    Code:
            Dim mDataPath As String = Application.StartupPath
            Dim strSelectedItem As String = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value
    
            Try
                Using connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & mDataPath & "\DB.mdb")
                    Using command As New OleDbCommand("DELETE * FROM tbl_Data WHERE ID = '" & strSelectedItem & "'", connection)
                        connection.Open()
                        command.ExecuteNonQuery()
                    End Using
                End Using
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
    The error I'm getting is "Data type mismatch in criteria expression." and it's on the line "command.ExecuteNonQuery()"
    What version of VS are you using? If you're using 2005 or newer you should be using an xsd file for this, you can add your delete query via the designer then simply call the delete method and pass it the ID and it'll do it for you.

    Also, there's nothing wrong with "DELETE * FROM....." either
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  4. #4

    Thread Starter
    Hyperactive Member Jonny1409's Avatar
    Join Date
    Mar 2005
    Posts
    308

    Re: [2008] Deleting From Access DB

    Cheers GaryMazzone, that worked a treat !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width