Results 1 to 7 of 7
  1. #1
    epardo87 is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Dec 2016
    Posts
    102

    Retrieve top N values through criteria


    Hello all,

    • I have a form with a textbox, where they can select how many records to see on the query (5,10,15 or 20)
    • Among the query columns, one is "Due Date"


    how can I tell the query through the criteria field to show me the top (selected) records, sorted by earliest "Due Date"?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    create a query ,say: qsResults
    now the user will pick items on the form, and press a GO button.
    now when the user makes choices on the form, the code will reWrite the query for the TOP n...

    Code:
    sub btnGo_click()
    dim qdf as querydef
    dim sSql as string
    
    if IsNull(me.cboTop) then
      sSql = "SELECT * FROM tData WHERE [Due Date]=forms!myForm!txtDate
    else
      sSql = "SELECT TOP " &  me.cboTop &  " tData.* FROM tData WHERE [Due Date]=forms!myForm!txtDate
    endif
    
         'save the sql as a qry or open the sql
    
    set qdf = currentdb.querdefs("qsResults")
    qdf.sql = ssql
    qdf.close
    docmd.openquery qdf.name
    end sub

  3. #3
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    See Top N Values by Group: http://allenbrowne.com/subquery-01.html

  4. #4
    epardo87 is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Dec 2016
    Posts
    102
    Quote Originally Posted by ranman256 View Post
    create a query ,say: qsResultsnow the user will pick items on the form, and press a GO button.now when the user makes choices on the form, the code will reWrite the query for the TOP n...
    Code:
    sub btnGo_click()dim qdf as querydefdim sSql as stringif IsNull(me.cboTop) then  sSql = "SELECT * FROM tData WHERE [Due Date]=forms!myForm!txtDateelse  sSql = "SELECT TOP " &  me.cboTop &  " tData.* FROM tData WHERE [Due Date]=forms!myForm!txtDateendif     'save the sql as a qry or open the sqlset qdf = currentdb.querdefs("qsResults")qdf.sql = ssqlqdf.closedocmd.openquery qdf.nameend sub
    Thanks for your answer,

    Lets say it is a little more complicated than what I exposed, Let me tell you the complete thing this is supposed to do:

    Form frmExpiring


    • Combobox cboWeeks (has values: 6;5;4;3;2;1;0) this will show results within the next N weeks ("Due Date" column in query)
    • Combobox cboCerts (has values: 20;15;10;5;0) this will show the top N results with earliest dates ("Due Date" column in query)


    • Option opWeeks (enables cboWeeks and disables cboCerts)
    • Option opCerts (enables cboCerts and disables cboWeeks)


    • Buttons btnOK opens a form with an embedded report made with the query qryExpiring

    I already have everything working fine, the ok button already opens the form with the report of the unfiltered query embedded....

    I was building a formula in the criteria field but it seems it doesn't work...

    Code:
    Like IIf([forms]![frmExpiring]![lstWeeks]>0,Between Now() And (Now()+[forms]![frmExpiring]![lstWeeks]),"*")
    tough it is not filtering the dates correctly and I was thinking on replacing the last "*" for a code filtering the top records...

  5. #5
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    Date math is done using
    DateAdd("d",6,[datefield])

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    It looks like you are using VBA - but there are a couple of errors.

    Code:
    Sub btnGo_click()
        Dim qdf As QueryDef
        Dim sSql As String
    
        If IsNull(Me.cboTop) Then
            sSql = "SELECT * FROM tData WHERE [Due Date]= #" & forms!myForm!txtDate & "#;"
        Else
            sSql = "SELECT TOP " & Me.cboTop & " tData.* FROM tData WHERE [Due Date] = forms!myForm!txtDate"
        End If
    
        'save the sql as a qry or open the sql
    
        Set qdf = CurrentDb.querdefs("qsResults")
        qdf.SQL = sSql
        qdf.Close
        DoCmd.OpenQuery qdf.Name
    End Sub
    You must concatenate the parts in RED in the code above.
    And you must use delimiters around the dates.
    Add a Debug.Print to see if the SQL is formed correctly.
    Set a breakpoint after the Debug statement, copy the SQL from the immediate window and paste it into a new query. Execute the query to see if records are returned.
    To set a breakpoint, click in the line, then press the F9 key.
    (For debugging info, see http://www.cpearson.com/excel/debug.htm)

    If/when everything is OK, comment out the Debug statement.

    Something like this:
    Code:
    Sub btnGo_click()
        Dim qdf As QueryDef
        Dim sSql As String
    
        If IsNull(Me.cboTop) Then
            sSql = "SELECT * FROM tData WHERE [Due Date]= #" & Forms!myForm!txtDate & "#;"
        Else
            sSql = "SELECT TOP " & Me.cboTop & " tData.* FROM tData WHERE [Due Date] = #" & Forms!myForm!txtDate & "#;"
        End If
        Debug.Print sSql
    
        'save the sql as a qry or open the sql
        Set qdf = CurrentDb.querdefs("qsResults")  '<<-- set a breakpoint at this line then look at the immediate window for the SQL
        qdf.SQL = sSql
        qdf.Close
        DoCmd.OpenQuery qdf.Name
    End Sub

  7. #7
    epardo87 is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Dec 2016
    Posts
    102
    Quote Originally Posted by ranman256 View Post
    Date math is done using
    DateAdd("d",6,[datefield])
    I tried this, but a message appears saying: "this expression is typed incorrectly, or is too complex to be evaluated. For example a numeric expression may contain too many complicated elements. Try simplifying the expression by assigning parts of the expression to variables"

    Code:
    Between Now() And DateAdd("ww",[forms]![frmExpiring]![lstWeeks],Now())
    I tested the between formula in a more simple way and it works the way it is below, I just need to change the 20 for the listbox value, I tried it with "[forms]![frmExpiring]![lstWeeks]" but there's no success...
    Code:
    Between Now() And DateAdd("ww",20,Now())
    Last edited by epardo87; 02-10-2017 at 01:15 PM.

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

Similar Threads

  1. Replies: 18
    Last Post: 03-26-2015, 07:26 AM
  2. Retrieve values from a multiselect combobox
    By b82726272 in forum Programming
    Replies: 13
    Last Post: 05-23-2014, 09:41 AM
  3. Replies: 6
    Last Post: 02-25-2014, 12:48 AM
  4. Replies: 2
    Last Post: 05-29-2013, 12:54 PM
  5. Replies: 1
    Last Post: 06-20-2007, 07:26 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