Results 1 to 7 of 7
  1. #1
    kirky is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2013
    Posts
    43

    Post Unbount textboxes in mainform to filter subform

    Dear Experts,

    I found a code in a forum which I think suitable to the one im looking for in filtering subform but i couldnt modified much due to limited knowledge in access vba.
    I am a novice in access & vba. Any idea on how to modify this code, please help me.

    I have a main form DesignManual where the textboxes for searching is located, it includes a button as well to filter and a button to clear the searches but when i run this code it wont work to filter the subform "tbleDesignManual".



    Code:
    Private Sub cmdfilter_Click()
            'Purpose:   Build up the criteria string form the non-blank search boxes, and apply to the form's Filter.
        'Notes:     1. We tack " AND " on the end of each condition so you can easily add more search boxes; _
                            we remove the trailing " AND " at the end.
        '           2. The date range works like this: _
                            Both dates      = only dates between (both inclusive. _
                            Start date only = all dates from this one onwards; _
                            End date only   = all dates up to (and including this one).
        Dim strWhere As String                  'The criteria string.
        Dim lngLen As Long                      'Length of the criteria string to append to.
        Const conJetDate = "\#mm\/dd\/yyyy\#"   'The format expected for dates in a JET query string.
        
        '***********************************************************************
        'Look at each search box, and build up the criteria string from the non-blank ones.
        '***********************************************************************
       
       If Not IsNull(Me.txtboxDocumentNo) Then
            strWhere = strWhere & "(Me!tblDesignManual.Form.DocumentNo  Like ""*" & Me.txtboxDocumentNo & "*"") AND "
        End If
        
         If Not IsNull(Me.txtboxDescription) Then
            strWhere = strWhere & "(Me!tblDesignManual.Form.DocumentDescription  Like ""*" & Me.txtboxDescription & "*"") AND "
        End If
       
        '***********************************************************************
        'Chop off the trailing " AND ", and use the string as the form's Filter.
        '***********************************************************************
        'See if the string has more than 5 characters (a trailng " AND ") to remove.
        lngLen = Len(strWhere) - 5
        If lngLen <= 0 Then     'Nah: there was nothing in the string.
            MsgBox "No criteria", vbInformation, "Nothing to do."
        Else                    'Yep: there is something there, so remove the " AND " at the end.
            strWhere = Left$(strWhere, lngLen)
            'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
            'Debug.Print strWhere
            
            'Finally, apply the string as the form's Filter.
            Me!tblDesignManual.Filter = strWhere
            Me!tblDesignManual.FilterOn = True
        End If
    End Sub
    
    Private Sub cmdclear_Click()
        'Purpose:   Clear all the search boxes in the Form Header.
        Dim ctl As Control
        
        'Clear all the controls in the Sub Form Detail section.
        For Each ctl In Me.Section(acDetail).Controls
            Select Case ctl.ControlType
            Case acTextBox, acComboBox
                ctl.Value = Null
            Case acCheckBox
                ctl.Value = False
            End Select
        Next
        
        'Remove the form's filter.
        Me!tblDesignManual.Form.FilterOn = False
    End Sub

  2. #2
    calaxan's Avatar
    calaxan is offline Novice
    Windows 7 32bit Access 2013 32bit
    Join Date
    Jan 2018
    Posts
    9
    Hi Kirky...

    I had same problem with u before...
    if you talk about sub form... you hv to connect between main and sub form.
    i found this tutorial http://www.iaccessworld.com/download...ord-ms-access/ and I follow his tutorial in you***e under AUSTIN72406 , it solved my problem.
    I think it could help also your difficulties.

    sorry if this reply is not solving ur problem
    CQ

  3. #3
    kirky is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2013
    Posts
    43
    Hello Calaxan...thank you so much for the reply I just want only to modify the code ive posted. I appreciate much your willingness to help..
    Have a nice day and thank you so much. I will try to check the link youve posted.

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,118
    Hi,
    Can you try:
    Private Sub cmdfilter_Click()
    'Purpose: Build up the criteria string form the non-blank search boxes, and apply to the form's Filter.
    'Notes: 1. We tack " AND " on the end of each condition so you can easily add more search boxes; _
    we remove the trailing " AND " at the end.
    ' 2. The date range works like this: _
    Both dates = only dates between (both inclusive. _
    Start date only = all dates from this one onwards; _
    End date only = all dates up to (and including this one).
    Dim strWhere As String 'The criteria string.
    Dim lngLen As Long 'Length of the criteria string to append to.
    Const conJetDate = "\#mm\/dd\/yyyy\#" 'The format expected for dates in a JET query string.

    '************************************************* **********************
    'Look at each search box, and build up the criteria string from the non-blank ones.
    '************************************************* **********************

    If Not IsNull(Me.txtboxDocumentNo) Then
    strWhere = strWhere & "[DocumentNo] Like ""*" & Me.txtboxDocumentNo & "*"" AND "
    End If

    If Not IsNull(Me.txtboxDescription) Then
    strWhere = strWhere & "[DocumentDescription] Like ""*" & Me.txtboxDescription & "*"" AND "
    End If

    '************************************************* **********************
    'Chop off the trailing " AND ", and use the string as the form's Filter.
    '************************************************* **********************
    'See if the string has more than 5 characters (a trailng " AND ") to remove.
    lngLen = Len(strWhere) - 5
    If lngLen <= 0 Then 'Nah: there was nothing in the string.
    MsgBox "No criteria", vbInformation, "Nothing to do."
    Else 'Yep: there is something there, so remove the " AND " at the end.
    strWhere = Left$(strWhere, lngLen)
    'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
    'Debug.Print strWhere

    'Finally, apply the string as the form's Filter.
    Me!tblDesignManual.Form.Filter = strWhere
    Me!tblDesignManual.Form.FilterOn = True
    End If
    End Sub

    Private Sub cmdclear_Click()
    'Purpose: Clear all the search boxes in the Form Header.
    Dim ctl As Control

    'Clear all the controls in the Sub Form Detail section.
    For Each ctl In Me.Section(acDetail).Controls
    Select Case ctl.ControlType
    Case acTextBox, acComboBox
    ctl.Value = Null
    Case acCheckBox
    ctl.Value = False
    End Select
    Next

    'Remove the form's filter.
    Me!tblDesignManual.Form.FilterOn = False
    End Sub

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,118
    Sorry, somehow the code tags got removed...
    Code:
    Private Sub cmdfilter_Click()
    'Purpose: Build up the criteria string form the non-blank search boxes, and apply to the form's Filter.
    'Notes: 1. We tack " AND " on the end of each condition so you can easily add more search boxes; _
    we remove the trailing " AND " at the end.
    ' 2. The date range works like this: _
    Both dates = only dates between (both inclusive. _
    Start date only = all dates from this one onwards; _
    End date only = all dates up to (and including this one).
    Dim strWhere As String 'The criteria string.
    Dim lngLen As Long 'Length of the criteria string to append to.
    Const conJetDate = "\#mm\/dd\/yyyy\#" 'The format expected for dates in a JET query string.
    
    '************************************************* **********************
    'Look at each search box, and build up the criteria string from the non-blank ones.
    '************************************************* **********************
    
    If Not IsNull(Me.txtboxDocumentNo) Then
    strWhere = strWhere & "[DocumentNo] Like ""*" & Me.txtboxDocumentNo & "*"" AND "
    End If
    
    If Not IsNull(Me.txtboxDescription) Then
    strWhere = strWhere & "[DocumentDescription] Like ""*" & Me.txtboxDescription & "*"" AND "
    End If
    
    '************************************************* **********************
    'Chop off the trailing " AND ", and use the string as the form's Filter.
    '************************************************* **********************
    'See if the string has more than 5 characters (a trailng " AND ") to remove.
    lngLen = Len(strWhere) - 5
    If lngLen <= 0 Then 'Nah: there was nothing in the string.
    MsgBox "No criteria", vbInformation, "Nothing to do."
    Else 'Yep: there is something there, so remove the " AND " at the end.
    strWhere = Left$(strWhere, lngLen)
    'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
    'Debug.Print strWhere
    
    'Finally, apply the string as the form's Filter.
    Me!tblDesignManual.Form.Filter = strWhere
    Me!tblDesignManual.Form.FilterOn = True
    End If
    End Sub
    
    Private Sub cmdclear_Click()
    'Purpose: Clear all the search boxes in the Form Header.
    Dim ctl As Control
    
    'Clear all the controls in the Sub Form Detail section.
    For Each ctl In Me.Section(acDetail).Controls
    Select Case ctl.ControlType
    Case acTextBox, acComboBox
    ctl.Value = Null
    Case acCheckBox
    ctl.Value = False
    End Select
    Next
    
    'Remove the form's filter.
    Me!tblDesignManual.Form.FilterOn = False
    End Sub

  6. #6
    kirky is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2013
    Posts
    43
    Dear Gicu,

    Good day!

    Sorry for the late reply, you are such a brilliant person....thank you so much....It really works...Thank you once again in sharing your expertise.
    May you continue sharing your expertise and the rest of the experts here in this forum.

    As a sign of respect and appreciation, I will repost the code together with the screenshot of the db form that ive been working with.

    This is the routine of my db mainform "DesignManual" with a subform "tblDesignManual" if the user what to search.
    1. Two (2) unbound textboxes in the main form where the user can search the document number "txtboxDocumentNo" or by description "txtboxDescription" in wildcard search.
    2. Two (2) buttons in main form that holds a corresponding command.
    1) Search button "cmdfilter" to filter the subform
    2) Clear button "cmdclear" to clear the unbound textboxes in mainform and unfilter the subform. In particular, fields of DocumentNo and DocumentDescription.

    Here is the code:
    [CODE]
    Private Sub cmdfilter_Click()
    'Purpose: Build up the criteria string form the non-blank search boxes, and apply to the form's Filter.
    'Notes: 1. We tack " AND " on the end of each condition so you can easily add more search boxes; _
    we remove the trailing " AND " at the end.
    ' 2. The date range works like this: _
    Both dates = only dates between (both inclusive. _
    Start date only = all dates from this one onwards; _
    End date only = all dates up to (and including this one).
    Dim strWhere As String 'The criteria string.
    Dim lngLen As Long 'Length of the criteria string to append to.
    Const conJetDate = "\#mm\/dd\/yyyy\#" 'The format expected for dates in a JET query string.

    '************************************************* **********************
    'Look at each search box, and build up the criteria string from the non-blank ones.
    '************************************************* **********************

    If Not IsNull(Me.txtboxDocumentNo) Then
    strWhere = strWhere & "([DocumentNo] Like ""*" & Me.txtboxDocumentNo & "*"") AND "
    End If

    If Not IsNull(Me.txtboxDescription) Then
    strWhere = strWhere & "([DocumentDescription] Like ""*" & Me.txtboxDescription & "*"") AND "
    End If

    '************************************************* **********************
    'Chop off the trailing " AND ", and use the string as the form's Filter.
    '************************************************* **********************
    'See if the string has more than 5 characters (a trailng " AND ") to remove.
    lngLen = Len(strWhere) - 5
    If lngLen <= 0 Then 'Nah: there was nothing in the string.
    MsgBox "No criteria", vbInformation, "Nothing to do."
    Else 'Yep: there is something there, so remove the " AND " at the end.
    strWhere = Left$(strWhere, lngLen)
    'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
    'Debug.Print strWhere

    'Finally, apply the string as the form's Filter.
    Me!tblDesignManual.Form.Filter = strWhere
    Me!tblDesignManual.Form.FilterOn = True
    End If
    End Sub

    Private Sub cmdclear_Click()
    'Purpose: Clear all the search boxes in the Form Header, and show all records again.
    Dim ctl As Control

    'Clear all the controls in the Form Header section.
    For Each ctl In Me.Section(acDetail).Controls
    Select Case ctl.ControlType
    Case acTextBox, acComboBox
    ctl.Value = Null
    Case acCheckBox
    ctl.Value = False
    End Select
    Next

    'Remove the form's filter.
    Me!tblDesignManual.Form.FilterOn = False
    End Sub
    [CODE]

    Thank you so much Gicu!

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,118
    Hi kirky,

    Glad I could help, definitely not brilliant ... .

    Good luck!
    Vlad

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

Similar Threads

  1. Replies: 1
    Last Post: 03-22-2015, 02:21 PM
  2. Replies: 1
    Last Post: 11-07-2012, 05:31 PM
  3. Replies: 4
    Last Post: 03-30-2012, 01:47 AM
  4. Replies: 1
    Last Post: 11-29-2011, 01:37 AM
  5. Filter a subform based on mainform
    By Cheshire101 in forum Queries
    Replies: 3
    Last Post: 01-06-2011, 12:56 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