DoCmd.TransferSpreadsheet

  • Thread starter Thread starter corkster
  • Start date Start date
C

corkster

I am working on a project that requires approx 600 queries to be
exported into Excel.

I have coded it using the DoCmd.TransferSpreadsheet command. However,
there are 600 queries that will be exported into approx 50 workbooks.
Would the DoCmd.TransferSpreadsheet command be my best option?

Also, would building 600 queries be my best option on getting the data
and exporting to Excel? Or would there be a better option coding in
VBA Form?

Couple more needs. Within these queries there are 5 columns (data
input by users) that need to total 100 percent. The users are going to
enter the data into the Excel spreadsheets we sent out and filling out
col 1, col 2, col 3, col 4, col 5. Is there away to code within Access
to send code into Excel if these columns don't total 100 then error
message?

Any ideas and help would be greatly appreciated

Corkster
 
A few questions:

Why are there 600? Is that because there are 600 different columns or
because of selection criteria?

As to the 100% criteria, That criteria will need to be in the
spreadshet itself. It can end up being there either by creating a base
spreadsheet you export into or by adding the criteria via excel opend
from within Access after the data has been exported.

Basically, we need a little more information.
 
Sorry I wasn't clear. Yes there would be 600 queries because there are
600 employees and each query would pull data for each employee placing
the condition on each query is specific to employee id.

What I would ultimately like is to query the data from the database
that would dynamically assign employee id for each employee because
employees could change. Thus I wouldn't have to change the coding of
the form that exports the information, nor would I have to set up 600
queries plus manually add new queries as new employees come into
database. Any coding information on being able to do this within a
form would be great.

Currently I have a table with all the data, a table with employee data,
and a table with coordinator data (which is the 50 workbooks that
muliple employees are assigned to).

If you need more information please let me know.

What is your comment ("or by adding the criteria via excel opend
from within Access after the data has been exported. ") Process entail
I am not familiar with that?

Thanks
Corkster
 
Hi Corkster,

I'd probably do this by writing code that creates and executes 600 SQL
statements, each doing one of the things you want. Pseudo code:

Dim dbD As DAO.Database
Dim BookName As String 'Filespec of workbook to create
Dim SheetName As String 'Name of worksheet
Dim strSQL As String

Set dbD = CurrentDB()

'Outer loop, once for each of c.50 workbooks
Bookname = blah blah
'Inner loop, once for each sheet in each workbook
Sheetname = blah blah

'At this point, use Bookname and Sheetname to create the
'SQL statement for a Make Table query, and assign it to strSQL.
'Presumably you need a different WHERE clause for each query.
'The syntax to write directly to a workbook is like this:
strSQL = "SELECT * INTO " _
& "[Excel 8.0;HDR=Yes;" & BookName & ";].[" & SheetName " _
& "] FROM XXX WHERE blah blah blah;"

dbD.Execute strSQL, dbFailOnError
'End inner loop

'End outer loop

XXX in the SQL statement can of course be either a table or a (stored)
query.
 
Hi again,

If you've tried the suggestion in my previous post you may have found it
a bum steer. I'd forgotten that recent updates for Access 2002 and 2003
have removed the ability to write to spreadsheets via queries as shown.
(If you're using earlier versions you're OK.)

The general idea stays the same, with an inner and outer loop. The
difference is that instead of directly creating and executing a SQL
statement, one has to manipulate the SQL statement contained in a query
and then export the query. Something like this:

1) Create a query in the usual way. Make it a select query, but it
doesn't matter what fields it gets from what table: we're going to
control that from our code. Let's call it qryTempExport.

Dim BookName As String
Dim SheetName As String
Dim strSQL As String
Dim strWhere As String
Dim qdfTempExport As DAO.QueryDef

Const QRY_EXPORT = "qryTempExport"

'A string constant containing the basic SQL statement
'for the 600 queries we'll construct and export. The
'WHERE clause will be constructed in code each time.
Const BASE_SQL = "SELECT * FROM MyQuery WHERE "

Set qdfTempExport = CurrentDB.QueryDefs(QRY_EXPORT)

'Outer loop, once for each of c.50 workbooks
Bookname = blah blah

'Inner loop, once for each sheet in each workbook

Sheetname = blah blah

'Construct the meat of the WHERE clause that
'selects the records for this sheet of this workbook.
strWhere = blah blah

