473,732 Members | 2,227 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Transfer data from a sub form and main form to excel spreadsheets

2 New Member
Hi, i have a form with the main details of a vendor and and contract description and a budget reference(PK). Within this form, i have a sub form which includes cost details for that specific vendor but also has the same budget reference hence the link. I would like to create a command button to be able to export the data from the main form to an excel spreadsheet and the data from the sub form to excel spreadsheet tab2 from the click of a button. Please help some genius out there that understands this problem. Here is what i have come up with so far: My query filters the main form and extracts the vendor details filtered by [Bud Ref].
This code does not fully work as i get an error message and it just opens the template i have designed but blank. Also it has a problem with my sSQL statement.

Expand|Select|Wrap|Line Numbers
  1. Private Function ExportQuery() As String
  2. On Error GoTo err_Handler
  3.  
  4.  
  5.  
  6. Dim appExcel As Excel.Application
  7. Dim wbk As Excel.Workbook
  8. Dim wks As Excel.Worksheet
  9. Dim wks2 As Excel.Worksheet
  10.  
  11. Dim sTemplate As String
  12. Dim sTempFile As String
  13. Dim sOutput As String
  14.  
  15. Dim dbs As DAO.Database
  16. Dim rst As DAO.Recordset
  17. Dim sSQL As String
  18. Dim lRecords As Long
  19. Dim iRow As Integer
  20. Dim iCol As Integer
  21. Dim iFld As Integer
  22.  
  23. Const cTabOne As Byte = 1
  24. Const cTabTwo As Byte = 2
  25. Const cStartRow As Byte = 3
  26. Const cStartColumn As Byte = 1
  27.  
  28. DoCmd.Hourglass True
  29. Excel.Application.SetOption "Error Tapping", 0
  30.  
  31.     sTemplate = "J:\2007\Databases\Contracts\ContractsForm.xls"
  32.     sOutput = "J:\2007\Databases\Contracts\ContractsFormTest.xls"
  33.  
  34. If Dir(sOutput) <> "" Then Kill sOutput
  35.  
  36. FileCopy sTemplate, sOutput
  37.  
  38. Set appExcel = New Excel.Application
  39. appExcel.Visible = True
  40. Set wbk = appExcel.Workbooks.Open(sOutput)
  41.  
  42. 'sSQL = "SELECT tblContracts.[Bud Ref], tblContracts.Vendor, tblContracts.[Contract Description], tblContracts.ApplicationSystemService, tblContracts.[Contract Type], tblContracts.[Cost Centre], tblContracts.HNC FROM tblContracts WHERE (((tblContracts.[Bud Ref])=[Forms]![frmContracts]![Bud Ref]))"
  43. sSQL = "SELECT * FROM tblDashboard"
  44.  
  45. 'DoCmd.OpenQuery "qryContracts", acViewDesign, acEdit
  46. 'DoCmd.OutputTo acOutputQuery, "", "MicrosoftExcel(.xls)", "ContractsForm", True
  47. 'DoCmd.SetWarnings False
  48. 'DoCmd.Close acQuery, "qryContracts", acSaveYes
  49.  
  50.  
  51.  
  52.  
  53. Set dbs = CurrentDb
  54. Set rst = dbs.OpenRecordset(sSQL, dbOpenSnapshot)
  55.  
  56. iCol = cStartColumn
  57. iRow = cStartRow
  58.  
  59.  
  60.     If Not rst.BOF Then
  61.         rst.MoveFirst
  62.             Do Until rst.EOF
  63.                 iFld = 0
  64.                 lRecords = lRecords + 1
  65.                 Me.Label16.Caption = "Exporting record #" & lRecords & " to ContractsFormTest.xls"
  66.                 Me.Repaint
  67.  
  68.  
  69.             rst.MoveNext
  70.         Loop
  71.  
  72.  
  73.  
  74.     ExportQuery = "Total of " & lRecords & "rows processed."
  75.  
  76.  
  77. exit_Here:
  78.  
  79. Set wbk = Nothing
  80. appExcel.Quit
  81. Set appExcel = Nothing
  82. Set rst = Nothing
  83. Set dbs = Nothing
  84. DoCmd.Hourglass False
  85. Exit Function
  86.  
  87. err_Handler:
  88. ExportQuery = Err.Description
  89. Resume exit_Here
  90. End If
  91.  
  92. End Function
  93.  
Nov 20 '07 #1
1 2740
Rabbit
12,516 Recognized Expert Moderator MVP
Please use code tags.

You have not told us:
1) What the error message was.
2) What the SQL error message was.

You have:
1) Dimmed worksheets yet did not use them.
2) Open workbooks and yet used the automated export function.
Nov 26 '07 #2

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

Similar topics

3
8010
by: Chris | last post by:
Could someone please provide me an effective means of exporting data from a data set (or data grid) to Excel?
9
4282
by: sifar | last post by:
Hi, I am right now learning PHP & want to know if there is a way to send web form data to an Excel sheet located on a network. My windows xp PC doesnot have a copy of Excel. Also i am not connected to the internet, its a local computer. What i am trying to do is to make a PHP script which will send a simple form data to an excel sheet (or create a new Excel sheet) on another PC on a network.
2
4990
by: Mr. Bungle | last post by:
When importing excel from access I am fully aware that one can import directly into a table. Can you get as specific via code or something to import data from an excel sheet to a FORM (Not Table) through a command button. For example: cell C5 on the selected excel spread sheet = textbox2 in the current form. This is for a service repair type database. The service technicians work out in the field and currently create their service orders...
14
3599
by: Peter | last post by:
Is there a fast way to move data from DataTable into double array, or do I have to spin through every record and column? I have five tables and I need to merge these tables column wise, each table will have to same amount of records but might have different amount of columns. So I need the first record form table1 and table2 and table3 and table4 and table5 to be in record 1 in my merged array or table. Second record form table1 and...
15
4117
by: John Machin | last post by:
I am pleased to announce a new general release (0.5.2) of xlrd, a Python package for extracting data from Microsoft Excel spreadsheets. CHANGES: * Book and sheet objects can now be pickled and unpickled. Instead of reading a large spreadsheet multiple times, consider pickling it once and loading the saved pickle; can be much faster. * Now works with Python 2.1. Backporting to Python 2.1 was partially
3
4117
by: JohnM | last post by:
I can transfer from a query with DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, "Filenam", CPath, True I would like to use a form for the user to select and order data then export th result. How do I set about this?
2
2441
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 outside the range of cells defined in this spreadsheet." If the spreadsheet is open, I am able to import the data, otherwise I get an error...and no data. How can I get around this issue. Thank you!
5
3217
by: billa856 | last post by:
Hi I am totaly new to MS Access. I have one worksheet in excel in which I have data of company's inventory. I want to know can I transfer data from a form made in MS Access to the Excel worksheet? Can I use access form to fetch or update data in excel worksheet? Can I use MS Access as frontend Or I have to use another frontend like VB?
5
3590
Khriskin
by: Khriskin | last post by:
I have searched the forums (as well as msdn and other websites) and have not been able to find a solution. Any and all suggestions would be greatly appreciated! Problem I have two Excel 2003 files from which I need to import/pull data, however these two files are almost always open/in use by the Scheduling Department. Current Workaround Currently I have a warning on the user interface that prompts the user to open the Excel spreadsheets...
0
8946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9447
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9307
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.