Solved Criteria Between two Dates Optional and not Mandatory (1 Viewer)

Sarah.M

Member
Local time
Today, 19:17
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

  • Between Date.accdb
    960 KB · Views: 84
Last edited:

Josef P.

Well-known member
Local time
Today, 18:17
Joined
Feb 2, 2023
Messages
826
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:

XPS35

Active member
Local time
Today, 18:17
Joined
Jul 19, 2022
Messages
159
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"))
 

Sarah.M

Member
Local time
Today, 19:17
Joined
Oct 28, 2021
Messages
335
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
 

XPS35

Active member
Local time
Today, 18:17
Joined
Jul 19, 2022
Messages
159
Or ............

Remove the button and make the field(s) empty and click "search".
 

Sarah.M

Member
Local time
Today, 19:17
Joined
Oct 28, 2021
Messages
335
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
 

Sarah.M

Member
Local time
Today, 19:17
Joined
Oct 28, 2021
Messages
335
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

  • Between Date2.accdb
    1.1 MB · Views: 72

Josef P.

Well-known member
Local time
Today, 18:17
Joined
Feb 2, 2023
Messages
826
Same principle as shown in #2:

SQL:
SELECT MeetT.*
FROM MeetT
WHERE MeetT.SecID=[Forms]![MeetF]![CombSecNum] Or  [Forms]![MeetF]![CombSecNum]  is null;
 

Sarah.M

Member
Local time
Today, 19:17
Joined
Oct 28, 2021
Messages
335
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?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 00:17
Joined
May 7, 2009
Messages
19,245
you can also use:

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

Attachments

  • Between Date2.accdb
    1.1 MB · Views: 78

Users who are viewing this thread

Top Bottom