Results 1 to 8 of 8
  1. #1
    Nico80000 is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2021
    Posts
    7

    Question Top n Query- Allow n to act as criteria where the user can enter a number each time the query is run

    I understand how to perform a "Top" query in Access by using the properties pane.


    My question is if there is a different way of doing this where the top n records wasn't a static number but instead, it could be entered in a pop-up box in the same manner as a parameter.

    I would love to have the ability to re-run the same Top query, which could be tied to a report, where I would not have to go into the back-end of the query, and manually change the properties pane each time I need a different amount of Top n records to populate my report.

    Any suggestions are welcome, and if this is a functionality that simply doesn't exist in Access, please let me know.
    My thought is that maybe this could be achieved through a macro, but I'm not very well-versed in macros so I could be completely wrong.

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,925
    Yes, you would just concatenate the top value into the rest of the sql.
    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

  3. #3
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,799
    Yes, if you think you'll never need to add/exclude a query field, simply get n from a form field or input box and run the sql in code.

    If you might alter query design in the future it would be more complicated but still doable. The advantage would be that if you alter the query, you won't have to find and edit your code as well.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    Nico80000 is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2021
    Posts
    7
    Could you provide an example of what the SQL code would look like?

  5. #5
    Nico80000 is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2021
    Posts
    7
    @Welshgasman Could you provide an example of what the SQL code would look like?

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,925
    Code:
    strSQL = "SELECT TOP " & YourInputNumber & " TableName.* FROM TableName
    Debug.Print strSQL
    Then use the strSQL as needed. The Debug is to make sure your/my syntax is correct.

    YourInputNumber can come from an INPUT function or better still a form control.
    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
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725

  8. #8
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,799
    I presume this is all about a SELECT sql statement and that matters since you cannot simply run one in code. You'd have to assign it to a query def, form, report or recordset so what the code looks like highly depends on all of that. If it is a SELECT statement, something like the following 'air code'
    Code:
    Private Sub SomeButton_Click()
    Dim strSql As String
    Dim n As Integer
    
    strSql = "SELECT TOP x FROM..." '< use exactly x here
    n = Inputbox "Enter value for top number of records.", "TOP VALUE FOR QUERY"
    If n > 0 Then 
      Replace(strSql,"x",n)
    Else
      Msgbox "Value must be greater than zero and not null"
      Exit Sub
    End If
    'now do something with the resulting sql as noted above
    
    End Sub
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Query Prompting User to Enter Date Range
    By C Muth in forum Queries
    Replies: 4
    Last Post: 11-21-2019, 04:33 AM
  2. Replies: 6
    Last Post: 11-08-2016, 07:49 AM
  3. Replies: 5
    Last Post: 05-08-2012, 01:26 PM
  4. Replies: 1
    Last Post: 01-27-2012, 02:03 PM
  5. Replies: 2
    Last Post: 03-02-2010, 01:58 AM

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