Filtering by Combobox and Date Range?

Denise2020

Member
Local time
Today, 23:43
Joined
Mar 31, 2020
Messages
82
Greetings!

I have a form with a list of jobs that have dates and agencies attached. On the form, I have a dropdown in order to be able to show all jobs by a single agency. I also have two date range search boxes with a start date and an end date and a button to select all jobs within that date range. Some wonderful helpful people here helped me with this code several months back.

The problem is that it only will filter by date when I first select a single agency. There is no way to show a date range that includes all jobs If I am showing all jobs by all agencies and put in a date range, I get zero results. When I choose an agency and then the date range, I get the expected results. I thought that the If Not IsNull would cover the chance that no selection is made in the Agency (Uppdragsstallare) combobox.

How do I get results for a date range and all agencies? Code is as follows:
 
Hi. I don't see any code. Perhaps you might also consider posting a sample copy of your db with test data.
 
Many apologies but getting a browser error when trying to toggle it as code. I am not sure how to post it.

"Oops! We ran into some problems.
Oops! We ran into some problems. Please try again later. More error details may be in the browser console."



Edit: I don't know if this is allowed but I have put the code in a txt file because no matter what I do, I cannot post it here. So strange. Let me know if this is disallowed and I will delete it.

Something I noticed is that if I make a selection in the Agency (uppdragsstallare) combobox and then erase it by pressing backspace or delete, THEN I get all agencies by a selected date range. So it is works for a "chosen" empty uppdragsstallare, but not if one is not selected in the first place.
 

Attachments

Last edited:
Many apologies but getting a browser error when trying to toggle it as code. I am not sure how to post it.

"Oops! We ran into some problems.
Oops! We ran into some problems. Please try again later. More error details may be in the browser console."
You are not doing anything wrong. That appears to be a recurring error at the moment. I've not had it for a few days, and was hoping it had gone, but it appears not. :(
 
Thank you all.

TheDBguy, I was thinking of trying to upload a generic version of my db but it is in another language (Swedish) and it will take quite a lot of changes to make it usable to anyone here, I think. Or am I not considering something?

Gasman, I tried three different browsers thinking it was an issue with Chrome at first. Good to know it isn't just me, but sorry it is still a problem.

Pat, I actually don't get any errors at all. The code seems fine, no syntax errors, but it isn't bringing the results I hope for.

I mention this every time I post a question here, but everything I know about Access (which is little) is self-taught from Google and YouTube so unfortunately I am always very irritating and have a lot of questions. So I am quite possibly misunderstanding what the code is checking for here. I thought that If Not IsNull() would result in:

1. if there is a selection made in either the date range, agency, or both, it would check and compare those to bring back the filtered results
2. If there is NO selection, i.e. it IsNull, then it would just bring back all results, for both agency and date range.

Instead, it is bringing back zero results. So I can only think that a "no selection" in the combo or date boxes do not equal Null, does no selection = Empty? Is there a way to change or add to this so that it selects all records if nothing else is picked?

I have tried to write in something that tells it that if no selection is made, then Uppdragsstallare = "*" but nothing I try has worked.

Thank you very, very kindly for all of your patience and help. It is greatly appreciated!
 
Without seeing the actual string created by the code we haven't a clue.
Ok, I am unsure what you mean by string created by the code. I opened the immediate window and ran the code but nothing showed up. I am sorry I don't know how to post/supply you with what you are asking for.
 
Pat, Thank you so much for your instructions, I am getting to it first thing this morning. In the below code, the string name would be 1=1? If so, the only thing I get in the immediate window is True. I am reading up further to see if I am again doing something wrong. Thanks again for your patience and help!

As a side note, interestingly, I narrowed down that the brower OOPS error is occurring when I try to use the double quote, quote, double quote combo in bold. Everything else will load fine. Not sure if anyone knows how or why that can be an issue but I thought I would just point out that that is what is causing the error in my case.


Private Sub btnValjDatum_Click()
Dim sWhere As String

sWhere="1=1"
If Not IsNull(txtStartDate) Then sWhere = sWhere & " and [Arendedatum] between #" & txtStartDate & "# and #" & txtEndDate & "#"
If Not IsNull(cboFilterUppdragsstallare) Then sWhere = sWhere & " and [Uppdragsstallare]='" & cboFilterUppdragsstallare & double quote quote double quote

If sWhere = "1=1" Then
Me.FilterOn = False
Else
Me.Filter = sWhere
Me.FilterOn = True
End If
End Sub
 
Try to like that,
Code:
     sWhere = "1=1"
     If Not IsNull(Me!txtStartDate) Then
           sWhere = sWhere & " and int([Arendedatum] BETWEEN " & Format(Me!txtStartDate], "\#" & "mm/dd/yyyy" & "\#") & " AND " & Format(Me![TxtEndDate], "\#" & "mm/dd/yyyy" & "\#")

Edit:
sWhere = ""
 
Try to like that,
Code:
     sWhere = "1=1"
     If Not IsNull(Me!txtStartDate) Then
           sWhere = sWhere & " and int([Arendedatum] BETWEEN " & Format(Me!txtStartDate], "\#" & "mm/dd/yyyy" & "\#") & " AND " & Format(Me![TxtEndDate], "\#" & "mm/dd/yyyy" & "\#")

