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

Exporting multiple queries to formatted excel template

Hello All, Thanks for the help so far. I have been able to get a file working that allows dynamic field names to be selected by the user and run through a query. The last piece of the puzzle I am dealing with is exporting the query results to Excel templates for formatting. I have attached my code below. Is there anything I can do to direct the results to the first worksheet in a formatted excel doc (see attached) that is driven by a macro? Can you spot any flaws in my querydef /loops? It does what I want for the most part but I want to improve and learn how to do this the right way for future reference.


Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub btn_run_Click()
  5.  
  6. Dim Answer As String
  7. Dim MyNote As String
  8. Dim MyNote2 As String
  9. Dim MyNote3 As String
  10. Dim Complete As String
  11. Dim Cancel As String
  12. Dim Detail As String
  13.  
  14. MyNote = "This process may take several minutes to complete.  Would you like to proceed?"
  15. MyNote2 = "Process complete.  Thanks for your patience."
  16. MyNote3 = "Process cancelled."
  17.  
  18. Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "Note")
  19.  
  20. If Answer = vbNo Then
  21.     Cancel = MsgBox(MyNote3, vbInformation, "Process Cancelled")
  22.     Exit Sub
  23.  
  24. Else
  25.  
  26. Dim DB As Database
  27. Dim Carrier As Recordset
  28. Dim setup As Recordset
  29. Dim QD As QueryDef
  30. Dim qd2 As QueryDef
  31.  
  32. Dim QDname As String
  33. Dim SCAC As String
  34. Dim Splitter As String
  35. Dim StrSQL As String
  36. Dim ctl As Control
  37. Dim StrName As String
  38. Dim StrSQLFinal As String
  39. Dim intNumSelected As Integer
  40. Dim Templatecopy As String
  41. Dim FileDest As String
  42. Dim Outputfile As String
  43. Dim SQL_SCACs As String
  44. Dim SelectedSplit As String
  45. Dim strMakeQD
  46. Dim Count_Records As Long
  47. Dim RunDate As Date
  48. Dim i As Integer
  49.  
  50.  
  51. Set DB = CurrentDb()
  52.  
  53. For Each ctl In Me.Controls
  54.   If ctl.ControlType = acCheckBox Then      'Control is a Checkbox
  55.     If ctl.Value Then                       'It is Selected?
  56.       intNumSelected = intNumSelected + 1
  57.         StrName = Replace(ctl.Name, "ck", "")
  58.           StrSQL = StrSQL & "[" & StrName & "], "
  59.     End If
  60.   End If
  61. Next
  62.  
  63.  
  64. If intNumSelected = 0 Then Exit Sub         'No Column(s) selected
  65.  
  66. 'Link to Template and Output files
  67. Templatecopy = Me.txtTemplate
  68. Outputfile = Me.txtFilename
  69.  
  70.  
  71. 'Create the SQL statement for checkboxed headers
  72. StrSQL = Left(StrSQL, Len(StrSQL) - 2)
  73. StrSQLFinal = "SELECT " & StrSQL & " FROM data WHERE " & Me.txtSplitfield & " = "
  74.  
  75. 'Gathers SCAC codes present in the input file
  76. SQL_SCACs = "SELECT " & Me.txtSplitfield & " as Splitter FROM data GROUP BY " & Me.txtSplitfield & " ORDER BY " & Me.txtSplitfield & ";"
  77. Set Carrier = DB.OpenRecordset(SQL_SCACs)
  78.  
  79. Set QD = Nothing
  80.  
  81. Do
  82.  
  83. SelectedSplit = Carrier!Splitter
  84. QDname = "qd_" & SelectedSplit
  85. strMakeQD = StrSQLFinal & Chr(34) & SelectedSplit & Chr(34) & ";"
  86.  
  87. For Each QD In DB.QueryDefs
  88.         If QD.Name = QDname Then
  89.             DB.QueryDefs.Delete QDname
  90.         End If
  91.  
  92.         Next
  93.  
  94.         DB.CreateQueryDef QDname, strMakeQD
  95.  
  96. template = "L:\Operations\FOS\DBs\Schedule A\Outputs\" & SelectedSplit & "-" & Outputfile
  97.  
  98.             FileCopy Templatecopy, template
  99.  
  100.             Count_Records = DCount("SCAC", QDname)
  101.             If (Count_Records > 0) Then
  102.                 DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, QDname, template, False, ""
  103.                 i = i + 1
  104.                 Call update_excel_loop(template, i)
  105.                 i = 0
  106.             End If
  107.             'DB.QueryDefs.Delete QDname
  108.  
  109. Carrier.MoveNext
  110.  
  111. Loop Until Carrier.EOF
  112.  
  113.  
  114. End If
  115.  
  116. Complete = MsgBox(MyNote2, vbInformation, "Process Complete")
  117.  
  118. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboRunID2_Change()
  2.  
  3.  
  4. Dim db2 As Database
  5. Dim PopFields As Recordset
  6. Dim DataPop As String
  7.  
  8. Set db2 = CurrentDb()
  9.  
  10. DataPop = "SELECT [RunID], [Template], [filename], [split_field], [LaneID], [RefNum], [OFacility], [OCity], [OState], [OZip], [ORegion], [OCountry], [DFacility], [DCity], [DState], [DZip], [DRegion], [DCountry], [Distance], [AnnualVol], [CarName], [CarRef], [Mode], [Equip], [EquipSize], [OrBid], [OrRateType], [OrMinChge], [OrTotalRate], [AssetType], [AllocAmount], [RateGuideSeq], [AwardType] from tbl_setup_detail where RunId = """ & Me.cboRunID2 & """"
  11. Set PopFields = db2.OpenRecordset(DataPop)
  12.  
  13. Me.txtTemplate.Value = PopFields!template
  14. Me.txtFilename.Value = PopFields!filename
  15. Me.txtSplitfield.Value = PopFields!split_field
  16. Me.ckLaneID = PopFields!LaneID
  17. Me.CkRefNum = PopFields!RefNum
  18. Me.CkOfacility = PopFields!OFacility
  19. Me.CkOCity = PopFields!OCity
  20. Me.CkOState = PopFields!OState
  21. Me.ckOZip = PopFields!OZip
  22. Me.ckOregion = PopFields!ORegion
  23. Me.ckOCountry = PopFields!OCountry
  24. Me.ckDFacility = PopFields!DFacility
  25. Me.ckDCity = PopFields!DCity
  26. Me.ckDState = PopFields!DState
  27. Me.ckDZip = PopFields!DZip
  28. Me.ckDRegion = PopFields!DRegion
  29. Me.ckDCountry = PopFields!DCountry
  30. Me.ckDistance = PopFields!Distance
  31. Me.ckAnnualVol = PopFields!AnnualVol
  32. Me.ckCarName = PopFields!CarName
  33. Me.ckSCAC = PopFields!CarRef
  34. Me.ckTransMode = PopFields!Mode
  35. Me.ckEquip = PopFields!Equip
  36. Me.ckEquipSize = PopFields!EquipSize
  37. Me.ckOrBid = PopFields!OrBid
  38. Me.ckRateType = PopFields!OrRateType
  39. Me.ckMinChge = PopFields!OrMinChge
  40. Me.ckTotalRate = PopFields!OrTotalRate
  41. Me.ckAssetType = PopFields!AssetType
  42. Me.ckAllocAmount = PopFields!AllocAmount
  43. Me.ckRouteGuideSeq = PopFields!RateGuideSeq
  44. Me.ckAwardType = PopFields!AwardType
  45.  
  46. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub update_excel_loop(template, i)
  2. 'declare variables
  3. Dim xlApp As Excel.Application
  4. Dim xlBook As Excel.Workbook
  5.  
  6. 'excel application stuff
  7. If i = 0 Then
  8.     Set xlApp = New Excel.Application
  9. Else
  10.     Set xlApp = Excel.Application
  11. End If
  12.  
  13. xlApp.Visible = True
  14. Set xlBook = xlApp.Workbooks.Open(template)
  15.  
  16. 'run the macro
  17. xlApp.Run "Macro1"
  18.  
  19. 'save file
  20. xlBook.Save
  21.  
  22. 'done
  23. xlApp.Quit
  24. Set xlBook = Nothing
  25. Set xlApp = Nothing
  26. End Sub
  27.  
  28.  
The main issue I am having is that the export creates a new worksheet instead of pasting on page 1 and calling the formatting.
Feb 7 '14 #1
0 1121

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

Similar topics

1
by: | last post by:
Hi all, I have some statictcle functions in C#, I am making some Excel templates, they are xls files. How to make my template using these functions? Before these functions are in Excel VB so...
3
by: Karen A Hodge | last post by:
I have a website that has an Excel template. The template contains 3 QueryTables. The template is located on the web server. I would like to refresh the data prior to the user opening the template...
2
by: Brent Bortnick | last post by:
Hello, Is it possible to send data from ASP and put it into a existing template? i don't want to create an excel document I only want to send one piece of information from ASP to the Exisiting...
0
by: mandara | last post by:
hi i was not able to write a programs such that it retrives the data from the database through sql queries and get the output in Excel using perl.when i write multiple Queries i need to get all...
3
by: CCECIL | last post by:
Hello all, I am new to VBA and I am not very good at it. I have chosen to learn VBA for this simple reason...I can't figure out how to export an Access table into a formatted excel document. ...
2
by: ezra | last post by:
I have an access report based on a bunch of queries that does a quarterly summary of some research related stats. Now my boss wants this report to export into excel based on a specific format she has...
13
by: Peter | last post by:
VS2008 ans ASP.NET 3.5, Office 2003 What is the best way to run Excel Template from ASP.NET web page were the Excel is only installed on the client without any ActiveX? If so can someone point...
3
by: nandithadevaraj | last post by:
When exporting a report to excel, is it possible to keep the properties around numeric values? More specifically - when exporting a report into excel, all of the numbers in a currency column are...
3
by: spl1490 | last post by:
Hello, I have a list of people that I am looking to export a report to excel about for each of them separately. I have already done this for a set number of people, but I had to create a lot of...
1
by: RobT | last post by:
My project is moving along smoothly- I've gotten a lot of info from this forum. One thread that helped me understand what I was doing is here. But I have a question I can't find an answer to. My...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.