0

My program works perfectly, but now the only problem is that the when the database has been updated, an alert box will appear saying:

Syntax error (missing operator) in query expression 'ID='.

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim choice As String
    choice = MsgBox("Do you really want to delete?", MsgBoxStyle.YesNo)
    If choice = vbYes Then
        cnn = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database.accdb")
        Dim str As String = ("DELETE FROM TableName WHERE ID= " & ComboBox1.Text & "")
        command = New OleDb.OleDbCommand(str, cnn)
        da = New OleDb.OleDbDataAdapter(command)
        cnn.Open()
        command.ExecuteNonQuery()
        Form1.Timer1.Enabled = True
        clearfields()
        Form1.Timer1.Enabled = False
        Me.Close()
    End If

    cnn.Close()

End Sub

It says that the error is located here: (ID is an Autonumber.)

Dim str As String = ("DELETE FROM TableName WHERE ID= " & ComboBox1.Text & "")

Thank you so much!

3 Answers3

0
Dim str As String = ("DELETE FROM TableName WHERE ID= '" & ComboBox1.Text & "'")

You need single quotes for the value

sabre
  • 207
  • 6
  • 18
0

First, you don't encapsulate string assignments in parentheses, and you must surround the query text in quotes.

Dim str As String = "DELETE FROM TableName WHERE ID= '" & ComboBox1.Text & "'"

Be careful. Your code opens you up to SQL Injection queries. Whatever is in ComboBox1.Text will be parsed as a SQL Command. If the ComboBox ever recieves text from a user, you must sanitize the input. Imagine how much fun you'd have if the user managed to put this text in your textbox

' OR 1=1 OR 'a' =';

So the database would process the command

DELETE FROM TableName WHERE ID= '' OR 1=1 OR 'a' =''

That command will delete all of your records.

Andrew Neely
  • 908
  • 11
  • 19
0

I fix my own problem, what I did is I changed my ID to text, so I will not have problems in the command.

Dim str As String = ("DELETE FROM TableName WHERE ID= " & ComboBox1.Text & "")

And then I had a problem in the timer, it won't start. I retyped the code and fixed some mistakes. Now my program runs perfectly and smoothly! Thank you all for your help, I really appreciate your answers.