Results 1 to 6 of 6
  1. #1
    nostr4d4m is offline Novice
    Windows XP Access 2003
    Join Date
    Mar 2011
    Posts
    11

    Requery a form with new criterior

    Hej,

    So i am making a advanced search with subform - top part is search criterior fields and bellow it a subform of search results. Now i have a need to use Multi-select List box. I have code to get all the selected values and build it into the string. Than lounch a query in new window based on selected values. But i would like to requery my old form insted of opening the new window.

    is there any way to do that?
    Can i set a query criterior to be a VBA value

  2. #2
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    Assuming that your main form is unbound and your subform is bound to a query, you, can with VBA, build the query dynamically based on the search criteria provided by the user and then assign the query as the record source for the subform. Constructing the query in code can be a little tricky. Here is the basic structure of the code. Assuming that you have a button on your main form to conduct the search, the code would go behind the on click event of the button.

    Code:
    Dim mySQL as string
     
    'your code to get the items from the list box
     
    mySQL= " SELECT..."
    mySQL= mySQL & " WHERE field="  & stringvariableholdinglistboxitems
     
    me.subformname.form.recordsource=mySQL

  3. #3
    nostr4d4m is offline Novice
    Windows XP Access 2003
    Join Date
    Mar 2011
    Posts
    11
    Ok it is working. But how do i now export this query ?
    DoCmd.OutputTo acOutputQuery and
    DoCmd.TransferSpreadsheet
    Exports original query and all my criterior disapears. Can i somehow target the query that i currently see in the form

  4. #4
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    You did not mention anything about exporting. I assume that you want to export the results of the query (i.e. the same records that are displayed in the subform) not the query itself. The query that you create in code, exists only in that code. If you want to use the docmd to export from that query, you have to save the query you created in code as a query in Access & then do the export. The following is a code snippet that does that. This code creates a query called myQuery does the export and then deletes the myQuery after the export is complete.

    Code:
    Dim dbs As DAO.Database
    Dim qdfTemp As DAO.QueryDef
    Dim strSQL As String
    Dim strQDF As String
    Dim strFile As String
    Set dbs = CurrentDb
                
    strSQL = "SELECT * FROM table1"
    strSQL = strSQL & " WHERE daterec=#" & Me.cboStartDate & "# AND enddate=#" & Me.cboEndDate & "# and company='" & Me.company & "'"
                            
    strFile = "mySpreadsheet.xls"
    strQDF = "myQuery"
     
    Set qdfTemp = dbs.CreateQueryDef(strQDF, strSQL)
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel3, strQDF, strFile
            
    dbs.QueryDefs.Delete ("myQUERY")
    dbs.Close
     Set dbs = Nothing

  5. #5
    evander is offline Competent Performer
    Windows 7 Access 2003
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    206
    Okay, I'm not here to help. I actually browsed at this thread. jzwp11, You're a genius. I'll remember your example. Glad I can benefit from someone else's experience. Thanks.

  6. #6
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    evander,

    I'm glad you were able to find it useful. The code was actually from someone else and I was just helping troubleshoot it.

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

Similar Threads

  1. Replies: 5
    Last Post: 03-17-2011, 06:21 AM
  2. Requery only one record on a form
    By mkallover in forum Forms
    Replies: 3
    Last Post: 01-26-2011, 09:31 AM
  3. Replies: 1
    Last Post: 03-26-2010, 10:32 PM
  4. Replies: 6
    Last Post: 01-13-2010, 02:41 PM
  5. Requery?
    By CO711 in forum Forms
    Replies: 0
    Last Post: 08-06-2008, 08:03 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