Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-11-15
10
Medium Priority
?
361 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

972 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