Page 1 of 3 123 LastLast
Results 1 to 15 of 34
  1. #1
    lewis1682 is offline Advanced Beginner
    Windows 7 32bit Access 2003
    Join Date
    Aug 2013
    Posts
    37

    Save subforms filtered results to table.

    Hi

    I have a form called filter which has some textboxes on it. Also in the form I have a subform called "View_Contacts_subform" This is bound to a table called "Contacts" The subform has the following fields: FirstName, LastName, Company, County, Category.

    I am using my filter form to filter the subform which works great it shows what I want to see. However I want to be able to save these results into a table which will be overwritten each time. This table will then be used to email them and make emails. If possible I would also like to include a checkbox which the user can untick if they do not want them to be used to it is removed from the table. The table I want all this information to be stored on is called "Filter_Results"

    I have tried using a SQL statement to do this, however it only adds one record not all of them that are showing on the form. For example I have 5 records. I filter the records down to A and B. When it saves them to the table it only saves record A not B. I am only saving the FirstName for now because that is easier for me to work with then I will add the other fields.

    I was thinking is there a way to count the number of records then loop though each one, that is beyond me, here is my code on the "Search" button on my Filter_Contacts form which holds the subform

    Code:
    Dim strWhere As String    Dim lngLen As Long
        
        If Not IsNull(Me.s_PostalCode) Then
            strWhere = strWhere & "([PostalCode] = """ & s_PostalCode & """) AND "
        End If
        
        If Not IsNull(Me.s_Company) Then
            strWhere = strWhere & "([Company] Like ""*" & Me.s_Company & "*"") AND "
        End If
    
    
        If Not IsNull(Me.s_County) Then
            strWhere = strWhere & "([County] Like ""*" & Me.s_County & "*"") AND "
        End If
        
        If Not IsNull(Me.CategoryList) Then
            strWhere = strWhere & "([Category] Like ""*" & Me.CategoryList & "*"") AND "
        End If
        
        If Me.AGM = -1 Then
            strWhere = strWhere & "([AGM] = true) AND "
        End If
        
        If Me.s_Newsletter = -1 Then
            strWhere = strWhere & "([Newsletter] = true) AND "
        End If
        
    
    
        lngLen = Len(strWhere) - 5
        If lngLen <= 0 Then
            MsgBox "No criteria", vbInformation, "Nothing to do."
        Else
            strWhere = Left$(strWhere, lngLen)
            
            Me.View_Contacts_subform.Form.Filter = strWhere
            Me.View_Contacts_subform.Form.FilterOn = True
        End If
        Dim MySQL As String
        
        MySQL = "INSERT INTO Filter_Results (FirstName) SELECT [Forms]![Filter_Contacts]![View_Contacts_subform].[Form]![FirstName] AS FirstName;"
        DoCmd.RunSQL MySQL


  2. #2
    mrojas is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Sep 2013
    Location
    Concord California
    Posts
    72
    Have you tried, on your MySQL statement:
    MySQL= "INSERT INTO Filter_Results " & strWhere

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    As mrojas, shows, must concatenate variables. Enclosing within quotes results in the literal string that is the variable name, not the contents of the variable. And why would you reference the FirstName control instead of the filter string constructed by code?
    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.

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    I built a warehouse module where I use loop commands to bring records from a purchase order table to an inventory table. I can post an example using DAO later today if you are interested in going that route.

  5. #5
    lewis1682 is offline Advanced Beginner
    Windows 7 32bit Access 2003
    Join Date
    Aug 2013
    Posts
    37
    After changing my code to:
    Code:
        Dim strWhere As String    Dim lngLen As Long
        
        If Not IsNull(Me.s_PostalCode) Then
            strWhere = strWhere & "([PostalCode] = """ & s_PostalCode & """) AND "
        End If
        
        If Not IsNull(Me.s_Company) Then
            strWhere = strWhere & "([Company] Like ""*" & Me.s_Company & "*"") AND "
        End If
    
    
        If Not IsNull(Me.s_County) Then
            strWhere = strWhere & "([County] Like ""*" & Me.s_County & "*"") AND "
        End If
        
        If Not IsNull(Me.CategoryList) Then
            strWhere = strWhere & "([Category] Like ""*" & Me.CategoryList & "*"") AND "
        End If
        
        If Me.AGM = -1 Then
            strWhere = strWhere & "([AGM] = true) AND "
        End If
        
        If Me.s_Newsletter = -1 Then
            strWhere = strWhere & "([Newsletter] = true) AND "
        End If
        
    
    
        lngLen = Len(strWhere) - 5
        If lngLen <= 0 Then
            MsgBox "No criteria", vbInformation, "Nothing to do."
        Else
            strWhere = Left$(strWhere, lngLen)
            
            Me.View_Contacts_subform.Form.Filter = strWhere
            Me.View_Contacts_subform.Form.FilterOn = True
        End If
        Dim MySQL As String
        
        MySQL = "INSERT INTO Filter_Results" & strWhere
        DoCmd.RunSQL MySQL
    I get a error saying "Error in INSERT INTO syntax"

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Your INSERT INTO does not identify fields to be populated nor where to select data from. Your first structure was closer to correct.

    If you want to overwrite the records, then first delete all of them.

    Instead of RunSQL which will trigger warning message unless you precede with SetWarnings = False (then reset with SetWarnings = True), try like:

    CurrentDb.Execute "DELETE FROM Filter_Results"
    CurrentDb.Execute "INSERT INTO Filter_Results(field1, field2, field3, field4, field5) SELECT field1, field2, field3, field4, field5 FROM tablename WHERE " & strWhere
    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
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    I was able to duplicate your single record error. I was testing some code using ADO to create the new records and ran into the same problem during certain instances where I passed the filter set to and from an SQL string.

    The single record symptom did not occur when I passed the where clause to the subform’s recordset verses passing it to the .Filter.

    Example:
    'start code
    Dim rcd As Recordset
    Dim stSQL As String
    Dim strNewWhere As String
    strNewWhere = Me.View_Contacts_subform.Form.Filter
    Me.View_Contacts_subform.Form.FilterOn = False
    stSQL = ""
    stSQL = "SELECT * FROM [Contacts]"
    stSQL = stSQL & " WHERE " & strNewWhere
    stSQL = stSQL & " ORDER BY [LastName]"
    Me.View_Contacts_subform.Form.RecordSource = stSQL
    Me.View_Contacts_subform.Form.Requery
    Set rcd = Me.View_Contacts_subform.Form.RecordsetClone 'This is your master recordset
    Dim intCount As Integer
    intCount = rcd.RecordCount
    MsgBox intCount
    'end code

    I did not use the INSERT via SQL to update the table in the model I built. However, I did stumble upon a single record within the recordset when there should have been multiple records while using the .Filter exclusively. Perhaps passing your strWhere clause into the SQL example above will allow you to change your subform's recordset rather than using the subforms Filter.

    It is just a suggestion. I was surprised to stumble across the single record count when I was looking at multiple filtered records. Somewhere between having the filtered records and adding another field using "AND" it would return a single record.

  8. #8
    lewis1682 is offline Advanced Beginner
    Windows 7 32bit Access 2003
    Join Date
    Aug 2013
    Posts
    37
    I added in the following code to the button:
    Code:
    CurrentDb.Execute "DELETE FROM Filter_Results"
    CurrentDb.Execute "INSERT INTO Filter_Results(ID,Title,FirstName,LastName,JobTitle,Company,AddressLine1,AddressLine2,Town,County,PostalCode,Phone,Fax,Email,Website,Category,Include,Notes,Newsletter,AGM) SELECT ID,Title,FirstName,LastName,JobTitle,Company,AddressLine1,AddressLine2,Town,County,PostalCode,Phone,Fax,Email,Website,Category,Include,Notes,Newsletter,AGM FROM Filter_Results WHERE" & strWhere
    There is no error when it runs however if I open up the table nothing is there, I don't get it! If I change it into a MySql statement yes it does ask if you want to append the rows, however it says do you want to append 0 rows meaning its not picking the data up using the strWhere from the Contacts Table.

  9. #9
    mrojas is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Sep 2013
    Location
    Concord California
    Posts
    72
    How about sharing with us a copy of the database so that we can take a closer look?

  10. #10
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by mrojas View Post
    How about sharing with us a copy of the database so that we can take a closer look?
    Just a heads up. OP states the main form is named "Filter".

  11. #11
    lewis1682 is offline Advanced Beginner
    Windows 7 32bit Access 2003
    Join Date
    Aug 2013
    Posts
    37
    Quote Originally Posted by ItsMe View Post
    I was able to duplicate your single record error. I was testing some code using ADO to create the new records and ran into the same problem during certain instances where I passed the filter set to and from an SQL string.

    The single record symptom did not occur when I passed the where clause to the subform’s recordset verses passing it to the .Filter.

    Example:
    'start code
    Dim rcd As Recordset
    Dim stSQL As String
    Dim strNewWhere As String
    strNewWhere = Me.View_Contacts_subform.Form.Filter
    Me.View_Contacts_subform.Form.FilterOn = False
    stSQL = ""
    stSQL = "SELECT * FROM [Contacts]"
    stSQL = stSQL & " WHERE " & strNewWhere
    stSQL = stSQL & " ORDER BY [LastName]"
    Me.View_Contacts_subform.Form.RecordSource = stSQL
    Me.View_Contacts_subform.Form.Requery
    Set rcd = Me.View_Contacts_subform.Form.RecordsetClone 'This is your master recordset
    Dim intCount As Integer
    intCount = rcd.RecordCount
    MsgBox intCount
    'end code

    I did not use the INSERT via SQL to update the table in the model I built. However, I did stumble upon a single record within the recordset when there should have been multiple records while using the .Filter exclusively. Perhaps passing your strWhere clause into the SQL example above will allow you to change your subform's recordset rather than using the subforms Filter.

    It is just a suggestion. I was surprised to stumble across the single record count when I was looking at multiple filtered records. Somewhere between having the filtered records and adding another field using "AND" it would return a single record.
    Is there a way I could use this method and then save the recordset into the "Filter_Results" table?

  12. #12
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by lewis1682 View Post
    Is there a way I could use this method and then save the recordset into the "Filter_Results" table?
    What you are looking at there is my effort to persuade you to change your search form so it saves the results as a full recordset and passes it to your subform's recordsource.

    Your problem may be associated with using .Filter. However, .Filter is not a deal breaker and should work too. I only believe changing the form's recordsource is a more stable foundation to work with. Any search form or cascading pull down examples I have use .Recordsource.

    MrOjas is requesting a copy of your DB. It seems there may be several small issues that compound symptoms or build hurdles into your foundation.
    Last edited by ItsMe; 09-15-2013 at 08:32 AM. Reason: typo

  13. #13
    lewis1682 is offline Advanced Beginner
    Windows 7 32bit Access 2003
    Join Date
    Aug 2013
    Posts
    37
    Okay so here is my database. You will have to bypass the start up using the shift key, you will also have to re-link the tables. I have removed all the forms that aren't linked to the Filter_Contacts form. This is the form which I am having the problem with, I need the tasks at the bottom to be able to read the data, which is why I think all the results should be stored on the local table "Filter_Results"DB Issues.zip

  14. #14
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    I got it working. I added a command button labeled New Command Button and named cmdNewExecute. I imported your BE tables to the front end for testing purposes.

    I inserted the code I posted into the Command22 click event; at the end. It transfers your code to the subform's recordset. Maybe you can do some reverse engineering and decide how you want to approach it in the end.

    Will post back in a few. Just want to smash it with a hammer a couple times before I upload the example.

  15. #15
    mrojas is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Sep 2013
    Location
    Concord California
    Posts
    72
    I've started to look into your challenge. First, you need to change your validation process from "If Not IsNull(Me.s_PostalCode)" because when you click on the clear button, you clear it by inserting a "blank" space, which then make the field non-null.
    For instance, if after clicking the Clear button you enter a company for criteria and clicked on the search, your condition when checking the PostalCode is no longer null, but a space. When you do the search, your statement is looking for PostalCode="" and Company="whatever the user entered"; thus the search fails, unless there is a record with a "blank" space for postal code.

    Will keep looking into other problem areas.

Page 1 of 3 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Exporting filtered results to Excel
    By doublec9 in forum Import/Export Data
    Replies: 2
    Last Post: 06-04-2013, 09:05 PM
  2. Mail merge for filtered results
    By har273 in forum Access
    Replies: 1
    Last Post: 01-03-2012, 12:12 PM
  3. Export Filtered Results to Excel Tabs
    By opod in forum Programming
    Replies: 1
    Last Post: 08-03-2011, 01:33 PM
  4. Save Combobox results to Table
    By Kinder0177 in forum Access
    Replies: 5
    Last Post: 06-29-2011, 08:47 AM
  5. Replies: 1
    Last Post: 10-24-2010, 04:01 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