Home > Back-end >  syntax error in insert statement access database
syntax error in insert statement access database

Time:07-26

Try
        con.Open()
        sql = "INSERT INTO into Services(Service Name,Service Cost,Description)values('" & txtname.Text & "'," & Val(txtcost.Text) & "," & txtdes.Text & ")"
        cmd.Connection = con
        cmd.CommandText = Sql
        i = cmd.ExecuteNonQuery
          If i > 0 Then
            MsgBox("New record has been inserted successfully!")
        Else
            MsgBox("No record has been inserted successfully!")
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        con.Close()
    End Try

CodePudding user response:

Try with:

sql = "Insert Into Services ([Service Name], [Service Cost], [Description]) Values('" & txtname.Text & "'," & txtcost.Text & ",'" & txtdes.Text & "')"
    

CodePudding user response:

You have to learn how to create table names and table columns first, if you give space in table columns then need to use square bracket in all queries, instead of space why cant use underscore "_" between two words ?

Two ways to solve your problem.

  1. [Service Name], [Service Cost] .... give square bracket or
  2. Service_Name, Service_Cost .... give underscore between two words in table columns in table design and no need to give square bracket in all SQL statement.

And one more thing always use command with parameters in all SQL statement.

From your stackoverflow name i thing your are now schooling so starting onwards change the way.

  • Related