Results 1 to 12 of 12
  1. #1
    Eddy Sincere is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2014
    Location
    Carson City, NV
    Posts
    114

    Filter by ComboBox


    I've created a very basic filter using the code form Allen Browne's website. Every field except the combobox field works great. When I enter criteria into the combobox it's like the filter doesn't even recognize the VBA. (Attachment 1).

    Attachment 2 has the code I'm using. Can someone tell me what I'm doing wrong? I'll post the DB if you need. Thanks!

    Attachment 1
    Click image for larger version. 

Name:	CDQ01.PNG 
Views:	29 
Size:	46.0 KB 
ID:	17950

    Attachment 2
    Click image for larger version. 

Name:	CDQ02.PNG 
Views:	29 
Size:	30.4 KB 
ID:	17951

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    The combobox shows a text value but your code is looking for Boolean values. What is the combobox RowSource?

    Your filter will fail if the last parameter is not included in the concatenation. That's because if any of the other parameters are used the filter will end with " And ".

    Look at Allen's example again. It has the string always end with " And " and then chops it off.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Since you are familiar with Allen's site, look at
    Problem names and reserved words in Access
    http://www.allenbrowne.com/AppIssueBadWord.html

    "Type" is a reserved word in Access.

    "Type" as a caption of a label is OK.
    "Type" as an object name... not so much...(aka bad)

  4. #4
    Eddy Sincere is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2014
    Location
    Carson City, NV
    Posts
    114
    Steve -

    Thanks again for the naming correction. I just went through and renamed everything (fields, tables, queries, etc) to avoid this problem in the future. I believe you gave me this advice on an earlier post, it's just taken me a while to get around to fixing it. :-)

    It didn't really fix my problem but I'll keep trying. I'm guessing that filtering via the combo box requires some specific VBA because even when I use a standard text search (opposed to a Boolean as mentioned by June7) it still doesn't return any values.

    On another note, I'm incorporating most of your other recommendations from our previous interaction and the relationships, etc. seem to be working better. Thanks again for the honest criticism, they're great opportunities for me to learn.

  5. #5
    Eddy Sincere is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2014
    Location
    Carson City, NV
    Posts
    114
    The combobox shows a text value but your code is looking for Boolean values.
    True, the string is looking for Boolean values. I just changed the string to resemble the text strings and, unfortunately, it didn't work. Whether I eliminate the "like" statement or leave it in the filter simply returns no records.


    What is the combobox RowSource?
    The RowSource is another table named, DcmntTp. It contains all the types of documents, i.e. Forms, SOP, etc.


    Your filter will fail if the last parameter is not included in the concatenation. That's because if any of the other parameters are used the filter will end with " And ".

    Look at Allen's example again. It has the string always end with " And " and then chops it off.
    Yes, I understand that which is why I left the And " off the last string only. Am I incorrect? Should it be added to the last string as well? It seems that whenever I leave it on I get an error message.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Look at Allen's example again. Include the " And " even with the last parameter. Then code chops off the unnecessary " And ". http://www.allenbrowne.com/ser-62code.html

    If the combobox RowSource is just a reference to table, all fields will be retrieved. What fields are in the table? Is there an ID field? Is it the first field? Is the combobox BoundColumn set to 1?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Eddy Sincere is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2014
    Location
    Carson City, NV
    Posts
    114
    ook at Allen's example again. Include the " And " even with the last parameter. Then code chops off the unnecessary " And ".
    Whenever I do this I get an error code (attachment 3) and when I start the debugger it stops at the str.where (attachment 4). However, when I drop the And off the last string it works just fine and I get no error messages.

    Attachment 3
    Click image for larger version. 

Name:	CDQ04.JPG 
Views:	21 
Size:	26.0 KB 
ID:	17969

    Attachment 4
    Click image for larger version. 

Name:	CDQ03.JPG 
Views:	21 
Size:	60.9 KB 
ID:	17968

    What fields are in the table?
    2 Fields. A record number (RcdNmbrDT) and text field (Tp). I made sure my naming conventions don't use the reserved words.

    [QUOTE]Is there an ID field? Is it the first field? Is the combobox BoundColumn set to 1?[QUOTE]
    Yes, yes and yes.

    The code in Attachment 3 uses only "Like" statements to search. I stopped trying to get the combo box to work because it kept messing up the searches.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    You are not using the code that chops off the " And ". It is clearly present in Allen's example. Why are you not including that code?

    Then the value of combobox is the ID, not the descriptive text. Why would you use True and False parameters?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    Eddy Sincere is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2014
    Location
    Carson City, NV
    Posts
    114
    LOL! I'm so sorry. You are exactly right. I was confused and didn't understand that the piece of code was what "chopped" the And. I inserted it and it's running fine.

    I'm using True/False parameters because it was on Allen's code . He used a combobox for one of the examples so I just copied that not realizing it was +/-. After reading your comment I see the error. However, even after correcting the error and using a text string I'm still getting errors. I inserted the following string (where txtTp = Combobox and Tp = Field ID):

    If Not IsNull(Me.txtTp) Then
    strWhere = strWhere & "([Tp] = """ & Me.txtTp & """) AND "
    End If

    But when it runs I get error 3075. I thought that a combobox was always recognized as text. I'm obviously missing something so I'm going to let this percolate over the weekend and try again next week. Thanks for the help.

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    It's the field type that drives the criteria syntax.

    What data type is [Tp]? Do you have Lookup on this field in table? I NEVER set lookup in table just because of the confusion it causes.

    As I already said, if the combobox BoundColumn is the ID field then that is the value of the combobox, not the descriptive text.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    Eddy Sincere is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2014
    Location
    Carson City, NV
    Posts
    114
    Alright, I got it figured out with your help. You correct about the bound column being the source of the issue. It needed to be "2" instead of "1" because the primary key is a number. I had the combobox displaying column "2" but the bound column was looking at column "1". Once I corrected that and made a small correction in the VBA string it works fine.

    My VBA code was:

    If Not IsNull(Me.txtTp) Then
    strWhere = strWhere & "([Tp] Like ""*" & Me.txtTp & "*"") And "
    End If

    It is now:
    If Not IsNull(Me.cboTp) Then
    strWhere = strWhere & "([Tp] Like '" & Me.cboTp & "') And "
    End If

    The primary differences being: a) Renamed the field to cbTp; b) Removed Wildcard asterix (*); c) Closed the statement in a quote mark (').

    Thanks for the help! It took me a while to understand what you meant so thanks for the patience. My filter form looks tons better and actually does what i want it to. I'm not sure how to mark as closed but this thread should be.

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Glad it works.

    Mark solved with Thread Tools drop down above first post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. filter combobox
    By azhar2006 in forum Sample Databases
    Replies: 3
    Last Post: 07-21-2014, 02:05 PM
  2. Replies: 1
    Last Post: 09-06-2011, 01:47 PM
  3. Replies: 29
    Last Post: 08-16-2011, 05:52 PM
  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