Paul Williams Paul Williams - 4 months ago 21 Question

Using the Replace() function in a parameterized query

I have the below mentioned code trying to perform an update/replace function on each row in the database where it finds the "Search" Pattern being stored in the parameter. However, when I run the query in the VB.NET program, the program is skipping over the row that it is supposed to be hitting.

For example, say the

variable is set to 4. And within the
table is a value
. The SQL should be replacing the first
but it is not. In fact, nothing is being changed. Even if the value is a sole

Dim sqlClearQuestion As New OleDb.OleDbCommand("UPDATE QuestionStacks SET QuestionData = REPLACE([QuestionData], "";@0;"", "";0;"") ", DBConnection)
sqlClearQuestion.Parameters.AddWithValue("@0", intQuestionID)

I've checked this query by running the SQL in the Query Builder in Access 2016 and it works fine as intended. However, attempting to use it in this VB.NET function, it is not yielding the desired result. What change is needed for this?

N.B. I know that
on Access SQL isn't needed, and is actually changed to something else (Read: But with only one parameter, there shouldn't be any problem with this.


One should always be suspicious when they see delimiters or any other decoration surrounding a parameter placeholder. Parameter placeholders should appear alone, with any required special characters in the parameter value. For example, a very common mistake is to try and use

' no good
cmd.CommandText = "SELECT * FROM Students WHERE FirstName LIKE '?%'"
cmd.Parameters.AddWithValue("?", theName)

when the correct approach is to use

' good
cmd.CommandText = "SELECT * FROM Students WHERE FirstName LIKE ?"
cmd.Parameters.AddWithValue("?", theName & "%")

In your particular case, you'll want to use

cmd.CommandText = "UPDATE QuestionStacks SET QuestionData = REPLACE([QuestionData], ?, "";0;"") "
cmd.Parameters.AddWithValue("?", ";" & intQuestionID & ";")