Syntax Error (1 Viewer)

mike60smart

Registered User.
Local time
Today, 16:44
Joined
Aug 6, 2017
Messages
1,909
Hi Everyone

What is wrong with the following please:-

Code:
strSQL = "UPDATE Order SET DateOrdered = Date() WHERE [Shopping Cart] = 0 And [PO]= 0;"
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:44
Joined
Feb 28, 2001
Messages
27,239
What error message did you get? Or how do you know it is wrong? What did you expect it to do that it doesn't do?

Giving us code without an error message or tangible symptom is like going to a doctor and saying "Doctor, it hurts when I slam the door on my finger." So would you be surprised to hear the doctor say, "Don't slam the door on your finger." ?? How about some symptoms?
 

mike60smart

Registered User.
Local time
Today, 16:44
Joined
Aug 6, 2017
Messages
1,909
What error message did you get? Or how do you know it is wrong? What did you expect it to do that it doesn't do?

Giving us code without an error message or tangible symptom is like going to a doctor and saying "Doctor, it hurts when I slam the door on my finger." So would you be surprised to hear the doctor say, "Don't slam the door on your finger." ?? How about some symptoms?
My apologies Doc_Man
The error is as follows:
The Immediate window shows the following:-

Code:
UPDATE Order SET DateOrdered = Date() WHERE [Shopping Cart] = 0 And [PO]= 0;
 

Attachments

  • error.jpg
    error.jpg
    8.6 KB · Views: 35

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:44
Joined
Feb 28, 2001
Messages
27,239
Might have to write that as

Code:
UPDATE Order SET DateOrdered = "#" & Format( Date(), "dd-MMM-yyyy" ) & "#" WHERE ...

Not going to swear on it, but the Date() function might return something unexpected in that context. So coerce it with the type coercion punctuation.
 

Edgar_

Active member
Local time
Today, 10:44
Joined
Jul 8, 2023
Messages
435
Isn't Order a reserved word?
Maybe try
Rich (BB code):
UPDATE [Order] SET DateOrdered = Date() WHERE [Shopping Cart] = 0 And [PO]= 0;
 

mike60smart

Registered User.
Local time
Today, 16:44
Joined
Aug 6, 2017
Messages
1,909
Hi Doc_Man & Edgar

Now get the following error and it highlights the first #
 

Attachments

  • error.jpg
    error.jpg
    38 KB · Views: 37

Josef P.

Well-known member
Local time
Today, 17:44
Joined
Feb 2, 2023
Messages
833
Have you tested Edgar's suggestion without the string concatenation with format?
 

Edgar_

Active member
Local time
Today, 10:44
Joined
Jul 8, 2023
Messages
435
Hi Doc_Man & Edgar

Now get the following error and it highlights the first #
If you're going to apply also Doc's suggestion, then maybe:
"UPDATE [Order] SET DateOrdered = #" & Format(Date, "dd-MM-yyyy") & "# WHERE [Shopping Cart] = 0 And [PO]= 0;"

EDIT: I simplified it a bit... but it should have worked without the formatting, as Josef points out. However, I hate working with date objects in ALL languages, so if that makes it more explicit, then great.
 
Last edited:

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:44
Joined
Feb 28, 2001
Messages
27,239
OK, nested quoting problem.

Code:
Set StrSQL = "UPDATE Order SET DateOrdered = '#' & Format( Date(), 'dd-MMM-yyyy' ) & '#' WHERE ...

The stuff after WHERE doesn't need quotes because they are numeric.
 

Josef P.

Well-known member
Local time
Today, 17:44
Joined
Feb 2, 2023
Messages
833
Formatting as a date string is not necessary. Date() can be processed by Jet/ACE.#
I bet: Code in #5 will work :)
 

mike60smart

Registered User.
Local time
Today, 16:44
Joined
Aug 6, 2017
Messages
1,909
Hi Everyone

Edgar's suggestion did the trick, another snippet to add to the Library

Code:
strSQL = "UPDATE [Order] SET DateOrdered = " & "#" & Format(Date, "dd-MM-yyyy") & "#" & " WHERE [Shopping Cart] = 0 And [PO]= 0;"
Many thanks for the help
 

Josef P.

