Just a correction for the last line.
Public Function ExportSheets() As Boolean
Dim db As Database
Dim sSQL As String
Dim rs As Recordset
Dim qdf As QueryDef
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT DISTINCT ProjectID FROM Projects;")
Do Until rs.EOF
Set qdf = db.CreateQueryDef()
sSQL = "SELECT Projects.* From Projects WHERE Projects.ProjectID=" &
rs("ProjectID") & ";"
qdf.SQL = sSQL
qdf.Name = "ProjectID" & rs("ProjectID")
db.QueryDefs.Append qdf
Application.RefreshDatabaseWindow
DoCmd.TransferSpreadsheet acExport, , qdf.Name, "H:\TestExport.xls"
db.QueryDefs.Delete qdf.Name
Application.RefreshDatabaseWindow
Set qdf = Nothing
rs.MoveNext
Loop
ExportSheets = True
Set rs = Nothing
Set db = Nothing
End Function
--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com
This response is supplied "as is" without any representations or warranties.
David:
Below is some code that loops through a set of ProjectIDs and exports to
separate worksheets for each ProjectID. Obviously, you will need to adjust
it to your needs. One note, if the export workbook is open when you try
this you will get an error. I have not trapped for this outcome. Again,
you will have to adjust this to your requirements. This is just one
alternative way to handle something like this.
Public Function ExportSheets() As Boolean
Dim db As Database
Dim sSQL As String
Dim rs As Recordset
Dim qdf As QueryDef
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT DISTINCT ProjectID FROM Projects;")
Do Until rs.EOF
Set qdf = db.CreateQueryDef()
sSQL = "SELECT Projects.* From Projects WHERE Projects.ProjectID=" &
rs("ProjectID") & ";"
qdf.SQL = sSQL
qdf.Name = "ProjectID" & rs("ProjectID")
db.QueryDefs.Append qdf
Application.RefreshDatabaseWindow
DoCmd.TransferSpreadsheet acExport, , qdf.Name, "H:\TestExport.xls"
db.QueryDefs.Delete qdf.Name
Application.RefreshDatabaseWindow
Set qdf = Nothing
rs.MoveNext
Loop
ExportSheets = True
Set rs = Nothing
Set db = Nothing
End Sub
--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com
This response is supplied "as is" without any representations or warranties.
This is a VBA question.
Currently, I have a query/recordset that is being exported to Excel, but on
one ("Master") worksheet. However, I would like the records to be organized
/
split across several different worksheets according to a number assigned to
each individual record ("ProjectID"); these worksheets would be added for
each unique ProjectID. Right now, I'm attempting to embellish a generic "For
Each" statement, but with no luck thus far. Any ideas?
Thanks,
David