Results 1 to 8 of 8
  1. #1
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    237

    Filter a subfForm based on a value typed on the main form

    Hi to all,


    I have a main form with a subform with a Many-To-Many relationship.
    I have a text Box on the main form. My subform has a combo box on it with Datasheet as Default View.
    The user has to enter a text in the text box (txtSearch) on the main form so that the sub form will show only records for the search criteria only.
    What code should I use in the AfterUpdate Event of the txtSearch text box to filter the subform.
    Note: The data entered in the txtSearh might be part of the field.

    Khalil

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    when user alters the search value:

    Code:
    sub txtSearch_Afterupdate()
    
    if isnull(me.txtSearch) then
       me.subform.form.filterOn =false
    else
       me.subform.form.filter = "[field]='" & me.txtSearch & "'"
       me.subform.form.filterOn =true
    endif
    end sub

  3. #3
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    237
    Hi,
    Thank you for the reply,
    I am having a runtime error 13, Type mismatch.
    In addition, the text box (txtSearch) might be part of a record, so I should use LIKE .

    The sub Form Row Source is the link table with two foreign key and a primary key.
    The only field on the sub form is a combo box with a foreign key as a control source and a record source to select the required field which is column(1).

    Khalil

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Strings need to be surrounded by single quotes ' unless it contains a single quote, then triple double quotes works, I think?


    Date literals with # and in mm/dd/yyyy or yyyy-mm-dd format


    Numbers do not need anything


    Also for anything other than one item of criteria, I tend to put the the criteria into a string variable and then debug.print it, until correct then also use that in the code.


    Added benefit is, if you still cannot see the error, you can copy and paste back here for someone else to spot it.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    I think you'll need to give us more info, at least a screen shot of your form\subform with the combo showing some values and the subform's recordsource. Are the forms linked in anyway via the master\child fields? Have you tried to restrict the subforms record source using the txtSearch and in the after update to simply requery the subform?

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  6. #6
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    237
    Hi,
    Sub Form Record Source is a table with a primary key and two foreign key fields: fkKeyWords and fkBookId

    The combo box on the subform has fkKeyWords as the Control Source and (SELECT tblKeyWords.pkKeyWords, tblKeyWords.KeyWords) as a Row Source.

    I was able to make it work with the following:
    Code:
    Me!cntKeywordsSubForm.Form.Filter = "[fkKeyWords]=" & Me.txtSearch
    Which is not exactly what I want.

    Following is a screen shot of the SubForm.
    Click image for larger version. 

Name:	Capture1.PNG 
Views:	15 
Size:	9.0 KB 
ID:	49236

    The user needs to enter the keyword and not the corresponding number of fkKeyWords.
    In the above screen shot if the user typed Jordan in the txtSearch on the main form, the subform should show 2 records only (first and forth).
    Maybe I should requery the subform based on the value in the txtSearch!!!

    Thanks

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Add a calculated field to the subform's record source SearchReturn:Instr([Keywords],Forms!frmYourMainForm.txtSearch)>0 and put in its criteria row True.
    Now in the AfterUpdate event just leave Me.cntKeywordsSubForm.Form.Requery.
    You might want to tweak the record source to return all records if the search textbox is empty.
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Could use a combobox for user input if it is not set for LimitToList and if you don"t include the fkKeyWords field in RecordSource or at least not as the Bound Column.

    As shown by Vlad, you need to do pattern match. This means either InStr() as Vlad shows or LIKE operator. Can be calculated in query or in VBA.

    Me.cntKeywordsSubForm.Form.Filter = "[KeyWords] LIKE '*" & Me.txtSearch & "*'"

    Me.cntKeywordsSubForm.Form.Filter = "InStr(KeyWords, '" & Me.txtSearch & "')>0"
    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. Replies: 39
    Last Post: 07-28-2018, 12:27 PM
  2. Replies: 8
    Last Post: 09-01-2017, 02:33 PM
  3. Filter main form based on subform data
    By Varda in forum Forms
    Replies: 9
    Last Post: 01-02-2015, 10:40 PM
  4. Subform Filter based on Main Form Information
    By james.carpenter2 in forum Forms
    Replies: 0
    Last Post: 02-16-2011, 09:55 PM
  5. Replies: 1
    Last Post: 11-16-2010, 08:42 AM

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