Well-known member
Local time
Today, 17:44
Joined
Feb 2, 2023
Messages
833
BTW:
Code:
strSQL = "UPDATE [Order] SET DateOrdered = " & "#" & Format(Date, "dd-MM-yyyy") & "#" & " WHERE [Shopping Cart] = 0 And [PO]= 0;"
vs.
strSQL = "UPDATE [Order] SET DateOrdered = " & Format(Date, "\#yyyy-mm-dd\#") & " WHERE [Shopping Cart] = 0 And [PO]= 0;"
Allways format a SQL date string in ISO-Format (or as Format(Date, "\#mm\/dd\/yyyy\#"))=> then it works with any country setting.

In this case, however, I think the variant of #5 is better, as it is much easier to read.
Rich (BB code):
UPDATE [Order] SET DateOrdered = Date() WHERE [Shopping Cart] = 0 And [PO]= 0;
 

tvanstiphout

Active member
Local time
Today, 08:44
Joined
Jan 22, 2016
Messages
233
Date() works. Why?
Check this out:
It says in part: the VBA expression service offers over 100 VBA functions that you can use in SQL expressions
Alas, there is no list of functions that are supported that I know of. But Date() function is one of them, so use that rather than rolling your own date formatting.
 

cheekybuddha

AWF VIP
Local time
Today, 16:44
Joined
Jul 21, 2014
Messages
2,289
Mike, as many have suggested, leave Date() as you had it originally - don't format it here.

If you really do want to format it into the a literal date in your SQL string then do not use the format suggested by Edgar:
Code:
strSQL = "UPDATE [Order] SET DateOrdered = " & "#" & Format(Date, "dd-MM-yyyy") & "#" & " WHERE [Shopping Cart] = 0 And [PO]= 0;"
                                                                   ^^^^^^^^^^
                                                                   ||||||||||
                                                            Do NOT use this format!
Use the format suggested by @Josef P.:
Code:
strSQL = "UPDATE [Order] SET DateOrdered = " & Format(Date, "\#yyyy-mm-dd\#") & " WHERE [Shopping Cart] = 0 And [PO]= 0;"
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 11:44
Joined
May 21, 2018
Messages
8,555
Put the CSQL in your library and not worry about it.
 

ebs17

Well-known member
Local time
Today, 17:44
Joined
Feb 7, 2020
Messages
1,952
Formatting as a date string is not necessary. Date() can be processed by Jet/ACE.
The so-called Expression Service has been available since Access 2000 / Jet 4.0. Through this functions
- Access functions (Nz, DLookup)
- VBA functions (Left, Instr, Date())
- custom functions
processed directly by Jet/ACE when those functions do not require arguments from outside the query.
In a way, this is a substitute for the fact that the actual language range of Jet-SQL is very narrow.

Date() as a function (with brackets) can therefore be used directly, additional formatting is only cumbersome (more effort) and more error-prone in design.
Isn't Order a reserved word?
I agree. Has this been checked for yourself?
A little more care in your own designation regime avoids such problems.

The QBE wastefully uses brackets to escape labels. Apparently not wasteful enough.
Such brackets, which are actually unnecessary, bother me because I look at and analyze queries and these brackets make the overview massively difficult.
This would not have happened with tblOrders instead of Order.
Code:
strSQL = "UPDATE tblOrders SET DateOrdered = Date() WHERE ShoppingCart = 0 And PO = 0;"
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:44
Joined
Feb 19, 2002
Messages
43,371
"Doctor, it hurts when I slam the door on my finger."
It's worse than that, it is more like
"Doctor, It hurts when I slam the door."
strSQL = "UPDATE Order SET DateOrdered = Date() WHERE [Shopping Cart] = 0 And [PO]= 0;"
This should work without formatting the Date(). When you build SQL using VBA and it doesn't work, print the string out to the debug window, copy the string, and paste it into the QBE in SQL view and run it. You will frequently get a better error message.
Date() works. Why?
It works because the query engine knows what the Date() function is and it obtains the date when it executes the query. Where you run into trouble with dates is when you include STRING values in the SQL. If the date is a STRING in the SQL, it MUST be in standard US date format - mm/dd/yyyy or the unambiguous yyyy/mm/dd format. So string values always need formatting. You can also refer to a form control in the query without formatting for the same reason that Date() and Now() work - objects that are defined as date data types do NOT contain dates that are STRINGS, they contain dates as they are stored by the database - double-precision numbers. The integer portion of the number is the number of days since Dec 30, 1899 (for Jet and ACE, SQL Server uses Jan 1, 1900 as the 0 date). The decimal portion is the elapsed time since midnight.
So,
12/30/1899 12:00 PM = 0.5
12/31/1899 06:00 PM = 1.75
12/29/1899 06:00 AM = -1.25
 

Users who are viewing this thread

Top Bottom