?
Solved

concatenating a prefix to the name of a worksheet in VBA

Posted on 2014-10-07
4
Medium Priority
?
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40365837
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 2000 total points
ID: 40365840
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
ID: 40365864
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
ID: 40365875
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

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.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

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