Filtering a Continuous Form by Date Ranges in VBA

I am an MS Access beginner using version 2013. A button opens a continuous form that I would like to filter for records in the range of ten days before the current date and 30 days after the current date (i.e. a 40-day period). I have tried various pieces of code for the button's on-click event, including the following, but just cannot get it to work.

DoCmd.OpenForm " Form_Projects", , , "Date= Between Date()-10 And Date()+30"

The expression Between Date()-10 And Date()+30 works fine in queries, but not in VBA, as I keep getting an error message telling me the code structure is wrong. Could anyone help point me in the direction of the correct VBA expression that should be used ?
Paul McCabeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rey Obrero (Capricorn1)Commented:

DoCmd.OpenForm " Form_Projects", , , "[Date] >= Date()-10 And [Date] <= Date()+30"
Dale FyeOwner, Developing Solutions LLCCommented:
1.  if you have a field called [Date] I would strongly recommend you change that.  "Date" is a reserved word in Access and will ultimately result in problems for you down the road.  Change it to something meaningful (BirthDate, InstallDate, DeliveryDate).

2.  I always recommend putting my criteria in a variable, which allows me to see what the OpenForm method is going to see. before it gets to that step.  Next, you cannot have the "=" and "Between" in the same criteria string.  Finally, I would recommend that you convert "Date() -10" into a value.
Dim strCriteria as string
strCriteria = "[DateField] BETWEEN #" & Date() - 10 & "# AND #" & Date() + 30 & "#"
debug.print strCriteria
docmd.OpenForm "Form_Projects",,, strCriteria

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rey Obrero (Capricorn1)Commented:
or possibly

DoCmd.OpenForm " Form_Projects", , , "[Date] >= "#" & Date()-10 & "#" And [Date] <= "#" & Date()+30 &"#""
Paul McCabeAuthor Commented:
Roy's first solution and Dale's suggested solution worked fine. Dale, your points are well made. Thank you both !
Dale FyeOwner, Developing Solutions LLCCommented:
glad to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.