Syntax for putting a varible in a date in SQL

Posted on 2015-01-18
Last Modified: 2015-01-18
I need to know the syntax for putting a date in SQL in Access with a variable.

This is what i want:

dim vYear as Date
vYear = Year(Now)

WHERE FinalDate = 1/1/vYear

vYear is the of course the variable.

Question by:ScootterP
  • 3
  • 2
  • 2
LVL 119

Expert Comment

by:Rey Obrero
ID: 40556655
try this

WHERE FinalDate = dateserial(year(now()),1,1)

Author Comment

ID: 40556663
I am trying to find the data parameters for each year for year to date reporting so the whole WHERE column is this dummy code.

WHERE FinalDate Between 1/1/vYear and 12/31/vYear

What i currently have is this:

WHERE FinalDate Between 1/1/2014 and 12/31/2014

And i was updatting so I would not have to change my code every January with the new year

Also the variable vYear could be the current year or it could be the previous month's year when it is january.
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 250 total points
ID: 40556673
then you need to use this

WHERE FinalDate = Between dateserial(year(now()),1,1)  and  dateserial(year(now()),12,31)
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

LVL 49

Accepted Solution

Gustav Brock earned 250 total points
ID: 40556717
>  it could be the previous month's year when it is january.

Then it would be:

WHERE FinalDate Between
    DateSerial(Year(DateAdd("m", -1, Date())),1,1)
    DateSerial(Year(DateAdd("m", -1, Date())), 12, 31)


Author Comment

ID: 40556741
Gustav is correct.  That is why I was hoping to just add variable vYear after 1/110 or 12/31/ that way I would not have to add the longer SQL statement all throughout out my, I could do it once when the form is opened.  So there is no way to do this?


Author Closing Comment

ID: 40556751
Thanks, I can use the code once when the form opens and store that in a variable for the from and to date.

LVL 49

Expert Comment

by:Gustav Brock
ID: 40557129
You are welcome!


Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now