A family of Microsoft relational database management systems designed for ease of use.
Billy
Presently, you are only creating one worksheet. I'm not much of an Excel automation guy so I'm not certain of what you need to do, but if you want a separate sheet for each SRO Control #, you will have to do it within the loop, not outside of it.
And this line:
ws.Range("A1").CopyFromRecordset
is reading the entire recordset into the sheet. YOu only want to read one line at a time so you can check the SRO Control #. Therefore, you have to limit the recordset to one SRO Control #.
Here is a samplpe using Northwind. I purposely used TOP 10 just to limit the test. You would want to get all the SRO Controls so don't use that.
Public Sub FillWrksht()
Dim db As Database
Dim rs As Recordset
Dim rsCustomerIDs As Recordset
Dim objExcel As New Excel.Application
Dim wb As Workbook
Dim ws As Worksheet
Dim strSRONumber As String
Dim x As Integer
Set db = CurrentDb
Set rsCustomerIDs = db.OpenRecordset("SELECT DISTINCT TOP 10 CustomerID FROM Orders ORDER BY CustomerID")
Set wb = objExcel.Workbooks.Add("C:\Temp_C\Test.xltx")
Set ws = wb.Worksheets("Data")
ws.Activate
With rsCustomerIDs
.MoveFirst
strSRONumber = !CustomerID
Do Until .EOF
If strSRONumber <> !CustomerID Then GoTo LoopMe
Set rs = db.OpenRecordset("SELECT CustomerID, OrderID, OrderDate " _
& "FROM Orders WHERE CustomerID = '" & rsCustomerIDs!CustomerID & "'")
ws.Range("A1").CopyFromRecordset rs
LoopMe:
.MoveNext
If Not .EOF Then
'Increment counter for sheet name
x = x + 1
'Add a new worksheet
Set ws = wb.Worksheets.Add
ws.name = "Data" & x
ws.Activate
strSRONumber = !CustomerID
End If
Loop
End With
Set rs = Nothing
Set rsCustomerIDs = Nothing
Set db = Nothing
Set ws = Nothing
wb.SaveAs "C:\Temp_C\Test_" & Format(Now(), "yyyymmddhhnnss") & ".xlsx"
wb.Close
Set wb = Nothing
objExcel.Quit
Set objExcel = Nothing
End Sub
Bill Mosca www.ThatllDoIT.com