divide a table into equal groups, using a calculated field based on total records

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #16
    The reason we have to "quibble" about words is because that's how we understand the requirements of the question. When communication is unclear, it's impossible to formulate a solution. We need to understand and agree on the meaning before an accurate solution can be provided.

    From what I can gather, you're not looking to return a random 10% or even 1/10th of the records each time the query is run. What you're looking for is to section all the records in the table so you can grab the same size grouping each time you run the query until you have processed all the records.

    This is very different from a query that returns 10% or 1/10 of the records.

    I suspect gnawoncents' code will get you most of the way to your actual requirements.

    Comment

    • gnawoncents
      New Member
      • May 2010
      • 214

      #17
      Angela,
      We understand your concern. In your first iteration, 10% would be fine. The only problem is if you then took 10% of the remaining 90%, you're actually only getting 9% of the total records for the second iteration. In that sense, yes, you need the original number saved somewhere. Or, the code I suggested earlier could get you there all at once and be ready for future use. If, however, you need it saved in a separate query, but automatically calculated the first time, you would need to use VBA to adjust the QueryDef.

      Something like this should work:
      Code:
      Dim db10 As Database
      Dim qdf10 As QueryDef
      Dim int10 As Integer
      Dim strSQL As String
      
      Set db10 = CurrentDb()
      
      'Determine 10% of total records in query (RcdNms)
      int10 = DCount("*", "RcdNms")
      
      'Update query (View10%) to reflect 10% of total records
      Set qdf10 = db10.QueryDefs("View10%")
          strSQL = "SELECT DISTINCTROW TOP " & int10 & " * " & _
                   "FROM Sheet1;"
          qdf10.SQL = strSQL
      
      Set db10 = Nothing
      This isn't necessarily how I would go about setting it up, but you know your database and requirements better than I do. It should work based on what information you have provided and the fix you requested.

      Comment

      Working...