Solved

Access Macro to Output an Excel Dynamic Filename based on a stored field value.

Posted on 2010-11-15
10
332 Views
Last Modified: 2014-06-16
Hi - I am having trouble with the Output File filename syntax to include a 3 letter dept value from my table?  Is this possible?  I've got the date format working in the filename, but now I want to output the current agency code without having to use an InputBox
Any ideas?
0
Comment
Question by:otamian
  • 4
  • 4
10 Comments
 
LVL 12

Expert Comment

by:snailcat
ID: 34140919
This will pull from a field named 'deptvalue' and place it in your excel file output name.  The current code will save the file in the location C:\folder\ with the name testfile_ followed by the 'deptvalue' but can be changed with your needs.

-------
 
On Error GoTo namefile1_Err

    Dim deptname As String
    deptname = Me.deptvalue
    
    DoCmd.OutputTo acOutputTable, "Table1", "Excel97-Excel2003Workbook(*.xls)", "C:\folder\testfile_" & deptvalue & ".xls", False, "", 0, acExportQualityPrint


namefile1_Exit:
    Exit Sub

namefile1_Err:
    MsgBox Error$
    Resume namefile1_Exit

Open in new window

0
 

Author Comment

by:otamian
ID: 34148179
although I was trying to do it in a macro, I thank you for this approach.  Code is better than a macro.  One question: do I put this on a blank form or what type of object/event?  I want this to happen automatically after some other steps have run.
0
 
LVL 12

Expert Comment

by:snailcat
ID: 34148320
You can have this run after whatever the other steps are that happen first. If the prior steps are in code just add this after that code.
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:otamian
ID: 34149184
Thanks.  Unfortunately, the other steps are in a macro... but can I place it in a RunCode action on my existing macro?  Sorry to be so dense.
0
 

Author Comment

by:otamian
ID: 34149316
As expected, I have something confused in this process.  I created a module called Archive_Detail in which I placed and attempted to modify your code.  I can't figure out how it would know what table the field DOC_DEPT_CD would be located.  I tried using the table name instead of Me. but the editor didn't like it.

Option Compare Database
On Error GoTo namefile1_Err

    Dim deptname As String
    deptname = Me.DOC_DEPT_CD
    
    DoCmd.OutputTo acOutputTable, "0_Results", "Excel97-Excel2003Workbook(*.xls)", "S:\QAB\Shared\_DATASPIKE\testfile_" & DOC_DEPT_CD & ".xls", False, "", 0, acExportQualityPrint

namefile1_Exit:
    Exit Sub

namefile1_Err:
    MsgBox Error$
    Resume namefile1_Exit

Open in new window

0
 
LVL 12

Expert Comment

by:snailcat
ID: 34149584
Are you running this in a form?

Is there more than one value of OC_DEPT_CD in the table?  If so how do you choose with value of the variable in the table you want for the file name?

0
 

Author Comment

by:otamian
ID: 34150730
Excellent question, but its far more simple than that.  The table is a download from another system that runs for one department at a time.  I can ouput a single occurance of the dept value into a one-column, one row table which would be a good place.  Table and Field are as follows: "[DEPT].[DOC_DEPT_CD]"

I could run the code as an OnClose event in a form or an OnOpen event on a report.  
0
 
LVL 12

Accepted Solution

by:
snailcat earned 500 total points
ID: 34152677
If the table has only a single value you can just open a recordset of the table and it will have your value.

This should work.

HTH,
snailcat
Option Compare Database
On Error GoTo namefile1_Err


Dim rs As DAO.Recordset
   Dim db As DAO.Database
   
     Dim deptname As String
 
        Set db = CurrentDb
       Set rs = db.OpenRecordset("DEPT")
       deptname = rs!DOC_DEPT_CD


    DoCmd.OutputTo acOutputTable, "0_Results", "Excel97-Excel2003Workbook(*.xls)", "S:\QAB\Shared\_DATASPIKE\testfile_" & deptname & ".xls", False, "", 0, acExportQualityPrint
  
namefile1_Exit:
    Exit Sub

namefile1_Err:
    MsgBox Error$
    Resume namefile1_Exit

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
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…

856 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