473,396 Members | 1,895 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,396 software developers and data experts.

Changing the Paste Orientation of a Transfer Spreadsheet

doma23
107 100+
Hi, I'm copy pasting the question that I've found on the net while I was trying to find the answer on the same problem, but didn't manage:

I have the followig TransferSpreadsheet Statement that moves the results of a query to an Excel Workbook which works fine. Problem is there are a lot of fields in the query and it is diffficult for people to read that far across. Is there a way to modify this so that it exports the data vertically say a1:b whatever instead of a1: EN2
Sort of a Paste Special transpose.

Thanks!

Here is the code:

Expand|Select|Wrap|Line Numbers
  1. DoCmd.TransferSpreadsheet acExport, 8, "A_EXPORT", str, True, ""
Jun 8 '10 #1
10 2913
doma23
107 100+
Ok, either nobody understand the question or there is no answer (more likely). I was hoping that there are some other solutions that may do the job.

Ok, just to be sure, I'll try to be more clear:

Is it possible to write code in Access that will
execute the query, export it to Excel, open the Excel file, then perform a copy/cut and paste with the transpose option?
Jun 9 '10 #2
doma23
107 100+
OK. I have found the solution and I would like to share it just in case somebody runs on the same problem:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdExportXLS_Click()
  2.  
  3. Const EXPORT_DATA_PATH = "C:\AccessProject\Output\Data.xls"
  4.  
  5. Call Transposer("query-name", "table-name")
  6. DoCmd.TransferSpreadsheet acExport, , "table-name", EXPORT_DATA_PATH, False, "worksheet-name"
  7. DoCmd.DeleteObject acTable, "table-name"
  8.  
  9. End Sub
Transposer is a function that takes one table (or a query) and then makes another table and transpose all the data from the previous table into new one.
I've found it on the Microsoft website.

TRANSPOSER FUNCTION:
Expand|Select|Wrap|Line Numbers
  1. Public Function Transposer(strSource As String, strTarget As String)
  2.  
  3.    Dim db As DAO.Database
  4.    Dim tdfNewDef As DAO.TableDef
  5.    Dim fldNewField As DAO.Field
  6.    Dim rstSource As DAO.Recordset, rstTarget As DAO.Recordset
  7.    Dim i As Integer, j As Integer
  8.  
  9.    On Error GoTo Transposer_Err
  10.  
  11.    Set db = CurrentDb()
  12.    Set rstSource = db.OpenRecordset(strSource)
  13.    rstSource.MoveLast
  14.  
  15.    ' Create a new table to hold the transposed data.
  16.    ' Create a field for each record in the original table.
  17.    Set tdfNewDef = db.CreateTableDef(strTarget)
  18.    For i = 0 To rstSource.RecordCount
  19.       Set fldNewField = tdfNewDef.CreateField(CStr(i + 1), dbText)
  20.       tdfNewDef.Fields.Append fldNewField
  21.    Next i
  22.    db.TableDefs.Append tdfNewDef
  23.  
  24.    ' Open the new table and fill the first field with
  25.    ' field names from the original table.
  26.    Set rstTarget = db.OpenRecordset(strTarget)
  27.    For i = 0 To rstSource.Fields.Count - 1
  28.       With rstTarget
  29.         .AddNew
  30.         .Fields(0) = rstSource.Fields(i).Name
  31.         .Update
  32.       End With
  33.    Next i
  34.  
  35.    rstSource.MoveFirst
  36.    rstTarget.MoveFirst
  37.    ' Fill each column of the new table
  38.    ' with a record from the original table.
  39.    For j = 0 To rstSource.Fields.Count - 1
  40.       ' Begin with the second field, because the first field
  41.       ' already contains the field names.
  42.       For i = 1 To rstTarget.Fields.Count - 1
  43.          With rstTarget
  44.             .Edit
  45.             .Fields(i) = rstSource.Fields(j)
  46.             rstSource.MoveNext
  47.             .Update
  48.          End With
  49.  
  50.       Next i
  51.       rstSource.MoveFirst
  52.       rstTarget.MoveNext
  53.    Next j
  54.  
  55.    db.Close
  56.  
  57.    Exit Function
  58.  
  59. Transposer_Err:
  60.  
  61.    Select Case Err
  62.       Case 3010
  63.          MsgBox "The table " & strTarget & " already exists."
  64.       Case 3078
  65.          MsgBox "The table " & strSource & " doesn't exist."
  66.       Case Else
  67.          MsgBox CStr(Err) & " " & Err.Description
  68.    End Select
  69.  
  70.    Exit Function
  71.  
  72. End Function
