What is the sysdate equivalent in Access docmd.runsql?

01-7700

Board Regular
Joined
Nov 7, 2011
Messages
62
I've tried sysdate in an sql statement, ie "select a,b,c from d where submitted_date > sysdate - 30;" and get an error - unknown function. I have googled for the answer and not found a reference to this. Does anyone know the answer? Thanks. :p
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Are you actually running this against an Access table or a SQL table?
 
Upvote 0
Try this modification:

Code:
strSQL = [COLOR=#333333]"select a,b,c from d where submitted_date > " & [/COLOR][B][COLOR=#ff0000]DATE[/COLOR][/B][COLOR=#333333] & "- 30;"
DoCmd.RunSQL strSQL
[/COLOR]
 
Last edited:
Upvote 0
Just use:
Code:
[COLOR=#333333]SELECT a,b,c FROM d WHERE submitted_date > Date() - 30;[/COLOR]
 
Upvote 0
Code:
strSQL = "select a,b,c from d where submitted_date > " & DATE & "- 30;"
DoCmd.RunSQL strSQL

If written in the vba environment, ... almost ... :)

(Joe is right if written in the normal Access GUI environment)
 
Upvote 0
Try this modification:

Code:
strSQL = [COLOR=#333333]"select a,b,c from d where submitted_date > " & [/COLOR][B][COLOR=#ff0000]DATE[/COLOR][/B][COLOR=#333333] & "- 30;"
DoCmd.RunSQL strSQL
[/COLOR]

good idea - but it is evaluating as 0 in this expression - i also tried using > #" & DATE & "# - 30;" - same effect
 
Upvote 0
good idea - but it is evaluating as 0 in this expression - i also tried using > #" & DATE & "# - 30;" - same effect


That's odd..... I added this to a test database containing email. It executes as expected....

Code:
[COLOR=#0000ff]Option Compare Database[/COLOR]


[COLOR=#0000ff]Sub[/COLOR] Test()


[COLOR=#0000ff]    Dim[/COLOR] strSQL [COLOR=#0000ff]As String[/COLOR]


    strSQL = "SELECT Inbox.Priority, Inbox.Subject, Inbox.From FROM Inbox WHERE submitted_date >" & Date & "-30;"


    DoCmd.RunSQL strSQL


[COLOR=#0000ff]End Sub[/COLOR]

Immediate Window:
Code:
?strSQL
SELECT Inbox.Priority, Inbox.Subject, Inbox.From FROM Inbox WHERE submitted_date >5/10/2016-30;

Glad you were able to solve your issue.
 
Upvote 0
Hi,

Well, technically 5/10/2016 - 30 is about -29.99 (5 divided by 10 divided by 2016 less 30). That should provide some results. But the correct way to write the string is with date literals enclosed in hashes:
Code:
strSQL = "SELECT Inbox.Priority, Inbox.Subject, Inbox.From FROM Inbox WHERE submitted_date > (#" & Date & "# - 30);"
result:
Code:
?strSQL
SELECT Inbox.Priority, Inbox.Subject, Inbox.From FROM Inbox WHERE submitted_date > (#5/10/2016# - 30);

Another minor detail DoCmd.RunSQL would require an action statement (Insert/Update/Delete) and I'm not sure it will do anything with Select.

The hashes (or lack thereof) might be less of an issue in the Access UI where Access will sometimes add them for you. But that won't be true if you are writing code in the VBA environment.
ξ
 
Upvote 0

Forum statistics

Threads
1,221,816
Messages
6,162,149
Members
451,746
Latest member
samwalrus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top