Link to home
Start Free TrialLog in
Avatar of otamian
otamian

asked on

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

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?
Avatar of snailcat
snailcat
Flag of United States of America image

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

Avatar of otamian
otamian

ASKER

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.
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.
Avatar of otamian

ASKER

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.
Avatar of otamian

ASKER

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

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?

Avatar of otamian

ASKER

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.  
ASKER CERTIFIED SOLUTION
Avatar of snailcat
snailcat
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial