Results 1 to 13 of 13
  1. #1
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383

    Filter Using ComboBox For Date Criteria

    I have a ComboBox named FilterByDate and it is Unbound.
    RowSource is set to "Today";"This Week";"Last Week";"This Month";"Last Month";"This Year";"Last Year";"All"
    Event Procedure is set to be On Change,, is this correct? Or else ???


    I am starting my vb code like this;



    Me.Filter = "OrderDate" = & Me.FilterByDate
    DoCmd.RunCommand acCmdApplyFilterSort

    I now want to add my date criteria and then move on the next else statement or filter!

    My code for the First Date Criteria is "Today"
    = (Year([OrderDate[) = Year(Date()) And Month([OrderDate])=Month(Date()) And Day([OrderDate]) = Day(Date())

    So, my question is, how do I put this together so that it runs when I choose my option form the list?
    I then want to be able to move on to the next criteria if chosen which is "This Week" and so one!

    Thanks in Advance,

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    That seems unnecessarily complex. Why not just

    OrderDate = Date()

    By the way, I'd use the after update event. The change event fires with every keystroke. If actually a date field, it would be:

    Me.Filter = "OrderDate = #" & Me.FilterByDate & "#"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    Are you saying this code will run whatever selection I enter in the combobox? Today,This Week, etc...???

    I need to be able to filter the selection by the List as noted above.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    This:

    OrderDate = Date()

    was an alternative to this:

    (Year([OrderDate[) = Year(Date()) And Month([OrderDate])=Month(Date()) And Day([OrderDate]) = Day(Date())

    This:

    Me.Filter = "OrderDate = #" & Me.FilterByDate & "#"

    was a correction of this:

    Me.Filter = "OrderDate" = & Me.FilterByDate

    because date values require the # delimiters.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    Me.Filter = "[OrderDate] Like '*" & [OrderDate] & "*'"
    Me.FilterOn = True


    I tried this, but it is not working.My form name is OrderDetails and the combobox is named Filter. The key date on the form is OrderDate which is what I want t filter by. It is on the after update event of the form. I was going to use the Date Picker thing, but maybe not. What is a Date error occurs? Do I not need error code for this as well? Criteria is in place to make sure you have a CustomerID and so on.

    Error message is , you cant save this record right now.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Is OrderDate a text field or date/time? You're treating it like text.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    It is a Date Field.

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Paul gave you the correct syntax for assigning a date value to a variable of date/time data type in post #4

    This:

    Me.Filter = "OrderDate = #" & Me.FilterByDate & "#"

    was a correction of this:

    Me.Filter = "OrderDate" = & Me.FilterByDate

    because date values require the # delimiters.

  9. #9
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    Explain to me how that is going to show me Today,This Week,Last Week,This Month,Last Month, etc... ???

  10. #10
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    What exactly do you think will happen when you assign
    OrderDate = Last Month??? or Next Month???

    Do you not think you would have to have some construct to assign an actual Date or Between Date1 and Date2?

    Last Month may be equivalent to
    OrderDate Between FirstOfLastMonth and EndOfLastMonth

    But I think you would agree if you are building a filter , or an SQL where clause, you have to have the proper units and syntax.

    When we see a Datefield (Orderdate), you have to be dealing in Dates , not a text string "Last Month".
    I guess you should tell us what is in your "list".

    Like is not going to help you either.
    You may have to use some combinations of DateAdd, DateDiff, Between AND....

  11. #11
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    I am using this code now, probably not the best way, but I don't know of any other way. I set my combo to unbound,value list to "Today";"This Week", etc...
    Then on the After Update Event I run this code. Not tested yet.

    If Forms![OrderDetails]![OrderDate]![Filter] = "Today" Then
    OrderDate = (Year([OrderDate]) = Year(Date) And Month([OrderDate]) = Month(Date) And Day([OrderDate]) = Day(Date))
    End If

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    There's nothing wrong with your concept, though I'd use Select/Case given the number of options. I suppose you could create a second column of the combo with the filter string in it.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    You may also want to review DateSerial, DateAdd , DateValue functions.

    See Date Functions at http://www.techonthenet.com/access/functions/index.php

    This code
    Code:
    If Forms![OrderDetails]![OrderDate]![Filter] = "Today" Then
    OrderDate = (Year([OrderDate]) = Year(Date) And Month([OrderDate]) = Month(Date) And Day([OrderDate]) = Day(Date))
    End If
    Could be
    Code:
    If Forms![OrderDetails]![OrderDate]![Filter] = "Today" Then
    OrderDate = Date
    End If
    As I said, you'll have to get the values for NextMonth etc into a Date datatype or some construct that can be used with Dates.

    Good luck with your project

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 09-06-2011, 01:47 PM
  2. Replies: 29
    Last Post: 08-16-2011, 05:52 PM
  3. Filter by comboBox value
    By blueraincoat in forum Forms
    Replies: 6
    Last Post: 03-16-2011, 09:49 AM
  4. Replies: 0
    Last Post: 08-24-2010, 06:38 PM
  5. Replies: 0
    Last Post: 12-16-2009, 01:14 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums