?
Solved

Output data to excel template. help

Posted on 2007-03-19
1
Medium Priority
?
466 Views
Last Modified: 2013-11-27
Below is a bit of code that exports data to excell. It creates a new excel file  each time it is run. The file is named based on the s-file name and the location shown below.  I would like to add some code that would do the same but open the data in  a template  file called Export_Template.xls. The template will run calculations on the data.  Can anyone help me out?

JCz

    'Build filename
    sFile = "qryReactorBatch_" & Format(Now(), "mm-dd-yyyy-hhmm") & ".xls"

    'True Output to excel and open
   DoCmd.OutputTo acQuery, "qryReactorBatch", acFormatXLS, "J:\Fermentation_Scale-Up\DB_Export_Excel\" & sFile, True


Private Sub cmdexport_via_Click()
On Error GoTo Err_cmdexport_via_Click

   
    Dim MyDB As DAO.Database
    Dim qdef As DAO.QueryDef
    Dim i As Integer
    Dim strSQL As String
    Dim strWhere As String
    Dim strIN As String
    Dim flgSelectAll As Boolean
    Dim varItem As Variant
   
    Set MyDB = CurrentDb()
    'JCz 2/13/2007 Export Cell Count data Second
    'This is the code that generates the query...
   
    strSQL = "SELECT tbl_SpinName_Source.sns_Spinner, tbl_U2_Spinners.Date, tbl_U2_Spinners.Log_Hour, Round([tbl_U2_Spinners].[Log_Hour]/24,2)AS[Culture Days] , tbl_U2_Spinners.Viable, tbl_U2_Spinners.Total, Round([tbl_U2_Spinners].[Viable]/[tbl_U2_Spinners].[total]*100,2) AS [% Viable]  FROM tbl_SpinName_Source INNER JOIN tbl_U2_Spinners ON tbl_SpinName_Source.sns_ID = tbl_U2_Spinners.SpinnerID"
   
    'Build the IN string by looping through the listbox
    For i = 0 To LstFlasks.ListCount - 1
        If LstFlasks.Selected(i) Then
            If LstFlasks.Column(0, i) = "All" Then
                flgSelectAll = True
            End If
            strIN = strIN & "'" & LstFlasks.Column(0, i) & "',"
        End If
     Next i
     
    'Create the WHERE string, and strip off the last comma of the IN string
    strWhere = " WHERE [SNS_Spinner] in (" & Left(strIN, Len(strIN) - 1) & ") ORDER BY tbl_SpinName_Source.sns_Spinner, tbl_U2_Spinners.Date;"
   
    'If "All" was selected in the listbox, don't add the WHERE condition
    If Not flgSelectAll Then
        strSQL = strSQL & strWhere
    End If
   
    MyDB.QueryDefs.Delete "qryReactorBatch"
    Set qdef = MyDB.CreateQueryDef("qryReactorBatch", strSQL)
'***********************************************************************************
    'This code will open the qry before the excel file...
    'Open the query, built using the IN clause to set the criteria
    'DoCmd.OpenQuery "qryReactorBatch", acViewNormal
'*************************************************************************************

    'Build filename
    sFile = "qryReactorBatch_" & Format(Now(), "mm-dd-yyyy-hhmm") & ".xls"

    'True Output to excel and open
   DoCmd.OutputTo acQuery, "qryReactorBatch", acFormatXLS, "J:\Fermentation_Scale-Up\DB_Export_Excel\" & sFile, True
   
    'False Output to excel do not open
'    DoCmd.OutputTo acQuery, "qryReactorBatch", acFormatXLS, "c:\myfiles\" & sFile, False
'****************************************************************************************

   
    'Clear listbox selection after running query
    For Each varItem In Me.LstFlasks.ItemsSelected
        Me.LstFlasks.Selected(varItem) = False
    Next varItem
   
   
Exit_cmdexport_via_Click:
   
    Exit Sub
   
Err_cmdexport_via_Click:

   If Err.Number = 5 Then
        MsgBox "You must make a selection(s) from the list", , "Selection Required !"
        Resume Exit_cmdexport_via_Click
    Else
    'Write out the error and exit the sub
        MsgBox Err.Description
        Resume Err_cmdexport_via_Click
    End If

End Sub
0
Comment
Question by:jcuzzola
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 39

Accepted Solution

by:
stevbe earned 1500 total points
ID: 18750733
I think if you use the FileCopy command to make a copy of you template you can then either arrange the template to read from the dtaa that gets pumped in all in 1 big block or you will have to hand code pushing the data into the sreadsheet via automation

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question