views:

282

answers:

4
+1  Q: 

vba sql problem

Below query is showing error

Please help :

DoCmd.RunSQL ("insert into tbltesting (IsDiff)values ('Yes') where empid= '" & Me.txtEmpId.Value & "' and testid= '" & Me.txtAutoNumber.Value & "'")
A: 

Does the empID record already exist in tblTesting? If so, you want an update:

UPDATE tblTesting
   SET IsDiff = 'Yes'
 WHERE empid = ...

and I won't get on my SQL injection soapbox...

Why do you have quotes around your empid/testid values? Are they strings or numbers? If numbers, watch out what you pass them to avoid conversion errors.

n8wrl
what is the error?
n8wrl
There is no error message but the control get stucked by showing a yellow color on the line all the textbox variables have values when i put my cursor on the variables
At that point press "F5" to continue. It will give you the text of the error message. OR : open the immediate window (by using VBA menu "View"->"Immediate Window" , and type in "Print Error" in the window.It will tell you where its hitting an error - hopefully some more details of the error.Make sure your table name is set up correct - Are you pointing to the right table? Did you try executing the same SQL directly?
blispr
This is the error message Data type mismatch in criteria expression.
IsDiff table datatype is "text" is that a problem ??
It says 'criteria expression' so I suspect you have a conversion error in one of empid or testid. I notice you're putting quotes around the values - are they numbers or strings?
n8wrl
THey are number not strings
Have you tried this - find out the values of Me.txtEmpId.Value and Me.txtAutoNumber.Value and execute the SQL statement with those values hard-coded? What happens then?
n8wrl
+1  A: 

Well, one problem is that your query is vulnerable to sql injection. Never never NEVER concatenate values from user inputs directly into a query string. Instead, use an ADO.Command object along with real query parameters or parameterized SQL executed with DAO or similar.

Here is an example.

Joel Coehoorn
Could you please show me an example about the way that i need to write query ?
Can you post a working example of sql injection when using jet?
Albert D. Kallal
Albert, have a look at this thread: http://stackoverflow.com/questions/512174/non-web-sql-injection/ -- I was very skeptical, too, and concluded that the only real vulnerability is with always-true WHERE clauses, such as True=True, because Jet/ACE can't execute multiple SQL statements (the usual worst-case SQL Injection scenario, e.g., "; DROP TABLE ...").
David-W-Fenton
Why would you recommend using an ADO command object instead of parameterized SQL executed with DAO? This is Access we're talking about here, where DAO is the correct first choice for data interface library.
David-W-Fenton
"DAO is the correct first choice" -- subjective. But I've added DAO to the answer.
onedaywhen
"the usual worst-case SQL Injection scenario, e.g., "; DROP TABLE ..." -- the scenario "DELETE FROM MyTable WHERE Name = 'x' = 'x';" is possible for the Access database engine and quite bad. I think that fact that SQL DDL cannot be injected is no excuse for creating vulnerabilities. Think of it a good practise :)
onedaywhen
+1  A: 

Insert is creating/adding a new row to the table - the record never existed. Update changes one or more fields in a record thats already saved in the table.

So an insert statement would not have a "where" clause - that would be an "update" statement you would use in this case.

You either need (if you are creating a brand-new record in your DB) -

DoCmd.RunSQL ("insert into tbltesting (IsDiff)values ('Yes') '")

Or you need (if you are changing some fields in an already-existing record ) -

DoCmd.RunSQL ("update tbltesting set IsDiff = 'Yes' where empid= '" & Me.txtEmpId.Value & "' and testid= '" & Me.txtAutoNumber.Value & "'")
blispr
Could you try - Me.txtAutoNumber.Value.Tostring(). Additionally.Could you : 1)Build a string say 'mySQLString' with the Update statement 2)MsgBox (mySQLString) and 3) DoCmd.RunSQL(mySQLString) ?Finally, Whatever you get in the MsgBox, copy it and execute it *outside vba* i.e. directly against the DB to check what the error is ? Hope that helps.
blispr
+2  A: 

I'm going to guess that empid and testid are numeric, and you're setting them off like they're strings in the SQL statement. Remove the single-quotes that you've wrapped around your field references.

DoCmd.RunSQL (" Update tbltesting set IsDiff ='Yes' where empid= " & Me.txtEmpId.Value & " and testid= " & Me.txtAutoNumber.Value & ";")
David Walker
Yes currently the error gone away , but the rows udates it shows as "Zero"
it says Zero rows updated instead one row updated
and i am sure that there is a record in the table
Run this as a check to make sure your fields have the data that you think they have: DoCmd.RunSQL (" SELECT * FROM tbltesting WHERE empid= " ") Incidentally, you can leave off the .Value portion.
David Walker