Solved

concatenating a prefix to the name of a worksheet in VBA

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

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.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

733 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