Search dates between two Text Fields (1 Viewer)

StyxLykin

New member
Local time
Tomorrow, 00:08
Joined
Aug 27, 2021
Messages
8
Hi, I would like to search between to text fields. [AbsenceFrom] and [AbsenceTo]

The code is
Option Compare Database

Private Sub Befehl21_Click()

Call Search

End Sub

Sub Search()

Dim strCriteria, task As String

Me.Refresh
If IsNull(Me.DateFrom) Or IsNull(Me.DateTo) Then
MsgBox "Please Enter Date Range", vbInformation, "Date Range Required"
Me.DateFrom.SetFocus
Else
strCriteria = "([AbsenceFrom] >= #" & Me.DateFrom & "# AND [AbsenceTo] <= #" & Me.DateTo & "#)"
task = "select * from Tabelle1 where (" & strCriteria & ") order by [AbsenceFrom]"
DoCmd.ApplyFilter task


End If


End Sub


I always receive an error with the dates. attached is the database with the code

Help would be awesome so I understand what is going wrong. I am new in VBA
 

Attachments

  • Database2.accdb
    896 KB · Views: 393

Gasman

Enthusiastic Amateur
Local time
Today, 23:08
Joined
Sep 21, 2011
Messages
14,260
I always use Between when having two dates.
Also in mm/dd/yyyy format as I am in the UK

Always usefule to say what the error is as well? :(
 

jdraw

Super Moderator
Staff member
Local time
Today, 18:08
Joined
Jan 23, 2006
Messages
15,378
?? worked for me with Dates Apr 1 21 thru May 31 21

NOTE: REvised dates to match my test.
 

Attachments

  • DateSearchResult.PNG
    DateSearchResult.PNG
    24.9 KB · Views: 426
Last edited:

StyxLykin

New member
Local time
Tomorrow, 00:08
Joined
Aug 27, 2021
Messages
8
I am in Germany and the date should be dd/mm/yyyy - The Error is
1630079143546.png
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:08
Joined
Sep 21, 2011
Messages
14,260
Try this.
I use a constant for formatting correctly as well. I also set my filters as below.

In a module I have

Public Const strcJetDate = "\#mm\/dd\/yyyy\#"

Then in your form
Code:
Sub Search()

Sub Search()

    Dim strCriteria, task As String

    Me.Refresh
    If IsNull(Me.DateFrom) Or IsNull(Me.DateTo) Then
        MsgBox "Please Enter Date Range", vbInformation, "Date Range Required"
        Me.DateFrom.SetFocus
    Else
        strCriteria = "[AbsenceFrom] >= " & Format(Me.DateFrom, strcJetDate) & " And [AbsenceTo] <= " & Format(Me.DateTo, strcJetDate)
        Debug.Print strCriteria
        'task = "select * from Tabelle1 where (" & strCriteria & ") order by [AbsenceFrom]"
        'DoCmd.ApplyFilter task
        Me.Filter = strCriteria
        Me.FilterOn = True
    End If
Comment out the Debug.Print when you are happy it is working. Use Debug.Print to see what you actually have, not what you think you have.

HTH
 

StyxLykin

New member
Local time
Tomorrow, 00:08
Joined
Aug 27, 2021
Messages
8
1630080190753.png


Was trying to change the dateformat, but I am just not able to figure it out
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:08
Joined
Sep 21, 2011
Messages
14,260
This is how I would also use BETWEEN
Code:
        strCriteria = "[AbsenceFrom] BETWEEN " & Format(Me.DateFrom, strcJetDate) & " AND " & Format(Me.DateTo, strcJetDate)

My dates are 01/05/2021 and 31/05/2021
Debug.print shows
[AbsenceFrom] BETWEEN #05/01/2021# AND #05/31/2021#


Previous code shows
[AbsenceFrom] >= #05/01/2021# And [AbsenceTo] <= #05/31/2021#
 

StyxLykin

New member
Local time
Tomorrow, 00:08
Joined
Aug 27, 2021
Messages
8
1630080616464.png
1630080623450.png


using the code it comes up with that I am utilizing a variable instead of modual
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:08
Joined
Sep 21, 2011
Messages
14,260
Do not try and be clever. :(
You cannot have modules the same name as variables/constants. :( How is Access supposed to know which one you mean. :(

Use what works.
Create a module, call it ModConst, and put that constant in that.
Then just use it where needed.
 

StyxLykin

New member
Local time
Tomorrow, 00:08
Joined
Aug 27, 2021
Messages
8
The Filter is working. Thank you. That was though
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 23:08
Joined
Sep 21, 2011
Messages
14,260
Here it is
 

Attachments

  • Database2(2).accdb
    896 KB · Views: 470

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:08
Joined
Feb 19, 2002
Messages
43,257
If Access is seeing the dates as a string, then you either need to change the data type on the server to DateTime OR change the format to yyyy-mm-dd. The ONLY date format that works correctly as a string is year, month day. String compares are character by character left to right so o2/11/2020 is > 01/11/2021
 

Users who are viewing this thread

Top Bottom