Click here to Skip to main content
14,981,532 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when i insert "let's" in my acces table i get an error

the problem is single quete

how can insert this data in my table

What I have tried:

dbscon.Execute "INSERT INTO UYUM(A,B,B,D,E) VALUES ('" & RS(0) & "','" & RS(1) & "','" & RS(2) & "','" & RS(3) & "','" & RS(4) & "' )"
Updated 2-Oct-20 4:35am

Simple solution...use parameters and the problem will go away.

scon.Execute "INSERT INTO UYUM(A,B,B,D,E) VALUES (@valA,@valB,@valC,@valD,@valE)"

If you happen to be using oledb it would look like this:
<pre>scon.Execute "INSERT INTO UYUM(A,B,B,D,E) VALUES (?,?,?,?,?)"

For OLEDB, the order of the parameters is critical.
Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
A perfectly valid "delete the table" command
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Fix that through your whole app, and the problem you have noticed will vanish at the same time ...
Member 14588284 1-Oct-20 13:13pm
you send always this message?
This is not my answer you know. thanks for advise but i must send my data to acces like this
OriginalGriff 1-Oct-20 13:54pm
Only when people are making the same old, tired mistake. As you are.

And yes, it is your problem. Replace "Baker's Wood" in my example with "let's" and see what happens to the query string.

And if you've been given this advice before and ignored it ... well, you tell me ...
First, I would try to replace:
dbscon.Execute "INSERT INTO UYUM(A,B,B,D,E) VALUES ('" & RS(0) & "','" & RS(1) & "','" & RS(2) & "','" & RS(3) & "',':" & RS(4) & "' )"

' pay attention field name           v here
dbscon.Execute "INSERT INTO UYUM(A,B,C,D,E) VALUES ('" & RS(0) & "','" & RS(1) & "','" & RS(2) & "','" & RS(3) & "',':" & RS(4) & "' )"

Using the same field 2 times in an insert is not a good idea !
Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
Probably not what you want to hear either, but ... guidance on the construction of a string in TSQL. Here's what I did with the code you posted:
DECLARE @value  varchar(256)
--SET @value ='" & RS(0) & "','" & RS(1) & "','" & RS(2) & "','" & RS(3) & "',':" & RS(4) & "' -- Msg 102, Level 15, State 1, Line 14 Incorrect syntax near ','.
SET @value = '" & RS(0) & "'',''" & RS(1) & "'',''" & RS(2) & "'',''" & RS(3) & "'','':" & RS(4) & "'
PRINT @value
Not hard at all, right?
" & RS(0) & "','" & RS(1) & "','" & RS(2) & "','" & RS(3) & "',':" & RS(4) & "
And now I recall why it doesn't pay to offer help (any at all) in QA ...
Aside from the first three answers which are trying to help you keep your data away from attacks, there's something I do for all free-form user input:

To insert a ' into most databases (SQL, MySQL, MariaDB, etc) you need to double it.
That is, use '' instead of ' where '' is two single quotes immediately next to one another. There's a long-term solution you should consider:

in no particular language, but it should be clear:

inputString = replace("'","''",inputString);

Have all of your string input pass through this filter before trying to insert/update a database field. Always. What you lose in efficiency is minor (saving a test and just do them all: sometimes even more efficient!). This way you are not going to have to worry about such surprises.

Works for me . . .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900