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
Asked
Active
Viewed 41 times
-1

Scott Hunter
- 48,888
- 12
- 60
- 101
-
You have not asked a question. – Scott Hunter Jul 25 '22 at 18:07
-
Does this answer your question? [SQL Query Syntax Error - Spaces in Field Names](https://stackoverflow.com/questions/7984124/sql-query-syntax-error-spaces-in-field-names) – 41686d6564 stands w. Palestine Jul 25 '22 at 18:19
-
1) Surround your column names with `[` and `]` (at least the ones with spaces). 2) Your code is subject to [SQL Injection](https://bobby-tables.com/). – 41686d6564 stands w. Palestine Jul 25 '22 at 18:21
-
If the last field, `Description`, is a Text datatype, then the value to be inserted also needs to have single quotes around it. – Applecore Jul 25 '22 at 18:38
2 Answers
0
Try with:
sql = "Insert Into Services ([Service Name], [Service Cost], [Description]) Values('" & txtname.Text & "'," & txtcost.Text & ",'" & txtdes.Text & "')"

Gustav
- 53,498
- 7
- 29
- 55
-
1While I agree you've technically answered the question, wouldn't it be a far more robust solution to show usage of parameters? – Hursey Jul 26 '22 at 00:01
-
@Hursey: Yes, but I didn't have the time. Many examples and tutorials can be looked up. – Gustav Jul 26 '22 at 08:38
0
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.
- [Service Name], [Service Cost] .... give square bracket or
- 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.

Aravind Aravind
- 179
- 1
- 10