So i'm trying to make a phonebook for my project. I can't seem to get the update code. It keeps on returning the error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1. Which I already know.
Why is it not possible to update only one column?
I have columns: name, tel#, mobile#, address
Here's my code for updating my selected column that returns the error
Dim sqlvalue As String = "update entries set Tel. # =
('" & txtNewTel.Text & "') where name = '" & txtName.Text & "'"
You need to enclose your field name Tel. # in backticks so MySQL will recognize this is a field and not some nonsensical formula or string.
Dim sqlvalue As String = "UPDATE entries SET `Tel. #` = '" & txtNewTel.Text & "' WHERE `name` = '" & txtName.Text & "'"
I also added backticks around the field name since that is often times a protected key word. Better safe than sorry.
Related
Getting the error data type conversion error running this code.
the data is from text boxes set to data type rich text.
CurrentDb.Execute "UPDATE tblISPServices", "SET Location=& Me.cboLocation,ServiceType = & Me.cboSType, BBProvider = & Me.txtBBProvider" & "WHERE ID= & Me.txtID.Tag"
please help
Remember to get variable or control values from the VBA code and rest should be string inside double quotes.
When generating such SQL statement to execute remember 3 rules
1) String data type fields should have single quote for string value so I had put single quote before & after location for e.g. '" & Me.cboLocation & "'
2) Number data type do not need single quote, so remove single quote for ID field for e.g. ID= " & Me.txtID.Tag
3) Date data type will have # instead of single quote for e.g. #" & Now() & "#
My assumption is , in the table tblISPServices field Location is Text Data type, ServiceType is Text data type, DBProvider is Text data type and ID is Numeric.
Observe how I made changes to get variable(control property) from VBA to generate String variable.
CurrentDb.Execute "UPDATE tblISPServices SET Location= '" & Me.cboLocation & "', ServiceType = '" & Me.cboSType & "', BBProvider = '" & Me.txtBBProvider & "' WHERE ID= " & Me.txtID.Tag
Note : remember while generating such string make sure you do not join any keyword to string or number for e.g. following is wrong SQL since it does not separate where
BBProvider = '" & Me.txtBBProvider & "'WHERE ID= " & Me.txtID.Tag
Correct SQL would be,
BBProvider = '" & Me.txtBBProvider & "' WHERE ID= " & Me.txtID.Tag
I have a table with 4 columns and update them through excel user form and all are varchar (255). when I try to enter the character ' I get syntax error, mysql doesn't accept it... What am I doing wrong here, do I need to change datatype
Update: I figured that the problem is not with MySQL (obviously :) ) but my code to update the table.
Dim sq As String
sq = "UPDATE sample.`nov-21` SET `Site work being carried out`='" & sitecombo.value & "',`Group`='" & eqgrp.value & "',`Description`='" & desc.value & "',`T Number`='" & tn.value & "', WHERE sample.`nov-21`.`ID`= " & Me.IDnum & ";"
Escape ' with \ or another ': 'O''Malley' or 'O\'Malley'
When you write 'O'Malley', MySQL read string literal 'O' followed by name Malley (which is not recognized) and a ' with no meaning, hence the syntax error.
I need some help. I use a combo box to create a sql query. Here is the query string
SQlstr = "UPDATE maindata SET " _
& "?theoption1 = ?therec1 " _
& "WHERE acct = ?theaccount AND clientnumber=?theclient;"
?theoption1 is a field name but it comes out with an apostrophe like 'xxxxx' , and this causes an error. How do I get around this. I cant use Replace because I am using parameters.
Thanks for any help I can get.
I have a form, and I want to fill it, and then save some of the fields into an existing table called Order.
I'm trying to do this with this line of code:
CurrentDb.Execute "INSERT INTO Order (OrderNumber)" & " VALUES (' " & Me.order & " ')"
I have also tried it like this
CurrentDb.Execute "INSERT INTO Order (OrderNumber)" & " VALUES ( " & Me.order & " )"
but it doesn't seem to make a difference. I keep getting the following error:
run-time error '3134': syntax error in INSERT INTO statement.
what am I doing wrong?
Order is a reserved word. If you must keep that as the table name, bracket it to avoid confusing the db engine.
Dim strInsert As String
strInsert = "INSERT INTO [Order] (OrderNumber) VALUES ('" & Me.order & "')"
Debug.Print strInsert
CurrentDb.Execute strInsert, dbFailOnError
If OrderNumber is numeric data type instead of text, discard those single quotes from the INSERT statement.
Store your statement in a string variable. Then use Debug.Print to examine the completed statement you're asking the engine to execute. You can view the Debug.Print output in the Immediate window. Go there with Ctrl+g Copy the statement and paste it into SQL View of a new Access query for troubleshooting.
running a vb application with the following code. I keep getting an error on my 'INSERT INTO' sql query, can anyone see what im doing wrong? This is the error - Syntax error in INSERT INTO statement.
connetionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = C:\Users\Dave\Documents\joblist.mdb;"
connection = New OleDb.OleDbConnection(connetionString)
Sql = "INSERT INTO jobList (StaffID, staffName, staffLastName, note, fault, section, techID, jobcomplete) VALUES ('" & staffid & "','" & staffFN & "','" & staffLN & "','" & staffNotes & "','" & staffFault & "', '" & staffSection & "', '" & techId & "','" & ava & "')"
connection.Open()
oledbAdapter.UpdateCommand = connection.CreateCommand
oledbAdapter.UpdateCommand.CommandText = Sql
oledbAdapter.UpdateCommand.ExecuteNonQuery()
connection.Close()
Me.JobListTableAdapter.Fill(Me.JoblistDataSet2.jobList)
Note and Section are reserved words in Jet SQL
You need to encapsulate them with square brackets
Sql = "INSERT INTO jobList (StaffID, staffName, staffLastName, [note], fault, " +
"[section], techID, jobcomplete) VALUES (......)"
This is the source of you syntax error, but .....
aside from that, you have many problems here:
As pointed out by Tim Schmeiter, you use the update command instead of
insert command.
you concatenate input text from your user to form an sql string. This
leads to sql injection attacks and problems in correct parsing of text
(apostrophes, invalid dates, invalid numbers, etc)
staffID and techID seems to be numeric fields in the database, but
you put their values inside single quotes like strings. If they are
numerics you will get another possible error there.
I assume you should use the InsertCommand instead of the UpdateCommand property since you are inserting.
oledbAdapter.InsertCommand.CommandText = "INSERT INTO jobList (StaffID, staffName, ....
Note that you're open for SQL-Injection and should use Parameters instead. You should also use Using statement to ensure that the connection gets closed even on error.