Results 1 to 8 of 8
  1. #1
    Robyn_P's Avatar
    Robyn_P is offline Competent Performer
    Windows 8 Access 2010 32bit
    Join Date
    Nov 2017
    Posts
    150

    Restrict report to first x records where x is user specified

    Hey, I'm looking for a way to restrict the report to just show the first x records where x is a number defined by the user. I can do this by getting the recordset and looping through it getting the ID of each record and building a WHERE statement that way, but this seems inefficient. Can anyone suggest another way?

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I have user select beginning and ending values and use BETWEEN AND for filtering on the range. Or you can calculate the ending value from the given beginning.

    You could consider code that sets the report RecordSource with a SELECT TOP N sql statement but this gets rather complicated. Unfortunately the N value for a TOP N query cannot be a dynamic parameter and that's why code would have to set 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.

  3. #3
    Robyn_P's Avatar
    Robyn_P is offline Competent Performer
    Windows 8 Access 2010 32bit
    Join Date
    Nov 2017
    Posts
    150
    Ooh BETWEEN could work. I looked at the SELET TOP N but as you said, it looked complicated. Will try you're BETWEEN solution and report back. Thanks

  4. #4
    Robyn_P's Avatar
    Robyn_P is offline Competent Performer
    Windows 8 Access 2010 32bit
    Join Date
    Nov 2017
    Posts
    150
    Beautiful. I did this:
    ' Build strWhere for the most recent x records
    Set OIs = CurrentDb.OpenRecordset("SELECT * FROM tblOpenItems ORDER BY [ID] DESC")

    ' Get the first ID
    OIs.MoveFirst
    ID1 = OIs("ID")

    ' Move to the nth record and get the ID
    OIs.Move Me.txtQuantity - 1
    ID2 = OIs("ID")

    ' Build the strWhere
    strWhere = "[ID] BETWEEN " & ID1 & " AND " & ID2
    Last edited by Robyn_P; 03-27-2018 at 04:19 AM. Reason: Formatting

  5. #5
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    For future reference, if it was an action query such as a make table query, you could do something like this

    Code:
    Dim N= Integer
    
    'N = some value set in code or on form control
    
    CurrentDB.Execute "SELECT TOP " & N & " .... INTO .... FROM ... WHERE ...."
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  6. #6
    Robyn_P's Avatar
    Robyn_P is offline Competent Performer
    Windows 8 Access 2010 32bit
    Join Date
    Nov 2017
    Posts
    150
    I thought about SELECT TOP N but I wasn't sure how I would then use that when opening the report. How would you then go on to open the report?

  7. #7
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    Quote Originally Posted by Robyn_P View Post
    I thought about SELECT TOP N but I wasn't sure how I would then use that when opening the report. How would you then go on to open the report?
    First of all it would be "SELECT TOP " & N & " ....

    I specifically said this approach was for action queries

    I've not tested the following and it may well not work for TOP N or give an error
    However I use something similar for other reports where the record source may vary e.g. based on crosstab query

    a) Set the value of N in a textbox in the calling form e.g. N=5

    Then set the record source for the report

    Code:
    strSQL = "SELECT TOP " & N & " ...."
    DoCmd.OpenReport "MyReportName", avViewPreview

    In your report, add this code

    Code:
    Private Sub Report_Open()    
    Me.RecordSource = strSQL
    End Sub


    By all means try it but don't be surprised if it errors
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  8. #8
    Robyn_P's Avatar
    Robyn_P is offline Competent Performer
    Windows 8 Access 2010 32bit
    Join Date
    Nov 2017
    Posts
    150
    Thanks for the explanation. I'll stick with the solution I've used above. The calling form has lots of different options for filtering the report so it's simpler just to build the strWhere.

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

Similar Threads

  1. Replies: 1
    Last Post: 02-16-2018, 03:38 AM
  2. Replies: 27
    Last Post: 11-18-2015, 12:39 PM
  3. Replies: 2
    Last Post: 01-16-2015, 06:43 PM
  4. Replies: 3
    Last Post: 03-11-2013, 05:11 PM
  5. Restrict user to navigation form
    By Whelt in forum Access
    Replies: 3
    Last Post: 04-22-2012, 06:19 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