'Set the new SQL statement for the query
qdfTempExport.SQL = BASE_SQL & strWhere

'export it
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
QRY_EXPORT, BookName, True, Sheetname

'End inner loop

'End outer loop







Hi Corkster,

I'd probably do this by writing code that creates and executes 600 SQL
statements, each doing one of the things you want. Pseudo code:

Dim dbD As DAO.Database
Dim BookName As String 'Filespec of workbook to create
Dim SheetName As String 'Name of worksheet
Dim strSQL As String

Set dbD = CurrentDB()

'Outer loop, once for each of c.50 workbooks
Bookname = blah blah
'Inner loop, once for each sheet in each workbook
Sheetname = blah blah

'At this point, use Bookname and Sheetname to create the
'SQL statement for a Make Table query, and assign it to strSQL.
'Presumably you need a different WHERE clause for each query.
'The syntax to write directly to a workbook is like this:
strSQL = "SELECT * INTO " _
& "[Excel 8.0;HDR=Yes;" & BookName & ";].[" & SheetName " _
& "] FROM XXX WHERE blah blah blah;"

dbD.Execute strSQL, dbFailOnError
'End inner loop

'End outer loop

XXX in the SQL statement can of course be either a table or a (stored)
query.


I am working on a project that requires approx 600 queries to be
exported into Excel.

I have coded it using the DoCmd.TransferSpreadsheet command. However,
there are 600 queries that will be exported into approx 50 workbooks.
Would the DoCmd.TransferSpreadsheet command be my best option?

Also, would building 600 queries be my best option on getting the data
and exporting to Excel? Or would there be a better option coding in
VBA Form?

Couple more needs. Within these queries there are 5 columns (data
input by users) that need to total 100 percent. The users are going to
enter the data into the Excel spreadsheets we sent out and filling out
col 1, col 2, col 3, col 4, col 5. Is there away to code within Access
to send code into Excel if these columns don't total 100 then error
message?

Any ideas and help would be greatly appreciated

Corkster
 
Corkster,
What John is telling you is what you need to do. I guess my question
is, are you comfortable with writing out the sql to do it?

What you want to do is develop it in a way (and it sounds as if you may
have the data to do it) in a manner so that you don't have to change a
thing, when they add or delete an employee, or an employee changes from
one supervisor to another.

Has John's answer helped?

Ron
 
You are correct I would prefer that I don't have to continually change
code and queries when employees change. I assumed I would have to do a
loop (which I am not entirely familiar with using) but will work
through his thought process and get back to y'all. As far as writing
the SQL I have some ability in writing SQL in Oracle and Sybase.
Usually in Access I use the GUI to establish my queries.

Thanks
Corkster
 
Sorry, but I am not that advanced on my Access and VBA. I didn't find
any reference in Help menu on Inner and outer loop. As I said I am not
familiar with loops. Also I am not following how this will dynamically
pull each of the supervisors and employees, bookname =????, Sheetname
=?? and strWhere =???

sorry guys.

I really need some examples to follow this through.

I appreciate your efforts. I am sure you don't have the time to train
someone thanks anyway.
 
Here is an example that may give you the idea that you need:

1) create a query that groups by supervisorName and by employeeName


