Results 1 to 7 of 7
  1. #1
    aliaslamy2k is offline Advanced Beginner
    Windows 7 32bit Access 2013 32bit
    Join Date
    Apr 2020
    Posts
    31

    Filter by Combo box in form

    Hi,


    I have created a form where i want to add combo box to fliter.When I tried to enter below code i always get error.
    I dont understand what's wrong with below code. Please help me correct this code.




    Private Sub CmbSearch_AfterUpdate()
    Dim SearchOwnerList As String
    SearchOwnerList = "Select * from Fleets DB where([Owner] = " & Me.CmbSearch & ")"
    Me.FleetsDBsearch1.Form.RecordSource = CmbSearch
    Me.FleetsDBsearch1.Form.Requery
    End Sub

    Regards,
    AB

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    What error message do you receive? If Owner is a text datatype you'll need to account for this in the SQL.
    Code:
    Private Sub CmbSearch_AfterUpdate()
    Dim SearchOwnerList As String
    SearchOwnerList = "Select * from Fleets DB where([Owner] = '" & Me.CmbSearch & "')"
    Me.FleetsDBsearch1.Form.RecordSource = SearchOwnerList
    Me.FleetsDBsearch1.Form.Requery
    End Sub
    Review the Similar Threads at the bottom of this page.

  3. #3
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,614
    Try:
    Code:
    Private Sub CmbSearch_AfterUpdate()
    Dim SearchOwnerList As String
       SearchOwnerList = "Select * from Fleets DB where([Owner] = " & Me.CmbSearch & ")"
       Me.FleetsDBsearch1.Form.RecordSource = SearchOwnerList
       Me.FleetsDBsearch1.Form.Requery
    End Sub
    Or even:
    Code:
    Private Sub CmbSearch_AfterUpdate()
       Me.FleetsDBsearch1.Form.RecordSource = "Select * from Fleets DB where([Owner] = " & Me.CmbSearch & ")"
       Me.FleetsDBsearch1.Form.Requery
    End Sub
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  4. #4
    aliaslamy2k is offline Advanced Beginner
    Windows 7 32bit Access 2013 32bit
    Join Date
    Apr 2020
    Posts
    31
    Click image for larger version. 

Name:	Capture.JPG 
Views:	15 
Size:	21.6 KB 
ID:	41679

    I am getting same error every time i run the code. I have struggling with this from past 3 days

  5. #5
    aliaslamy2k is offline Advanced Beginner
    Windows 7 32bit Access 2013 32bit
    Join Date
    Apr 2020
    Posts
    31
    Hi Orange,

    Yes it started working now Thank you so much
    But its not filtering two names from combo box, rest all is fine.

    I have 14 company names in the combo box and i am able to filter 12 out of 14. I am not sure what must be the reason for not filtering those two names. I mean when i select this names nothing happens

  6. #6
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,614
    Show us the Row Source property of the combo box.

    What is the name of the table that has the field called "[Owner]" ?

    What Data Type is the "[Owner]" field?
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  7. #7
    aliaslamy2k is offline Advanced Beginner
    Windows 7 32bit Access 2013 32bit
    Join Date
    Apr 2020
    Posts
    31
    Click image for larger version. 

Name:	Field Type.JPG 
Views:	14 
Size:	67.0 KB 
ID:	41680Click image for larger version. 

Name:	Row Source.JPG 
Views:	15 
Size:	148.6 KB 
ID:	41681


    Row Source of Combo Box property:
    SELECT [AllOwnerCompanies Query].[Owner] FROM [AllOwnerCompanies Query] ORDER BY [Owner];

    Table Name: Fleets DB
    Field Name: Owner (Data Type: Short text)

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

Similar Threads

  1. filter a form using combo box
    By prasad halande in forum Forms
    Replies: 4
    Last Post: 03-19-2020, 05:52 PM
  2. Replies: 4
    Last Post: 02-24-2018, 11:48 PM
  3. Replies: 11
    Last Post: 09-03-2015, 11:12 AM
  4. Filter A Form With Combo Box
    By JeffG3209 in forum Forms
    Replies: 15
    Last Post: 05-27-2011, 09:04 PM
  5. Filter my form from combo box
    By Angate in forum Forms
    Replies: 3
    Last Post: 04-24-2010, 01:57 PM

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