How search for records based on a specified date range

I have several unbound field on a "Search Form".  I also have a command button in the footer of the form.  The onclick event of the command button currently has this code:

Dim s As String

    If Nz(Me.txtRecordID, "") <> "" Then
        s = s & " [RecordID] Like '*" & Me.txtRecordID & "*'"
    End If

    If Nz(Me.txtPRn, "") <> "" Then
        If Len(s) > 0 Then
            s = s & " AND "
        End If
        s = s & " [PRn] Like '*" & Me.txtPRn & "*'"
    End If

    If Nz(Me.txtSKUn, "") <> "" Then
        If Len(s) > 0 Then
            s = s & " AND "
        End If
        s = s & " [SKUn] Like '*" & Me.txtSKUn & "*'"
    End If

    If Nz(Me.txtRevisionDate, "") <> "" Then
        If Len(s) > 0 Then
            s = s & " AND "
        End If
        s = s & " [RevisionDate] Like '*" & Me.txtRevisionDate & "*'"
    End If

    If Len(s) > 0 Then
        s = "SELECT * FROM [tblRecords] WHERE " & s & " Order by [RecordID]"
    Else
        s = "SELECT * FROM [tblRecords] " & " Order by [RecordID]"
    End If

    DoCmd.OpenForm "frmRecordsHeader", acNormal, , , acFormReadOnly, acWindowNormal
    
    Forms!frmRecordsHeader.Form!subfrmRecordsDS.Form.RecordSource = s

Open in new window


But on the form I will have two date fields.  txtStartDate and txtEndDate.  So instead of this part of the code:

    If Nz(Me.txtRevisionDate, "") <> "" Then
        If Len(s) > 0 Then
            s = s & " AND "
        End If
        s = s & " [RevisionDate] Like '*" & Me.txtRevisionDate & "*'"
    End If

Open in new window


I want to search where RevisionDate is between txtStartDate and txtEndDate.  How would I handle this?  What would the code:

    If Nz(Me.txtRevisionDate, "") <> "" Then
        If Len(s) > 0 Then
            s = s & " AND "
        End If
        s = s & " [RevisionDate] Like '*" & Me.txtRevisionDate & "*'"
    End If

Open in new window


Be changed to?
SteveL13Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

SteveL13Author Commented:
I've tried this but it doesn't work:

    If Nz(Me.txtStartDate, "") <> "" And Nz(Me.txtEndDate, "") <> "" Then
        If Len(s) > 0 Then
            s = s & " AND "
        End If
        s = s & " [RevisionDate] BETWEEN # & [txtStartDate] & # AND # & [txtEndDate] & #"
        
    End If

Open in new window

0
Dale FyeOwner, Developing Solutions LLCCommented:
    If Nz(Me.txtStartDate, "") <> "" Then
        If Len(s) > 0 Then s = s & " AND "
        s = s & "([RevisionDate] >=  #" & Me.txtStartDate & "#)"
    End If

'I use the following syntax to make certain that if the RevisionDate field contains a time
'component, then all of the records pertaining to that date are included in the results.
    If Nz(Me.txtEndDate, "") <> "" Then
        If Len(s) > 0 Then s = s & " AND "
        s = s & "([RevisionDate] <  #" & DateAdd("d", 1, Me.txtEndDate) & "#)"
    End If

Open in new window

0
SteveL13Author Commented:
Dale,  Your suggested code doesn't seem to be working.  I'm getting no records when I click the command button
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Gustav BrockCIOCommented:
You just miss some quotes and - to play safe - to apply a forced format of the date expressions:

s = s & " [RevisionDate] BETWEEN #" & Format([txtStartDate], "yyyy\/mm\/dd") & "# AND #" & Format([txtEndDate], "yyyy\/mm\/dd") & "#"

Open in new window

/gustav
0
SteveL13Author Commented:
Still getting no re3cords with:

    If Nz(Me.txtStartDate, "") <> "" Then
        If Len(s) > 0 Then s = s & " AND "
        s = s & "([RevisionDate] >=  #" & Me.txtStartDate & "#)"
    End If

    If Nz(Me.txtEndDate, "") <> "" Then
        If Len(s) > 0 Then s = s & " AND "
        s = s & " [RevisionDate] BETWEEN #" & Format([txtStartDate], "yyyy\/mm\/dd") & "# AND #" & Format([txtEndDate], "yyyy\/mm\/dd") & "#"
    End If

Open in new window

0
SteveL13Author Commented:
Just for kicks I put this after the last line:

MsgBox s

And got:

msgbox
0
SteveL13Author Commented:
When I entered 9/6/2005 and 4/11/2007
0
Dale FyeOwner, Developing Solutions LLCCommented:
you need to remove the first set of code

    If Nz(Me.txtRevisionDate, "") <> "" Then
        If Len(s) > 0 Then
            s = s & " AND "
        End If
        s = s & " [RevisionDate] Like '*" & Me.txtRevisionDate & "*'"
    End If

get rid of that and just use the code for txtStartDate and txtEndDate
0
SteveL13Author Commented:
Still no records with:  (Note I changed the formatting because I thought that might be the issue.  It didn't work either way)

    If Nz(Me.txtEndDate, "") <> "" Then
        If Len(s) > 0 Then s = s & " AND "
'        s = s & " [RevisionDate] BETWEEN #" & Format([txtStartDate], "yyyy\/mm\/dd") & "# AND #" & Format([txtEndDate], "yyyy\/mm\/dd") & "#"
        
        
        s = s & " [RevisionDate] BETWEEN #" & Format([txtStartDate], "mm\/dd\/yyyy") & "# AND #" & Format([txtEndDate], "mm\/dd\/yyyy") & "#"
        
    End If

Open in new window

0
Gustav BrockCIOCommented:
Your message clearly shows your error.
I assumed your second code, thus:

If Nz(Me.txtStartDate, "") <> "" And Nz(Me.txtEndDate, "") <> "" Then
    If Len(s) > 0 Then
        s = s & " AND "
    End If
    s = s & " [RevisionDate] BETWEEN #" & Format([txtStartDate], "yyyy\/mm\/dd") & "# AND #" & Format([txtEndDate], "yyyy\/mm\/dd") & "#"
End If

Open in new window

/gustav
0
SteveL13Author Commented:
I don't know what I'm doing wrong.  Please see attached test file.  Enter 9/6/2005 and 4/11/2007 in the bottom two fields.  Then click [Search].  Nothing shows up in the results form.  Two records should show up.
Date-Range-Test.accdb
0
Gustav BrockCIOCommented:
The query (SQL) is correct, but you do some very weird things with this setup of a parent and a child form - a lot of DLookups etc.
So this is where to look for the error(s).
You may have to redesign it from scratch bit for bit.

/gustav
0

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
SteveL13Author Commented:
Redesigned the forms and used your latest code.  All is well.  Thank you.
0
Gustav BrockCIOCommented:
You are welcome!

/gustav
0
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.