ComboBox for finding a record

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyWaterloo
    New Member
    • Dec 2007
    • 135

    ComboBox for finding a record

    Code:
    '''''''''''''''''''''''''''''''''''''''''''
    ' The AfterUpdate event procedure used in '
    ' the Find Customer combo box search.     '
    '''''''''''''''''''''''''''''''''''''''''''
    
        'Moves to Customer Name text box and
        'finds the record of whatever name
        'is selected in the combo box
        DoCmd.ShowAllRecords
        Me.OrderDate.SetFocus
        DoCmd.FindRecord Me.Combo74
        
        'Set value of combo box equal to an empty string
        Me!Combo74.Value = ""
    I am using this code that I got form (probably on here) somewhere to make my ComboBox look up records by date. The problem comes in when I have multiple records for the same date. The combobox shows the date as many times as a record was entered on that date. Example: shows the date 5/20/2009 five diff times. When I select one of the 5/20/2009 dates to bring up the respective record...it goes to the first record it can find with that matching date. Naturally, this is what the code is telling it to do after all. So what can I do to create a Combo that can be used to lookup records by date? How can I make the combo just show the date 5/20/2009 once, and then when selected filter the records so only those records with the matching date are shown? But then how would I unfilter the records if I want to back to viewing all the Records?
  • ChipR
    Recognized Expert Top Contributor
    • Jul 2008
    • 1289

    #2
    Code:
    Combo74_AfterUpdate()
    If Combo74.ListIndex >= 0 Then
      Me.Filter = "DateField = #" & Combo74 & "#"
      Me.FilterOn = True
    End If
    
    ClearFilterButton_Click()
      Me.FilterOn = False

    Comment

    • MyWaterloo
      New Member
      • Dec 2007
      • 135

      #3
      Originally posted by ChipR
      Code:
      Combo74_AfterUpdate()
      If Combo74.ListIndex >= 0 Then
        Me.Filter = "DateField = #" & Combo74 & "#"
        Me.FilterOn = True
      End If
      
      ClearFilterButton_Click()
        Me.FilterOn = False
      Thank you very much.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32653

        #4
        Check out Literal DateTimes and Their Delimiters (#) for an explanation of why using the default display of a date is not always a good idea when using within SQL.

        Comment

        • MyWaterloo
          New Member
          • Dec 2007
          • 135

          #5
          This works great to filter my records by the date selected in the combo box. My combo box still shows multiple of the same date. I know each date represents a record created on that date, but is there any way to just show a duplicated date only once? I don't need every record represented for that date since I am going to select the date and filter by it.

          Comment

          • ChipR
            Recognized Expert Top Contributor
            • Jul 2008
            • 1289

            #6
            Use the DISTINCT keyword in the SELECT statement for your combo box source.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32653

              #7
              You could also consider using a GROUP BY clause.

              For your specified requirement though, the DISTINCT predicate would be preferable.

              Comment

              Working...