Edit:
sWhere = ""
I edited the sWhere to "" but am getting a syntax error with this, possibly this? But not sure how to fix it, getting expected end of statement with()

(Me!txtStartDate]

edit: sorted, removed ], testing gives me Syntax Error:

Syntax error (missing operator) in query expression ' and int([Arendedatum] BETWEEN # # AND # # and
[Uppdragsstallare] = "NBS"
 

Attachments

  • Screenshot 2021-03-22 100011.jpg
    Screenshot 2021-03-22 100011.jpg
    69.2 KB · Views: 618
Last edited:
maybe something like this.
(i am unable to post the code?)
 

Attachments

Could anyone kindly explain to me why the If Not IsNull() does not cover the contingency that no selection is made in the dropdown/combobox? It seems like this is the problem and why it gives no results. Does If Not IsNull() not mean Else ALL Records? If not, then what would refer to All Records? Sorry if my question is confusing.
 
oh, i missed the two #.
 

Attachments

oh, i missed the two #.
Thank you once again. Unfortunately I get zero results when choosing dates but no Uppdragsstallare.

I have been messing with trying to add a Union Select in the query behind the combobox to add an <All> choice but while the query itself shows <All> in the list, selecting <All> brings zero records rather than all records as desired. Is this a track to follow or should I just skip this idea? Thanks again!
 
do you have Date picker on your 2 date textbox? you should to avoid confusion (when using different Date Region setting).

EDIT:
you can also make those (start/end date) textbox as combobox.
so you can limit only dates available on your table, eg:

combo0 (combobox)
Row Source Type: Table/Query
Row Source: Select [date field] from [your table] group by [date field]
 
do you have Date picker on your 2 date textbox? you should to avoid confusion (when using different Date Region setting).

EDIT:
you can also make those (start/end date) textbox as combobox.
so you can limit only dates available on your table, eg:

combo0 (combobox)
Row Source Type: Table/Query
Row Source: Select [date field] from [your table] group by [date field]
I do have a date picker on the two date textboxes, I think for our use, it would work much better than a combobox.
 
I edited the sWhere to "" but am getting a syntax error with this, possibly this? But not sure how to fix it, getting expected end of statement with()

(Me!txtStartDate]

edit: sorted, removed ], testing gives me Syntax Error:

Syntax error (missing operator) in query expression ' and int([Arendedatum] BETWEEN # # AND # # and
[Uppdragsstallare] = "NBS"

Mr. @arnelgp sir give you nice code. I thing you follow his code.
And you can try this :
Note : If you have List box then used it.
Code:
Dim sWhere As String
     sWhere = " "
     If Not IsNull(Me![cboFilterUppdragsstallare]) Then sWhere = sWhere & " AND [Uppdragsstallare]=" & Me![cboFilterUppdragsstallare ]

     If Not IsNull(Me!txtStartDate) Then
           sWhere = sWhere & " and int([Arendedatum] BETWEEN " & Format(Me![txtStartDate], "\#" & "mm/dd/yyyy" & "\#") & " AND " & Format(Me![TxtEndDate], "\#" & "mm/dd/yyyy" & "\#")
        End If
    End If

    If sWhere <> "" Then
        sWhere = Right(sWhere, Len(sWhere) - 5)
    End If
    Me![YourSubform/ListBox].RowSource = "SELECT * FROM QueryName WHERE " & sWhere
    Me![YourSubform/ListBox].Requery

Another ways change last 6 lines .

Code:
If sWhere <> "" Then
        sWhere = Right(sWhere, Len(sWhere) - 5)
        Me.Filter = sWhere
        Me.FilterOn = True
     
    Else
        Me.FilterOn = False
    End If

Edit :
DELETE : "End If" Because double
 
Last edited:
Why I can't see "PREVIEW content page". Its always error.
It is the same for me.


I appreciate SO very much all your suggestions but unfortunately nothing is working so far.

@arnelgp Maybe if I go about it another way, really all I need is to be able to select ALL records in the dropdown. The UNION select that I have seen you post about in other threads seems promising but I cannot make it work. Am I missing part of the equation?

I create the query and add the UNION line in the SQL, and when I run the query, I get <All> in the list. But when I go to my form, selecting <ALL> returns no records. I feel like I am once again missing something obvious.
 
Last edited:
Unless you follow the instructions and post the string created by the code we're just guessing.
I am sorry because I am very much a beginner here but as I wrote earlier today, I followed your instructions and there was NO string to post here. It only said the word True.

It is quite possible that I did something wrong, but I have researched and tried and nothing else gave me any string at all. The only thing I did find was from another thread where someone said "you can't step into a procedure that takes arguments. You can set a breakpoint in the code and step from there." I tried setting breakpoints at various points but nothing has worked. The immediate window is just blank. Sometimes a macro window opens with no info and I can only select Close, and sometimes I get a Windows "ding" sound. Other than that, no string. I watched a video of how it is supposed to look when it is working and it definitely did not do that. Again, apologies, but I don't know what else to do with that.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom