Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Developer Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
DateRange Function as criteria
Chris Pardy 
   
3 years ago
Hi I created a function that takes a single date as and input,
and outputs a string that I want to use in a query as search criteria. The string is a date range that comes out like this : Between #2023-04-17# AND #2023-04-21#

If I copy the function result right out of the immediate window  and paste is into the query grid it works as intended. But if I call the function from the query grid like this:
GetWorkWeekRange([TempVars![TVeD])
it either show no results, or gives an error. but it does successfully call the function because I can see the string in the immediate window.

the TempVars is created elsewhere but it feeds the function a date formated like this :

4/19/2023

The Function looks like this:


code
Public Function GetWorkWeekRange(inputDate As Date) As String

    Dim startDate As Date
    Dim endDate As Date
    Dim dayOfWeek As Integer
    dayOfWeek = Weekday(inputDate)
    If dayOfWeek = vbMonday Then
        'if the input date is a Monday, use the previous week's range
        startDate = inputDate - 6
        endDate = inputDate
    Else
        'otherwise, use the current week's range
        startDate = inputDate - dayOfWeek + 2
        endDate = startDate + 4
    End If
    GetWorkWeekRange = "Between #" & Format(startDate, "yyyy-mm-dd") & "# AND #" & Format(endDate, "yyyy-mm-dd" & "#")
    Debug.Print GetWorkWeekRange
    
End Function

Kevin Robertson  @Reply  
          
3 years ago
You are missing a bracket after TempVars.

GetWorkWeekRange([TempVars]![TVeD])

What error message do you get?
Chris Pardy OP  @Reply  
   
3 years ago
Hi Kevin.. oops that's a typo in the in the post but the it was correct in the software.
The error was something along the lines of 'this argument May be to complex' but I'll verify when I'm back at work lol.
Kevin Robertson  @Reply  
          
3 years ago
I tested your function and it works fine for me. Does it work if you use a static date?
Chris Pardy OP  @Reply  
   
3 years ago
Hi Kevin, I've tried a few more things.. but still no luck.

When You say static date to you just mean a singe date value ? if so Yes, in fact I can use my tempvars date as the criteria and that works to return the records from the singe day.

when I use the function pasted above, the error i get is : "This Expression is typed incorrectly, or is to complex to be evaluated..."

I Modified the function so the Function itself called the accessed the variable instead of using the query to pass it in, the query simply calls the function.. and in that case I get the error "Data Type Mismatch in criteria expression"
Chris Pardy OP  @Reply  
   
3 years ago

Chris Pardy OP  @Reply  
   
3 years ago
Just to add, in this case this query is intended as the record source for a report.

I think I mentioned already, but If I simply copy the result of either of the above functions from the debug window, then paste into the criteria field, I get the intended results.
Kevin Robertson  @Reply  
          
3 years ago
The problem is you are using your function as criteria. Create a new calculated field like this:

myField: GetWorkWeekRange([TempVars]![TVeD])

Create Function
Chris Pardy OP  @Reply  
   
3 years ago
Hi Kevin.. not sure I understand... the result of the function IS what I need the search criteria to be.

So in the table there is a date field. this function takes a date that is stored as a variable, creates a date range, and in theory returns any date that is in the range..
Chris Pardy OP  @Reply  
   
3 years ago
Hi Kevin.. OK I think its solved.. I Made a new function that just spits out the a single date that would be the start of the range, and I use that in a calculated field with the input to the function coming from the related record, then for the search criteria I use the same function, but use the date stored in tempvars as the source.. seems to be working.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Developer Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 4/30/2026 2:57:07 PM. PLT: 1s