Jun 9 '10 #3
doma23, Good soulotion

i will try it...

Thank You..

(Medo)
Jun 9 '10 #4
NeoPa
32,556 Expert Mod 16PB
Doma,

That's a tidy solution, but not how I would have done it myself (though now that solution is available I'm not so sure in future).

An alternative would be to export the results into a spreadsheet then, using Application Automation, handle the Copy and Paste Special / Transpose in your code.
Jun 10 '10 #5
doma23
107 100+
@NeoPa
Aha, so it's possible to insert Excel VBA code into Access sub.
Yeah, I've thought that something like that might be possible, but didn't know that it actually is.
Great, tnx for the pointing to another possible solution.
I think it's quite good, but I guess it requires a little bit more VBA knowledge, as I don't know how would I actually write the code.
Jun 10 '10 #6
NeoPa
32,556 Expert Mod 16PB
No worries Doma. Glad to help where I can.

Can you tell me exactly what you would need help with. I have added a post to that linked article after your reply, as I reread it and felt it was necessary. I don't know if that's all you need though. There are various examples included already, specifically one directed at using Excel. Can you explain what else would help, if indeed the latest post doesn't resolve your current difficulty.
Jun 10 '10 #7
doma23
107 100+
Ok, just to be easier, I'm copy-pasting the code:

Expand|Select|Wrap|Line Numbers
  1. ' Requires a Reference to Microsoft Excel 8.0 Object Library or Higher 
  2. ' In VBE, goto Tools->References... and select it from the list 
  3. Private Const conAppNotRunning As Long = 429 
  4.  
  5. Private Sub ExportToExcel() 
  6.     ' Variable Declarations 
  7.     Dim objExcel As Excel.Application 
  8.  
  9.     ' If Excel is open, use GetObject, otherwise create a new Excel object 
  10.     On Error Resume Next 
  11.     Set objExcel = GetObject(, "Excel.Application") 
  12.     If Err = conAppNotRunning Then Set objExcel = New Excel.Application 
  13.  
  14.     With objExcel 
  15.         ' Adds a new workbook to the Excel environment 
  16.         .Workbooks.Add 
  17.  
  18.         ' Excel VBA Code goes here 
  19.  
  20.         ' Causes the Excel window to become visible 
  21.         .Visible = True 
  22.     End With 
  23. End Sub 
So first, the title of the sub confuses me a little, as how I understood it - it doesn't export anything to Excel, it just controls Excel application.

I guess first I would need to use docmd.transferspreadsheet to export the query to excel and then call your ExcelAutomation sub.

So, this is how it should look like:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdExportXLS_Click() 
  2.  
  3. DoCmd.TransferSpreadsheet acExport, , "table-name", "C:\access\data.xls", False, "worksheet-name" 
  4. Call ExportToExcel  
  5.  
  6. End Sub 
Also, docmd.transferspreadsheet would just tranfer it to xls file, and afterward it would close xls file immediately. So how would ExportToExcel sub know which xls file to use? I'm looking at the code, and I don't quite understand it.

Of course, instead of ' Excel VBA Code goes here in ExportToExcel sub, I would put the code that would handle copy/paste/transpose.
I don't know how to code that. I was referring on that in my previous reply.

Also, I'm not sure would I need to use Range() property in this case. If I would need to, it would be a problem as you wrote that it would fail due to the absence of the reference object.

So this is how I see it, but there is a possibility that I got the idea completely wrong, as I'm quite new to Access and VBA world.

