Solved Criteria Between two Dates Optional and not Mandatory

Sarah.M

Member
Local time
Today, 03:33
Joined
Oct 28, 2021
Messages
335
Hi, Plz note, My VBA is blocked by IT for security reason (Cybersecurity).
I can use only Expression builder, Macros, Queries, Tables, Forms and Reports only.
------------------------------------------------
I want to make query creteria between two dates based on txtboxes in form, but the criteria is not working properly if I add date in [txtFrom]
I did this but it does not filter the from :cautious: 🙇‍♀️🙇‍♀️
Like "*" & [Forms]![MeetF]![txtFrom] & "*" And Like "*" & [Forms]![MeetF]![txtTo] & "*"

Between Like "*" & [Forms]![MeetF]![txtFrom] & "*" And Like "*" & [Forms]![MeetF]![txtTo] & "*"

Between "*" & [Forms]![MeetF]![txtFrom] & "*" And "*" & [Forms]![MeetF]![txtTo] & "*"

I did this it works, but entring data is mandatory 🙇‍♀️🙇‍♀️🙇‍♀️
Between [Forms]![MeetF]![txtFrom] And [Forms]![MeetF]![txtTo]

I want all data to be visible, but search textbox by date is optional and not mandatory
Please🙏 help me 🙏🙇‍♀️

Sampel Attached
 

Attachments

Last edited:
A date is not a string. ;)

Maybe that's what you're looking for:
SQL:
...
WHERE
    ( MeetDate >= [Forms]![MeetF]![txtFrom]  OR [Forms]![MeetF]![txtFrom] IS NULL )
    and
    ( MeetDate <= [Forms]![MeetF]![txtTo] OR [Forms]![MeetF]![txtTo] IS NULL )

or (if MeetDate is not null):
SQL:
...
WHERE
    MeetDate >= Nz([Forms]![MeetF]![txtFrom], #1900-01-01#)
    and
    MeetDate <= Nz([Forms]![MeetF]![txtTo], #2999-12-31# )
This variant is better for index.
 
Last edited:
Or ............
In the criteria of the function you can put:
SQL:
Between Nz([Forms]![MeetF]![txtFrom],DMin("Meetdate","MeetT")) And Nz([Forms]![MeetF]![txtTo],DMax("Meetdate","MeetT"))
 
Or ............
In the criteria of the function you can put:
SQL:
Between Nz([Forms]![MeetF]![txtFrom],DMin("Meetdate","MeetT")) And Nz([Forms]![MeetF]![txtTo],DMax("Meetdate","MeetT"))
Smart!

My way to remove filter
1676238735268.png


But I encountered a problem when I remove the filter I get an error message

1676238577914.png
1676238647045.png
 
Or ............

Remove the button and make the field(s) empty and click "search".
 
A date is not a string. ;)

Maybe that's what you're looking for:
SQL:
...
WHERE
    ( MeetDate >= [Forms]![MeetF]![txtFrom]  OR [Forms]![MeetF]![txtFrom] IS NULL )
    and
    ( MeetDate <= [Forms]![MeetF]![txtTo] OR [Forms]![MeetF]![txtTo] IS NULL )

or (if MeetDate is not null):
SQL:
...
WHERE
    MeetDate >= Nz([Forms]![MeetF]![txtFrom], #1900-01-01#)
    and
    MeetDate <= Nz([Forms]![MeetF]![txtTo], #2999-12-31# )
This variant is better for index.
WoW! both ways are works
 
Hi, I have question realted to this threads

My question is What about if I have a query has [SecID] and the criteria refer to 1 combo box [Forms]![MeetF]![CombSecNum] how can I let the query to show all the Recored if the [Forms]![MeetF]![CombSecNum] is Null or ""?

I do the following way but, if I pick for example Sec1 it is works, but if i[Forms]![MeetF]![CombSecNum] is Null all recored gone :cautious:🙇‍♀️🙇‍♀️, I want if the ComboBox is null or "" show all recored 🙏🙏
Nz([Forms]![MeetF]![CombSecNum],"*")
Nz([Forms]![MeetF]![CombSecNum],Is Null)
Nz([Forms]![MeetF]![CombSecNum],IsNull(Forms]![MeetF]![CombSecNum]))

Help me Plz 🙆‍♀️🧏‍♀️🙇‍♀️o_O
Sample Attached
 

Attachments

Same principle as shown in #2:

SQL:
SELECT MeetT.*
FROM MeetT
WHERE MeetT.SecID=[Forms]![MeetF]![CombSecNum] Or  [Forms]![MeetF]![CombSecNum]  is null;
 
Same principle as shown in #2:

SQL:
SELECT MeetT.*
FROM MeetT
WHERE MeetT.SecID=[Forms]![MeetF]![CombSecNum] Or  [Forms]![MeetF]![CombSecNum]  is null;
Awesome!
Do you have another method with NZ easy to write?
 
you can also use:

Where IIf(Trim([Forms]![MeetF]![CombSecNum] & "")="",[SecID],[Forms]![MeetF]![CombSecNum])
 

Attachments

Users who are viewing this thread

Back
Top Bottom