Solved

concatenating a prefix to the name of a worksheet in VBA

Posted on 2014-10-07
4
166 Views
Last Modified: 2014-10-07
Experts,

I am creating a spreadsheet for each operator from a table called "EMP".  I would like to prefix each tab with "EMP_".  The code below currently loops through and creates the tab.  

Can someone tell me how to concatenate the "EMP_" to name of each tab in the code below.


DoCmd.DeleteObject acTable, "DISTINCT_EMPS"
   Db.Execute "SELECT DISTINCT SALV_OPERATOR  INTO DISTINCT_EMPS FROM EMP"



   Set rs = Db.OpenRecordset("SELECT [DISTINCT_EMPS].SALV_OPERATOR FROM [DISTINCT_EMPS] GROUP BY [DISTINCT_EMPS].SALV_OPERATOR;", dbOpenSnapshot, dbReadOnly)
   rs.MoveFirst
   Do Until rs.EOF
      'DoCmd.DeleteObject acQuery, rs!SALV_OPERATOR
      Db.CreateQueryDef rs!SALV_OPERATOR, "SELECT [EMP].* FROM [EMP] WHERE ((([EMP].SALV_OPERATOR)='" & rs!SALV_OPERATOR & "'));"
      DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, rs!SALV_OPERATOR, strOutputFileExcel
   rs.MoveNext
   Loop
0
Comment
Question by:morinia
  • 3
4 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
Change

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, rs!SALV_OPERATOR, strOutputFileExcel

to

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, rs!SALV_OPERATOR, "EMP_" & strOutputFileExcel
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
Comment Utility
Alternatively:

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, rs!SALV_OPERATOR, strOutputFileExcel,,"EMP_" & strOutputFileExcel

which really shouldn't work, but according to http://accessblog.net/2006/07/export-to-excel-range.html it does.
0
 

Author Comment

by:morinia
Comment Utility
Is it possible for you to give me the syntax to delete the above query also.  Currently i have this

  Set rs = Db.OpenRecordset("SELECT [DISTINCT_EMPS].SALV_OPERATOR FROM [DISTINCT_EMPS] GROUP BY [DISTINCT_EMPS].SALV_OPERATOR;", dbOpenSnapshot, dbReadOnly)
   rs.MoveFirst
   Do Until rs.EOF
      DoCmd.DeleteObject acQuery, rs!SALV_OPERATOR
    rs.MoveNext
  Loop
0
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
Seems OK. The only thing you should do is put a

ON ERROR RESUME NEXT

command before it, in case the query doesn't exist.

You may also want to use

DoCmd.SetWarnings False

at the beginning, and

DoCmd.SetWarnings True

to get rid of the dialog boxes.
0

Featured Post

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

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

771 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

11 Experts available now in Live!

Get 1:1 Help Now