Set Mrecs = CurrentDb.OpenRecordset("Query - Statistics by
Supervisor")
If Mrecs.RecordCount > 0 Then
With Mrecs
.MoveFirst
Do While Not .EOF
reportname = "directory info" & ![supervisorName] &
" " & date()
Forms![HiddenKey]![HKSelectEmp] = ![employeename]
' see note 1 below
DoCmd.RunMacro "create the employee table"

DoCmd.TransferSpreadsheet _
TransferType:=acExport, _
SpreadsheetType:=acSpreadsheetTypeExcel9, _
TableName:="employeeTable", _
FileName:=reportname, _
HasFieldNames:=True, _
Range:="Export" & ![EmployeeName]
End If

.MoveNext
Loop
.Close
End With
End If


Note 1
Create a query that will empty a temporary employee export table
Create a query (or sqltext) that will select etc all the information
you need but the where uses the fForms![HiddenKey]![HKSelectEmp] as
criteria and append all needed information into that table.
This will be the table that you will be exporting.

This can be done as a macro or as three different queries that you
execute from this code.



The end result of going through this is that there will be a dated
workbook (file) for every supervisor and within that workbook a
different tab for every employee. The rest is up to you

With that type of logic you will never have to change code for new
employees and/or new supervisors. I hope this gives you the idea that
we were talking about.

Hope this gives you some ideas.

Ron
 
In general the loop within a loop could be looked as the following (Its
purpose was to format an email with multiple pieces of information but
one per a higher level of summary):

bounce through the first query formating part of the email
setup the second query constrained by key from first query
now bounce through second query adding all of the
pertinent info to the body of the
email body = body + newdata (with carrage returns)
at eof
send the email
loop until eof
at eof
end the routine.
 
Ron

Yes this gives me some ideas. However it gives me some more questions
(sorry, please bear with me)

In your Note 1
Create a query that will empty a temporary employee export table...
what exactly does this mean?
Create a query (or sqltext) that will select all information but the
where uses the Forms![HiddenKey]![HKSelectEmp] as criteria and append
all needed information into that table. Do I create a text query that
states where Form![HiddenKey]![HKSelectEmp]?

Example Select * From Employee where Form![HiddenKey]![HKSelectEmp].

Docmd.RunMacro is this used to create a temporary employee table? Do I
copy the Employee information table into this new table?

reportname="directory info" &![supervisorName] & " " date() What
exactly does this do?

Again I apologize for my limited knowledge I can kind of follow this
but am not familiar with the all and some of the terms.
If this is taking up too much of your time let me know. If you have
some links to help me on this that would be great too.
Thanks
C
 
1) Create a query that will empty a temporary employee export table...
what exactly does this mean?
example: Delete * from Employeetemptable

2) Do I create a text query that
states where Form![HiddenKey]![HKSelectEmp]?

Yes...

3) The runmacro is an alternative. If you had a macro that executed
those two queries (the delete query and the apend query) you could run
the macro OR you can execute them right there in your code.

example sqltxt = "Delete * from Employeetemptable"
docmd.runsql sqltxt
sqltxt = "kkkjkj where
[realemptable]![empname] = " & Form![HiddenKey]![HKSelectEmp
docmd.runsql sqltxt

4) The reportname has to be the complete mapped address of where the
file is to be created.
example reportname = "C:\My Documents\My Spreadsheets\" &
![supervisorName] & " " & Date()
If the supervisor name was Joe Small then the above would result in a
reportname of

"C:\My Documents\My Spreadsheets\Joe
Small 04 18 2006.xls"


Hope this helps
 
Ron Thanks for this I will work through it and let you know how
successful I was :)

Quick question is your code in addition to John's previous reply? or is
your code providing me all that will be needed for the form?

Thanks
corkster
 
I believe it is probably all you need to set up the export. I think
John and I are saying the same thing just going about implementing it
is slightly different ways. (and his has more comments in it.)
 
Sorry I was side tracked on another project.

When I build a query using SQL txt this is exact sql code
SELECT empl.[employee name]
FROM empl
WHERE [FORM]![HiddenKey]![HKSelectempl];
if I run this query it requests a paramter to be put in if I select 1
it returns data if I select 0 or nothing it returns no data.

What I am confused on is this Form!HiddenKey!HKSelectempl
What does this do? and do I type in the exact syntax
Form!HiddenKey!HKSelect or do I use form name it relates to or any of
the fields it will relate to?

Here is an overly simplified code for a data base that has 1 table
called empl
one query called Query1 and
one form called Form1 the empl table only has one field called
Employee Name
I would like for the following code to export 1 query for each of the
Employee Names into separate Excel Workbooks for each employee name,
Will it Work? I am sure I am missing a lot here.

Set Mrecs = CurrentDb.OpenRecordset("Query1")
If Mrecs.RecordCount > 0 Then
With Mrecs
..MoveFirst
Do While Not .EOF
reportname = "C:\" & ![empl] & " "
Form![Form1]![SelectEmpl] = ![empl]
DoCmd.CopyObject , "empltemp", acTable, "empl"
sqltxt = "Delete * from empltemp"
DoCmd.RunSQL sqltxt
sqltxt = "Query1 Where [empl]![Employee Name]=" &
Form![Form1]![SelectEmpl]
DoCmd.RunSQL sqltxt
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8,
![empl], "reportname", False





..MoveNext
Loop
..Close
End With
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top