Query a specific number of records

BryGy

New Member
Joined
Jun 7, 2004
Messages
9
I have a table with 100K +/- records in it. I need to be able to dump the data into Excel for some massaging. How can I query records 1 thru 50,000 and then records 50,001 thru 100,000 for example.

Thanks
Bryan
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Access does not "number" records. At least, you can't rely on the first 50,000 being the same each time you run a query, *unless* you sort them. You can add a unique incremented key, such as autonumber to help you identify them.

There's a couple approaches.

The first that sounds the easiest would be to use the TOP modifier
(SELECT TOP 50000 fld1, fld2, fld3 FROM tblName ORDER BY fld1, fld2) and then export the query into Excel. And then repeat in some fashion that excludes the first batch (or 2nd/3rd if necessary)

With a unique incremented key (add Autonumber field and it fills them in) you can show a parameter.

SELECT fld1, fld2, fld3 FROM tblName WHERE fld1<50001


The better way is to export recordsets that you figure out via VBA code.
Take a look at this:

http://www.mvps.org/access/modules/mdl0035.htm

I'd recommend the third function.
sCopyRSToNamedRange

With a modification.

Mike

Code:
Sub sCopyRSToNamedRange()
'Copy records to a named range
'on an existing worksheet on a
'workbook
'
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim db As Database
Dim rs As Recordset
Const conMAX_ROWS = 20000
Const conSHT_NAME = "SomeSheet"
Const conWKB_NAME = "c:\temp\book1.xls"
Const conRANGE = "RangeForRS"

Dim strSQL As String

  Set db = CurrentDb
  Set objXL = New Excel.Application

  strSQL = "your_SQL_query_here"

  Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
  With objXL
    .Visible = True
    Set objWkb = .Workbooks.Open(conWKB_NAME)
    On Error Resume Next
    Set objSht = objWkb.Worksheets(conSHT_NAME)
    If Not Err.Number = 0 Then
      Set objSht = objWkb.Worksheets.Add
      objSht.Name = conSHT_NAME
    End If
    Err.Clear
    On Error GoTo 0
    objSht.Range(conRANGE).CopyFromRecordset rs
  End With
  Set objSht = Nothing
  Set objWkb = Nothing
  Set objXL = Nothing
  Set rs = Nothing
  Set db = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,704
Messages
6,161,390
Members
451,701
Latest member
ckrings

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top