Solved

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

Posted on 2010-11-15
10
324 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL Bulk load data error 5 34
Access 2010 ComboBox Requery Not Working 17 24
Code editor Problem 8 13
DSum between dates 5 0
In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now