Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Importing / Linking / December 2004

Tip: Looking for answers? Try searching our database.

Export question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Eric @ SEASH, Evansville - 31 Dec 2004 20:15 GMT
I have a query I need to export into Excel, but due to limitations in the
system where the data from Excel will be pasted into, I need a new tab in the
spreadsheet (or a new spreadsheet) for each 50 records.  Is there some way of
accomplishing that using a macro or code ?  Any ideas ?

Thanks !
John Nurick - 31 Dec 2004 22:38 GMT
Hi Eric,

There are several ways of doing it. One is to modify your query to
generate sequential record numbers and use this as the core of a
make-table query that creates an Excel worksheet. Then execute this once
for each 50 records, using appropriate record numbers and sheet names.

Here's a query that does this, from my test database. PK is the primary
key of the underlying table (tblT - replace this with the name of your
query), SEQ is the calculated record number field (doing this means that
it doesn't matter if the primary key is non-sequential or non-numeric).

SELECT PK, ITEM, DESCRIPTION, AMOUNT
INTO [Excel 8.0;HDR=Yes;Database=C:\Temp\XXX.xls;].[Sheet2]
   FROM
   (SELECT
       (SELECT COUNT(*) FROM tblT AS C
          WHERE C.PK <= T.PKD) AS SEQ,
       ID, CUST, ITEM, DESCRIPTION, AMOUNT
       FROM tblT AS T)
   WHERE (SEQ>=11) AND (SEQ<=20)
ORDER BY PK;

The VBA code would be something like this air code:

Dim strSQL
Dim strFileSpec As String
Dim strSheetBaseName As String
Dim lngSheetNumber As Long
Dim lngRecCount As Long
Dim lngFirstRec As Long

'Elements of SQL string for query
'replace ... with the actual SQL stuff needed
Const SQL1 = "SELECT ... Database="
Const SQL2 = "] FROM ... WHERE (SEQ>="
Const SQL3 = ")ORDER BY PK;"

Const CHUNKSIZE As Long = 50

strFileSpec = "C:\Folder\Filename.xls"
strSheetbaseName = "Sheet"
lngSheetNumber = 1

'Get number of records to export
lngRecCount = DCount("*","MyQuery")
lngSheetNumber = 1
lngFirstRec = 1

Do
 'Assemble the SQL string for the query
 strSQL = SQL1 & strFileSpec & ";].[" _
   & strSheetBaseName & Format(lngSheetNumber, "000") _
   & SQL2 & Cstr(lngFirstRec) & ") AND (SEQ<=" _
   & Cstr(lngFirstRec + CHUNKSIZE) & SQL3
 'execute it
 DBEngine(0).(0).Execute strSQL
 lngSheetNumber = lngSheetNumber + 1
 lngFirstRec = lngFirstRec + CHUNKSIZE
Loop Until lngFirstRec > lngRecCount



>I have a query I need to export into Excel, but due to limitations in the
>system where the data from Excel will be pasted into, I need a new tab in the
>spreadsheet (or a new spreadsheet) for each 50 records.  Is there some way of
>accomplishing that using a macro or code ?  Any ideas ?
>
>Thanks !

--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.