Solved

Output data to excel template. help

Posted on 2007-03-19
1
465 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 500 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

688 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