Results 1 to 10 of 10
  1. #1
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    215

    Create Report from Search Criteria

    Hi,

    I have a search form and I want to have the results go into a report (whatever they may be). The search form has a subform in it where it shows the criteria that matches the search. I want that information to go into my report that I am creating. I have included the code for my search form below. There will be a button that will have on the form that when clicked should show the results just as the subform does, but as a separate report. I can't figure out how to incorporate the BuildFilter into the report click similar to how it is done for the Search_Click().



    Any suggestions?


    Code:
    Private Sub Search_Click()
    'Update the record source
    Me.SearchSubform.Form.RecordSource = "Select * From Search " & BuildFilter
    'Requery the subform
    Me.Form!SearchSubform.Form.Requery
    End Sub
    Private Function BuildFilter() As Variant
    Dim varWhere As Variant
    varWhere = Null 'Main Filter
    'Check for LIKE Status
    If Me.Status > "" Then
        varWhere = varWhere & "[Status] LIKE '*" & Me.Status & "*' And "
    End If
    'Check for LIKE Last Name
    If Me.LastName > "" Then
        varWhere = varWhere & "[Last Name] Like '*" & Me.LastName & "*' And "
    End If
    'Check for LIKE First Name
    If Me.FirstName > "" Then
        varWhere = varWhere & "[First Name] LIKE '*" & Me.FirstName & "*' And "
    End If
    'Check for LIKE Account Number
    If Me.AccountNumber > "" Then
        varWhere = varWhere & "[Account Number] LIKE '*" & Me.AccountNumber & "*' And "
    End If
    'Check for LIKE Social Security Number
    If Me.SocialSecurityNumber > "" Then
        varWhere = varWhere & "[Social Security Number] LIKE '*" & Me.SocialSecurityNumber & "*' And "
    End If
    'Check for LIKE Entity Name
    If Me.EntityName > "" Then
        varWhere = varWhere & "[EntityName] LIKE '*" & Me.EntityName & "*' And "
    End If
    'Check for LIKE EIN
    If Me.EIN > "" Then
        varWhere = varWhere & "[EIN] LIKE '*" & Me.EIN & "*' And "
    End If
    'Check if there is a filter to return...
    If IsNull(varWhere) Then
        varWhere = ""
    Else
        varWhere = "WHERE" & varWhere
        
    ' strip off last "AND" in the filter
    If Right(varWhere, 5) = " AND " Then
        varWhere = Left(varWhere, Len(varWhere) - 5)
        End If
    End If
    BuildFilter = varWhere
    Dim db As DAO.Database
       Dim qdf As DAO.QueryDef
       Dim varItem As Variant
       Dim strCriteria As String
       Dim strSQL As String
       Set db = CurrentDb()
       Set qdf = db.QueryDefs("Search")
       If Me!Company.ItemsSelected.Count > 0 Then
          For Each varItem In Me!Company.ItemsSelected
             strCriteria = strCriteria & "[Account List].[Company Name] = " & Chr(34) & Me!Company.ItemData(varItem) & Chr(34) & "OR "
          Next varItem
          strCriteria = Left(strCriteria, Len(strCriteria) - 3)
       Else
          strCriteria = "[Account List].[Company Name] Like '*'"
       End If
       strSQL = "SELECT * FROM [Account List] " & "WHERE " & strCriteria & ";"
       qdf.SQL = strSQL
       Set db = Nothing
       Set qdf = Nothing
    End Function

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,630
    Possibly store the value of strCriteria into a textbox (I do that) or a global variable. Then:
    DoCmd.OpenReport "report name", , , Me.textboxname [or the variable]
    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
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    215
    The strCriteria is from a mulit-select extended listbox. If I store the values into a textbox, and I have multiple items selected, how will that work? I understand the concept if it didn't allow multi-select.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,630
    You have a procedure that builds a string that is used in the WHERE argument of an SQL statement. Save that string to a textbox on the form and the string is available for use in other procedures, such as button to open report.

    You are using the string to modify a query definition - why? What is that query used for? I don't modify query definition just to get a filtered form or report. I use the WHERE CONDITION argument of OpenForm/OpenReport method to pass filter criteria.
    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.

  5. #5
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    215
    You are using the string to modify a query definition - why? What is that query used for? I don't modify query definition just to get a filtered form or report. I use the WHERE CONDITION argument of OpenForm/OpenReport method to pass filter criteria.
    Please see this thread..https://www.accessforums.net/forms/s...orm-28370.html

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,630
    Not sure how that thread relates to the question posed in this one.

    You want to know how to use the constructed criteria string to filter a report? I think I have addressed that requirement.

    I still don't know why you are modifying query definition.
    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
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    215
    I included that thread because that is where I received help/guidance on how to use a multi-select listbox to filter my search form. In one of the links provided, it used the format for the query. That is why I have that in there. That query has no other function (as of now).

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,630
    Okay, so the procedure to build criteria string works? Want to use the string to filter report? Has my suggestion helped?

    Either use the string in the OpenReport method or use the query that you are modifying as the RecordSource for report. As I said, I don't do the latter.
    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
    cbrsix is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    215
    I haven't had a chance to give it a try yet, but will do tomorrow. Is one way more efficient than the other?

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,630
    Probably just a matter of preference. I prefer not to alter saved Access queries. And in fact, most of my forms and reports don't have an Access query as RecordSource - I often build the SQL statement directly in the RecordSource property.
    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. Opening a report with search criteria through a combo box
    By very_much_a_beginner in forum Reports
    Replies: 1
    Last Post: 07-16-2012, 01:30 PM
  2. Replies: 5
    Last Post: 07-13-2012, 01:15 AM
  3. Search By Criteria - Flexible Criteria Fields
    By lilanngel in forum Access
    Replies: 0
    Last Post: 03-16-2011, 06:25 PM
  4. Replies: 0
    Last Post: 02-28-2011, 09:46 AM
  5. How do I create a name search ?
    By nightviperdark in forum Access
    Replies: 1
    Last Post: 11-23-2009, 09:53 AM

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