Results 1 to 9 of 9
  1. #1
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272

    Unable to create a search button and generate printable filtered report.

    I have a database which am able to filter the results to be displayed as a split form data for one record which is the date.



    I used the code :

    Me.Filter = "[FieldNameHere]= #" & Me.YourComboBoxNameHere & "#"
    Me.FilterOn = True


    I did a little research and I found another code that was able to filter the date by staff_name.

    Here is the code:

    Me.Filter = "[FieldNameHere]= " & Chr(34) & Me.YourComboBoxNameHere & Chr(34)
    Me.FilterOn = True


    I will like to create a search button that will able to filter the data by both date and staff_name.

    If staff_name is left blank, search button should filter data for only date and vice versa.

    I will like to be able to create a button to generate a printable report for the results from the search

    Any help to be able to obtain a code to achieve this results will be greatly appreciated.

  2. #2
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    you would do something like the code below to create the filter functionality. Also take a look at this example: http://allenbrowne.com/ser-62.html
    Code:
        Dim criteria As String
        
        'check to see if the name combo box has a selection
        If Not IsNull(Me.YourComboBoxNameHere) Then
            criteria = "[NAME_FieldNameHere] = """ & Me.YourComboBoxNameHere & """ AND "
        End If
        
        'check to see if there is a date selected
        If Not IsNull(Me.YourDateControlHere) Then
            criteria = criteria & "[DATE_FieldNameHere] = #" & Me.YourDateControlHere & "# AND "
        End If
        
        'if the user made at least one selection then apply the filter
        If criteria <> "" Then
            'remove the trailing " AND "
            criteria = Left(criteria, Len(criteria) - 5)
            
            'apply the filter
            Me.Filter = criteria
            Me.FilterOn = True
        Else
            'remove the filter
            Me.Filter = ""
            Me.FilterOn = False
        End If

  3. #3
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    Oh and as for the report you would use DoCmd.OpenReport to open your report and filter the report using the criteria string created above
    Code:
    DoCmd.OpenReport "YOUR REPORT NAME HERE", acViewPreview, , criteria
    review this: https://docs.microsoft.com/en-us/off...cmd.openreport

  4. #4
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272
    Thanks so much. I tried the codes and it worked for me.
    Thanks so much for your time and assistance.

    I really appreciate

  5. #5
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272
    Quote Originally Posted by kd2017 View Post
    you would do something like the code below to create the filter functionality. Also take a look at this example: http://allenbrowne.com/ser-62.html
    Code:
        Dim criteria As String
        
        'check to see if the name combo box has a selection
        If Not IsNull(Me.YourComboBoxNameHere) Then
            criteria = "[NAME_FieldNameHere] = """ & Me.YourComboBoxNameHere & """ AND "
        End If
        
        'check to see if there is a date selected
        If Not IsNull(Me.YourDateControlHere) Then
            criteria = criteria & "[DATE_FieldNameHere] = #" & Me.YourDateControlHere & "# AND "
        End If
        
        'if the user made at least one selection then apply the filter
        If criteria <> "" Then
            'remove the trailing " AND "
            criteria = Left(criteria, Len(criteria) - 5)
            
            'apply the filter
            Me.Filter = criteria
            Me.FilterOn = True
        Else
            'remove the filter
            Me.Filter = ""
            Me.FilterOn = False
        End If

    I created another form which follows similar criteria.

    In this form, we will maintain the details in the date combo but we will use a textbox instead of a combo list for the other combo.
    This new textbox will pick data from a numeric field.

    I tried to modify the code to meet this criteria but looks like the code for that combo doesn’t work for numeric a data.

    Can you modify the code for me so it could work for a numeric data?

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Numerics do not have quotes surrounding them?

    Code:
     criteria = "[NAME_FieldNameHere] = " & Me.YourNumericControl & " AND "
    Strings need single quotes ' or triple double quotes " (I think)
    Dates need # and be in mm/dd/yyyy or yyyy-mm-dd formats

    HTH
    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

  7. #7
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272
    Quote Originally Posted by Welshgasman View Post
    Numerics do not have quotes surrounding them?

    Code:
     criteria = "[NAME_FieldNameHere] = " & Me.YourNumericControl & " AND "
    Strings need single quotes ' or triple double quotes " (I think)
    Dates need # and be in mm/dd/yyyy or yyyy-mm-dd formats

    HTH

    Alright. Will give it a try and see how it goes.
    Thanks a lot

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Before you ever use a filter like this, put it all into a string variable., which kd2017 has done for you here.
    Then you can Debug.Print it to see what you actually have, not what you think you have.

    Once you get it correct, you can use the string variable. So in your case
    Debug.Print criteria

    You can then also copy and paste it back here, so someone can advise.
    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

  9. #9
    Emmanuel is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    272
    Quote Originally Posted by Welshgasman View Post
    Before you ever use a filter like this, put it all into a string variable., which kd2017 has done for you here.
    Then you can Debug.Print it to see what you actually have, not what you think you have.

    Once you get it correct, you can use the string variable. So in your case
    Debug.Print criteria

    You can then also copy and paste it back here, so someone can advise.
    Thanks so much for your help.
    And also the further explanations given.
    I really appreciate.

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

Similar Threads

  1. FIltered Report based on filtered SubForm
    By Misterpokey in forum Programming
    Replies: 1
    Last Post: 03-16-2020, 08:27 AM
  2. Replies: 8
    Last Post: 03-15-2018, 11:51 AM
  3. Advice - getting data filtered to generate report
    By ss188 in forum Database Design
    Replies: 3
    Last Post: 03-24-2017, 08:21 AM
  4. Create a printable report of certain records
    By gjpcrest in forum Access
    Replies: 3
    Last Post: 09-08-2016, 01:18 AM
  5. Replies: 3
    Last Post: 05-18-2015, 08:46 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