Solved

concatenating a prefix to the name of a worksheet in VBA

Posted on 2014-10-07
4
180 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 500 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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 how to use a scrolling table in Microsoft Excel using the INDEX function.

695 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