473,383 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

export access into separate excel worksheet by value

Hi all,
Im seeking the web for a long time and found nothing to help my case...
I have 1 table called 'Schollership' with fields like 'Scholler_name','Entitled','info' and more...
I would like to export this table into one spreadsheet excel with separaded worksheets by 'Entitled' field.
for Example:
if the table looks like-
Entitled1,Scholler_name1,info1...
Entitled1,Scholler_name2,info2...
Entitled2,Scholler_name3,info3...
---
the spreadsheet will contain two worksheets: Entitled1,Entitled2 and will have all the fields assosiated with it.

Thanks for any reply...
Aug 1 '11 #1

✓ answered by NeoPa

I will assume [Entitled] is a string field (You don't say in your post). If it's a numeric field then the literal value in the WHERE clause will need to be lose the single-quotes (' - See Quotes (') and Double-Quotes (") - Where and When to use them).

We start off then, with the assumption that you already have a query template (I'll refer to it as [qryTemplate]) that has SQL of :
Expand|Select|Wrap|Line Numbers
  1. SELECT [Entitled]
  2.      , [Scholler_Name]
  3.      , [Info]
  4. FROM   [Schollership]
At this point I'll show the procedure wrapper. The code that surrounds the code that needs to execute (represented here simply as ...). I'll assume you're running this behind a Command Button on a form (called cmdExport) :
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub cmdExport_Click()
  5.     Dim strEnts As String, strSQL As String, strFName As String
  6.     Dim cdb As DAO.Database
  7.     Dim varEnt As Variant
  8.  
  9.     On Error GoTo Error_cmdExport
  10.     '...
  11.     Exit Sub
  12.  
  13. Error_cmdExport:
  14.     Call MsgBox(Prompt:="Error {" & Err & "}" & vbNewLine & Err.Description, _
  15.                 Buttons:=vbOKOnly Or vbExclamation, _
  16.                 Title:="cmdExport")
  17. End Sub
The first step of the process is to initialise any variables that require it :
Expand|Select|Wrap|Line Numbers
  1.     strFName = "C:\Try.xlsx"
  2.     strEnts = ""
The next step is to create a list of the values of [Entitled] from the table. As the length of the list is not known in advance using standard arrays would be cumbersome, so I'll illustrate using a string instead, which is naturally flexible in length (up to 65536 characters approx) :
Expand|Select|Wrap|Line Numbers
  1.     strSQL = "SELECT DISTINCT [Entitled] FROM [Schollership]"
  2.     With cdb.OpenRecordset(Name:=strSQL, Type:=dbOpenSnapshot)
  3.         Call .MoveFirst
  4.         Do While Not .EOF
  5.             strEnts = strEnts & "," & !Entitled
  6.             Call .MoveNext
  7.         Loop
  8.         Call .Close
  9.     End With
  10.     strEnts = Mid(strEnts, 2)
Now we have the list of the existing values for [Entitled] we can create some looping code that will do the work for all of them (This part is pretty straightforward in itself) :
Expand|Select|Wrap|Line Numbers
  1.     For Each varEnt In Split(strEnts, ",")
  2.         ...
  3.     Next varEnt
The payload is in the last piece of code that fits in there in place of the ellipsis (...) :
Expand|Select|Wrap|Line Numbers
  1.         Call DoCmd.CopyObject(NewName:=varEnt, _
  2.                               SourceObjectType:=acQuery, _
  3.                               SourceObjectName:="qryTemplate")
  4.         Set cdb = CurrentDb
  5.         With cdb.QueryDefs(varEnt)
  6.             strSQL = Replace(.SQL, ";", "") & "WHERE [Entitled] = '%E'"
  7.             .SQL = Replace(strSQL, "%E", varEnt)
  8.             Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
  9.                                            TableName:=varEnt, _
  10.                                            Filename:=strFName, _
  11.                                            HasFieldNames:=True)
  12.         End With
  13.         Call DoCmd.DeleteObject(Objecttype:=acQuery, ObjectName:=varEnt)
  14.         Set cdb = Nothing

7 2662
NeoPa
32,556 Expert Mod 16PB
You will need to use DoCmd.TransferSpreadsheet() for each worksheet. The data for each sheet must be that produced by a query of the same name as you require for the sheet, so the query should have a WHERE clause to match the required data. As the queries are all similar in other respects though, it may make sense to use a query template and simply create (and subsequently delete) each query as you need it.
Aug 1 '11 #2
Thank you NeoPa for reply,
I've looked at DoCmd.TransferSpreadsheet() command manual (http://msdn.microsoft.com/en-us/libr...ffice.10).aspx)
but Im little confused how to use it...Im really a novice in all of this...
can you give me an example using the data example I've post in the start of the thread?

assuming I have 20 "Entitled" so I will have to create 20 queries that each one of them will give me one of the Entitled?
and then I'll run:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.TransferSpreadsheet acExport,,[query_name],"C:\try.xlsx", True
thanks,
Aug 1 '11 #3
NeoPa
32,556 Expert Mod 16PB
I will assume [Entitled] is a string field (You don't say in your post). If it's a numeric field then the literal value in the WHERE clause will need to be lose the single-quotes (' - See Quotes (') and Double-Quotes (") - Where and When to use them).

We start off then, with the assumption that you already have a query template (I'll refer to it as [qryTemplate]) that has SQL of :
Expand|Select|Wrap|Line Numbers
  1. SELECT [Entitled]
  2.      , [Scholler_Name]
  3.      , [Info]
  4. FROM   [Schollership]
At this point I'll show the procedure wrapper. The code that surrounds the code that needs to execute (represented here simply as ...). I'll assume you're running this behind a Command Button on a form (called cmdExport) :
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub cmdExport_Click()
  5.     Dim strEnts As String, strSQL As String, strFName As String
  6.     Dim cdb As DAO.Database
  7.     Dim varEnt As Variant
  8.  
  9.     On Error GoTo Error_cmdExport
  10.     '...
  11.     Exit Sub
  12.  
  13. Error_cmdExport:
  14.     Call MsgBox(Prompt:="Error {" & Err & "}" & vbNewLine & Err.Description, _
  15.                 Buttons:=vbOKOnly Or vbExclamation, _
  16.                 Title:="cmdExport")
  17. End Sub
The first step of the process is to initialise any variables that require it :
Expand|Select|Wrap|Line Numbers
  1.     strFName = "C:\Try.xlsx"
  2.     strEnts = ""
The next step is to create a list of the values of [Entitled] from the table. As the length of the list is not known in advance using standard arrays would be cumbersome, so I'll illustrate using a string instead, which is naturally flexible in length (up to 65536 characters approx) :
Expand|Select|Wrap|Line Numbers
  1.     strSQL = "SELECT DISTINCT [Entitled] FROM [Schollership]"
  2.     With cdb.OpenRecordset(Name:=strSQL, Type:=dbOpenSnapshot)
  3.         Call .MoveFirst
  4.         Do While Not .EOF
  5.             strEnts = strEnts & "," & !Entitled
  6.             Call .MoveNext
  7.         Loop
  8.         Call .Close
  9.     End With
  10.     strEnts = Mid(strEnts, 2)
Now we have the list of the existing values for [Entitled] we can create some looping code that will do the work for all of them (This part is pretty straightforward in itself) :
Expand|Select|Wrap|Line Numbers
  1.     For Each varEnt In Split(strEnts, ",")
  2.         ...
  3.     Next varEnt
The payload is in the last piece of code that fits in there in place of the ellipsis (...) :
Expand|Select|Wrap|Line Numbers
  1.         Call DoCmd.CopyObject(NewName:=varEnt, _
  2.                               SourceObjectType:=acQuery, _
  3.                               SourceObjectName:="qryTemplate")
  4.         Set cdb = CurrentDb
  5.         With cdb.QueryDefs(varEnt)
  6.             strSQL = Replace(.SQL, ";", "") & "WHERE [Entitled] = '%E'"
  7.             .SQL = Replace(strSQL, "%E", varEnt)
  8.             Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
  9.                                            TableName:=varEnt, _
  10.                                            Filename:=strFName, _
  11.                                            HasFieldNames:=True)
  12.         End With
  13.         Call DoCmd.DeleteObject(Objecttype:=acQuery, ObjectName:=varEnt)
  14.         Set cdb = Nothing
Aug 1 '11 #4
NeoPa
32,556 Expert Mod 16PB
While testing the code outlined in the previous post (#4) I discovered, to my considerable surprise, that the cdb object that had previously been set to CurrentDb (The original version of the post had cdb set once only at the initialisation stage), was not able to reference the newly created queries in the database. To get around this I had to ensure cdb was set every time through the loop after an object had been created. CurrentDb() called after the creation of the object (and therefore cdb set afterwards) was able to reference the new object.

I developed and tested this in Access 2003, so I cannot confirm everything will be perfectly smooth-running in later versions, but I see no reason to assume otherwise.
Aug 1 '11 #5
thank you,I will check it right away...I'll post the results
Aug 2 '11 #6
Thank you!!
it works!
I did some minor adjustments but nothing serious.
it works as a charm..thank you again...
Aug 2 '11 #7
NeoPa
32,556 Expert Mod 16PB
Pleased to hear it Uriya :-)
Aug 2 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: sridevi | last post by:
Hello How to export data from ms-access database to excel worksheet using ASP. mainly i need to export data to multiple worksheets. it is very urgent to us. i have a sample code which works...
2
by: Anne Sachleben via AccessMonster.com | last post by:
I am using the TransferSpreadsheet function to export a query result to a specific worksheet in an Excel file titled "report". I want the result to be exported to the worksheet titled "facts". ...
4
by: Hans [DiaGraphIT] | last post by:
Hi! I want to export a dataset to an excel file. I found following code on the net... ( http://www.codeproject.com/csharp/Export.asp ) Excel.ApplicationClass excel = new ApplicationClass();...
6
by: Elena | last post by:
I'm trying to export data to an Excel worksheet. I can export the data in the cell values perfectly. I need the code to change a header and footer for the worksheet, not for the columns. Is...
4
by: paul.chae | last post by:
I have a table in Access with about 3000 records. There are ~60 unique values in the ID field for the 3000 records. What I would like to do is automatically generate multiple Excel worksheets...
5
by: hmiller | last post by:
Hey there folks: I have been trying to get this work for about a week now. I'm new to VBA... I am trying to transfer a populated table in Access to an existing, but blank, Excel worksheet. I...
1
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm...
0
by: blainegray | last post by:
Greetings This is one of those Access is not closing Excel problems. The first time through the code works fine. The second time there is a problem. After lots of combinations, I finally...
1
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
1
by: accessvbanewbie | last post by:
I would like to export a recordset from access to excel but after each record is exported I want to insert a new row. The first recordset does this ok. However, the second recordset onwards does not...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.