Solved

concatenating a prefix to the name of a worksheet in VBA

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

770 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