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?
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:
try

DoCmd.OpenForm " Form_Projects", , , "[Date] >= Date()-10 And [Date] <= Date()+30"
0
Dale FyeCommented:
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

0

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 &"#""
0
Paul McCabeAuthor Commented:
Roy's first solution and Dale's suggested solution worked fine. Dale, your points are well made. Thank you both !
0
Dale FyeCommented:
glad to help.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.