|
-
Mar 4th, 2010, 10:12 AM
#1
Thread Starter
Hyperactive Member
[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()"
-
Mar 4th, 2010, 10:18 AM
#2
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
-
Mar 4th, 2010, 10:31 AM
#3
Re: [2008] Deleting From Access DB
 Originally Posted by Jonny1409
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
-
Mar 4th, 2010, 10:33 AM
#4
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|