Looking forward to get your feedback, NeoPa.
Cheers!
Jun 11 '10 #8
NeoPa
32,556 Expert Mod 16PB
There are a couple of points to bear in mind here :
  1. The code example you refer to is a simple template to give an idea of the structure of using Excal code within Access. It is not an implementation of anything really other than the basics of the structure. The name of the procedure is pretty random, and only reflects your requirement by accident (or because that's probably the most popular use for Excel Automation anyway).
  2. The example code was submitted later by another member (as noted), so I can't absolutely guarantee that the code works well. It seems fine, but not checked fully by me. One caveat I would mention is that using :
    Expand|Select|Wrap|Line Numbers
    1. Set objExcel = New Excel.Application
    is not a technique I was aware of nor have I checked it. I would use the function CreateObject() myself. That is not to say it won't work. Just that I haven't tested that approach myself.
  3. Application Automation is not too simple a concept. I brought it to your attention because the description of your problem was specific enough to indicate it was the sort of thing you had in mind. It is quite advanced, and has various complications involved (not easy to develop), but when mastered can provide a great deal of power to your projects. If this sounds like something you're still interested in then I'm happy to lead you forward. I don't plan simply to do it all for you though. This must be something you feel comfortable taking on, rather than a neat trick you're happy for someone-else to do for you.
So, to answer a couple of points from your last post :
  1. If you export the data to a spreadsheet in your Access code, you would need to control and remember where that file is, and use that to control the file to open from within your embedded Excel code.
  2. You first need to develop the code you will use to do the transposing within Excel. It is pretty straightforward. If you struggle with this then a good technique is to record a macro then do the task manually. This will create some (pretty basic) code to do the job for you. You may need to tweak it somewhat, but it will provide all the basic techniques required.
Jun 11 '10 #9
doma23
107 100+
OK, now it's more clear when I know it's merely a template.
Like you said - it's not simple concept. And yes, I've had something like this in mind when I was searching for a solution, but honestly I've thought it would be easier.

Luckily, as I've already showed, I've came accross another pretty neat and maybe much easier solution (giving that the transpose function code is provided, as it is in this case).
I'm sure there are various situations in which it won't be possible to use that type of solutions and in which it will be required to use Excel automation.

But since I'm still not on advanced level (started using access two months ago) and since I have a deadline to build this application, this is not something I would feel comfortable to dive in to right now.
There are still some other less advanced concepts that I must master first and as far as the application I'm developing goes, I think that the transpose function and docmd.transferspreadsheet is more or less everything I will need to communicate with Excel.

But it's definitely very valuable to know that something like this does exist, so if the problem which require this type of solution arises one day I will know where to look and what to search for.

Therefore NeoPa, thank you very much for your effort and willingness to teach.
Jun 11 '10 #10
NeoPa
32,556 Expert Mod 16PB
It's been a pleasure Doma.

As you know, you know where we are now should you need us again :)
Jun 11 '10 #11

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

Similar topics

1
by: Roz Lee | last post by:
I want to cut and paste an Excel spreadsheet into a web page and then update my SQLServer database - it's basically an import function but using cut and paste rather than reading the file from a...
1
by: Mike Hammer | last post by:
Hello, We are building a report that needs the main form printed in landscape and the subforms in portrait. Everything we've tried to do this has failed, they all come out landscape. Anyone...
0
by: JLuv | last post by:
What i want to do is take the checked data from a datagrid, send it to a table, then show that table on an Excel Spreadsheet. Here is a little bit of my code... //adminProgress.aspx...
1
by: edralyn | last post by:
hello everyone! i have a problem in coding for my system...i want to know how could i use the Const transferDir = "c:\my documents\" rangeName = "Companies" tableName = "Companies" DoCmd.RunSQL...
2
by: Restless Native | last post by:
I am importing several Excel spreadsheets, and need to use the range in order to capture the field names. If the spreadsheets are closed, I get the following error "The table contains cells that are...
5
by: barien75 | last post by:
Does anyone know if there is a way to specify the file to transfer from? The files I need are all named the same with one small difference. Each one has the days date at the end of the file name....
2
by: Mufasa | last post by:
What are the ramifications of changing from Server.Transfer to Response.Redirect? I need to keep track of the URLs being used. Can you do relative addresses through redirect? TIA - Jeff.
1
by: is49460 | last post by:
Good afternoon! I use transfer spreadsheet function the export data from one of the table into the excel spreat sheet. I use the following code: DoCmd.TransferSpreadsheet acExport, 8, "qry...
9
by: HHung1977 | last post by:
Hello All - I currently have a database with a module that manipulates some data from Oracle and then writes the data into a table. I want to export the table to a web server, but when I use the...
2
by: fieldling | last post by:
I use the following code on a command button to export "Table1" into a spreadsheet called "Table1.xls. Private Sub Command14_Click() Private Sub Command14_Click() stDocName = "Table1" ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.