Solved

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

Posted on 2010-11-15
10
328 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
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…
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.

920 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

17 Experts available now in Live!

Get 1:1 Help Now