all groups > vb.net data > january 2007 >
You're in the

vb.net data

group:

Problem running query in code


Problem running query in code John
1/15/2007 8:19:04 PM
vb.net data:
Hi

I am using the below code to run a sql on an underlying access table;

insStr = "INSERT INTO [Web Site Action Queue] ( Action, [Client ID],
[Operator], Request_Date ) " & _
"VALUES (""Modify Client"", 93, ""Administrator"", Now())"

Dim insCmd As New OleDbCommand(insStr, dbConContacts)

insCmd.ExecuteNonQuery()

I get a 'Syntax error in INSERT INTO statement.' error on the last line.

But if I run the same query as below directly in access it works fine.

INSERT INTO [Web Site Action Queue] ( Action, [Client ID], [Operator],
Request_Date ) VALUES ("Modify Client", 93, "Administrator", Now())

What is the problem and how can I fix it?

Thanks

Regards

Re: Problem running query in code John
1/15/2007 9:00:25 PM
Tried following, still no luck.

INSERT INTO [Web Site Action Queue] ( Action, [Client ID], [Operator],
Request_Date ) VALUES ("Modify Client", 93, "Administrator", "15/01/2007
20:57:54")

Works in Access but not from in vb.net ExecuteNonQuery().

Thanks

Regards

[quoted text, click to view]

Re: Problem running query in code Cor Ligthert [MVP]
1/16/2007 6:48:13 AM
John.

Try to use parameters especially for datetime

http://www.vb-tips.com/dbpages.aspx?ID=886bba68-8a2f-4b99-8f66-7139b8970071

It is at the bottom of the page

Be aware that the way it is showed is the most simple one as you will
probably never use it.

Cor

"John" <John@nospam.infovis.co.uk> schreef in bericht
news:OtvMnJOOHHA.2232@TK2MSFTNGP02.phx.gbl...
[quoted text, click to view]

Re: Problem running query in code Miha Markic [MVP C#]
1/16/2007 6:55:57 AM
You should use a parametrized sql statement instead, something like:
INSERT INTO [Web Site Action Queue] ( Action, [Client ID], [Operator],
Request_Date ) VALUES (?, ?, ?, ?)
And pass values as parameters. It will save you the trouble of formatting
and what's most important, it will protect you from sql injection attacks.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/



[quoted text, click to view]
Re: Problem running query in code Van T. Dinh
1/16/2007 7:48:45 AM
I think the problem is Now() is a VBA function and this is available in JET
only if you are running the Query in Access (via the Expression Service).

From the newsgroups you posted to, I guess you are running this in VB.Net
and I don't think JET would recognize the Now() used in your SQL.

Try with a literal date/time value and see if Now() is the cause.

Perhaps, the VB.Net experts can advise you of an alternative.

--
HTH
Van T. Dinh
MVP (Access)



[quoted text, click to view]

Re: Problem running query in code Van T. Dinh
1/16/2007 8:34:06 AM
Date literal in JET must be of the US format "mm/dd/yyyy hh:nn:ss" (or an
unambiguous format like "yyyy-mm-dd hh:nn:ss") and enclosed in hashes (#),
not double-quotes. Try:

INSERT INTO [Web Site Action Queue]
( [Action], [Client ID], [Operator], [Request_Date] )
VALUES
("Modify Client", 93, "Administrator", #01/15/2005 20:57:54#)

--
HTH
Van T. Dinh
MVP (Access)



[quoted text, click to view]

Re: Problem running query in code René_Jensen
3/4/2007 12:00:00 AM
Could it have something to do with your string notation you use " to
tell that it is a string try with ' instead.

René

[quoted text, click to view]
Re: Problem running query in code John Spencer
3/4/2007 9:51:27 AM
My guess is that NOW() is causing the problem. It is probably not
understood by the database engine. Try inserting the date and time as a
literal value using # (or perhaps ') as the delimiter.

Something like:

insStr = "INSERT INTO [Web Site Action Queue] ( Action, [Client ID],
[Operator], Request_Date ) " & _ "VALUES (""Modify Client"", 93,
""Administrator"", #" & Now() & "#)"

This of course assumes that you have NOW as an available function in
your code environment.

'====================================================
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
'====================================================


[quoted text, click to view]
Re: Problem running query in code RobinS
3/4/2007 10:03:23 AM
He's using Access, so Now() should work, and he should take out the #
delimiters.

And use single quotes around 'Modify Client' and 'Administrator'.

Robin S.
---------------------------------------
[quoted text, click to view]

AddThis Social Bookmark Button