Using a function as a query date range criteria

Generally, I will use functions to pass variables to queries.

However, I am having a unique problem (or senior moment <grin>) and the string that I am passing to the query is resulting a "type mismatch" error when the query executes.

Here is my function:
Public Function StudyDateRange() As String
Dim strTheString As String
Dim strTheStartDate As String
Dim strTheEndDate As String
    strTheStartDate = DLookup("dteStudyStartDate", "tblCF_StudyDateRange", True)
    strTheEndDate = DLookup("dteStudyEndDate", "tblCF_StudyDateRange", True)
    strTheString = "Between #" & strTheStartDate & "# And #" & strTheEndDate & "# Or Is Null"
    StudyDateRange = strTheString
End Function

In the query criteria of a date filed, I coded: StudyDateRange()

What am I doing wrong?

Lenny Gray
LVL 10
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.

Your test (DLOOKUP) is asking if dteStudyStartDate in tblCF_StudyDateRange is True.

I think you may be looking to use the dmax() and dmin() funciton.
The criteria is being interpreted as a literal string, not as an expression.
I'm not sure how you would convert it into a valid criteria expression using a function.
Have you tried to use:

Between  DLookup("dteStudyStartDate", "tblCF_StudyDateRange") and
DLookup("dteStudyEndDate", "tblCF_StudyDateRange")

DimitrisSenior Solution ArchitectCommented:
I think that in the place holder " ##########" you need to add the column name

 strTheString = "Between #" & strTheStartDate & "# And #" & strTheEndDate & "# Or ########## Is Null"
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

LennyGrayAuthor Commented:
Brandon -

The table is a single-record table. So the criteria being forced to True is valid.

Peter57r -

I tried your solution to no avail. Even after I changed it to:    

strTheString = "Between " & DLookup("dteStudyStartDate", "tblCF_StudyDateRange") & " and " & DLookup("dteStudyEndDate", "tblCF_StudyDateRange")
That's not my solution.
My solution was to use the expression in the criteria cell - not to assign it via a string.

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
LennyGrayAuthor Commented:
Oh, thanks Peter, but I know that I could do that already.

I am looking for a generic (function-based) criteria that I can use at will.

The user defines a study period, which is a date range that I apply to pre-written queries. By passing a function as the criteria, I am able to keep the same query and let it be altered by the function values at runtime.

LennyGrayAuthor Commented:
Thanks, Peter. I misunderstood. When I applied your approach, it gave me the flexibility that I was seeking with the function.

Sorry for the mis-read on my part!
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.