Hi Jennpen1
Sorry to be such a flake.
Yep the TransferSpreadsheet works fine. It is actually kind of cool since you can create and Excel spreadsheet ahead of time and the TransferSpreadsheet action will export to it and create a target sheet in the workbook with the same name as the query. Each time the export is run, the target sheet is overwritten. Cool part is you can create charts based on the data -- crosstabs, scatter diagrams,etc. and those remain available and untouched by the export. The data is replaced each time so it represents a kind of dynamic 'real time' analysis. The workbook will open up to whatever sheet was open when it was last saved so, if that was a chart, the end user simply sees a chart of the most recent data. The end user can also fiddle with the workbook as they wish and make their own versions.
The critical part is making sure the file and location called out in the TransferSpreadsheet actually exists or it will hang. I have it so that each end user can have a local version of the results. In this scenario they must start off with the basic spreadsheet(s) on their machine and located in the path specified. One could get fancy with dialog boxes locating the target spreadsheet or some public variable pointing to a common target -- whatever you need. I just wanted to keep it simple and this was a simple way to keep a user from clobbering someone else's work.
I used list boxes to select subsets of data and then define the query with the QueryDef("qry").SQL property. The tricky part for me was cutting and pasting the sql statement from the design grid into my form's class module. Little critical syntax things not easily found in documentation like making sure there is a space before the end quote and between that and the underscore line separator had me chasing my tail a couple of times especially late at night when I was getting bleary-eyed.
The important part in the sql string is in the WHERE clause where I can refer to a control on my form. This is where you could insert a complex OR string built up from your multi select list box as in "AND [somefield] = &" somestringvariable. I get the somestringvarible from a control on my form -- hidden or otherwise. You cannot refer to that control directly in your qery. Well, you can but sql doesn't know what to do with it. You have to fool it with the subterfuge.
Here is an example of that condition. In this example I have two controls -- one (Showlist) gives the end-user feedback on his/her selection. The other (strList) has all the OR's it it. Hope I don't get too many guffaws from all the code mavins out there on my lame code -- just a simple guy trying to make stuff work......
===============================================================
Private Sub Positions_In_ReportingGroups_Click()
Dim varItem As Variant
Dim strList As String
Dim nextItem As String
Dim strSql As String
strSql = ""
Me.Clear_btn.Visible = True
Me.txtSelected = Nothing
Me.SelectedReport = "ReportingGroups With Positions"
With Me.Positions_In_ReportingGroups
If .MultiSelect = 0 Then
Me.txtSelected = .Value
Me.txtParms = .Value
Else
ShowList = "" 'What user sees
strList = "" 'the WHERE clause string
For Each varItem In .ItemsSelected
ShowList = ShowList & .Column(0, varItem) & Chr(13) & Chr(10)
strList = strList & Chr(34) & .Column(0, varItem) & Chr(34) & " OR "
Next varItem
If Len(strList) Then
strList = Left$(strList, Len(strList) - 4) 'strip extra characters from end of string
End If
If Len(ShowList) Then
ShowList = Left$(ShowList, Len(ShowList) - 2) 'strip extra characters from end of string
End If
Me.txtSelected = ShowList
Me.txtParms = strList
strSql = "SELECT [First Name] & ' ' & [Last Name]" _
& "AS FullName," _
& "[Talent Review Data].[Business Results Score] ," _
& "[Talent Review Data].[Behavior Values Score]," _
& "[Talent Review Data].[Partner Number]," _
& "[Reporting Group Table].[Reporting Group Description]," _
& "[Reporting Group Table].[Reporting Group ID]," _
& "[Talent Review Data].[Current Position]" _
& "FROM [Reporting Group Table] INNER JOIN (([Partner Data]" _
& "INNER JOIN [Talent Review Data] ON" _
& "[Partner Data].[Partner Number] = [Talent Review Data].[Partner Number])" _
& "INNER JOIN [Reporting Group Business Units] ON" _
& "[Talent Review Data].[Current Business Unit] = [Reporting Group Business Units].[Business Unit Number])" _
& "ON [Reporting Group Table].[Reporting Group ID] = [Reporting Group Business Units].[Reporting Group ID]" _
& "WHERE ((([Talent Review Data].[Business Results Score]) > 0) And" _
& "(([Talent Review Data].[Behavior Values Score]) > 0) And" _
& "(([Reporting Group Table].[Reporting Group ID]) = [Forms]![MatrixToPrint_frm]![SelectedReportingGroups])" _
& "And" _
& "(([Talent Review Data].[Current Position]) = " _
& strList & "))" _
& "ORDER BY [Talent Review Data].[Business Results Score]," _
& "[Talent Review Data].[Behavior Values Score]," _
& "[Reporting Group Table].[Reporting Group ID], [Talent Review Data].[Current Position];"
CurrentDb.QueryDefs("LeadershipAssessmentSummary_byReportingGroupPositions_qry").SQL = strSql
End If
End With
strSql = ""
End Sub
===============================================================
Here is an overall solution without a multiselect contition and showing the TransferSpreadsheet part:
I find out what level of detail they want -- summary or detail. Each condition has to have it's own spreadsheet.
I also have a drill down to, in this case, Region and Site -- you could do more levels. The only change here is in the WHERE clause
I then call the TransferSpreadsheet in a standard module
===============================================================
Private Sub Command34_Click()
On Error GoTo Err_Command34_Click
Dim strDocName As String
Dim strWhereClause As String
Dim varItem As Variant
Dim strSql As String
Select Case Me.Selectedexport
Case "SummaryScores.xls"
If Me.SelectedLevel = "Regions" Then
strSql = "SELECT DISTINCTROW Names.Employee_ID, " _
& "[Names].[LastName] & "", "" & [Names].[FirstName]& "" "" & [Names].[MID_INIT] AS Name, " _
& "Positions_tbl.PositionDescription, " _
& "Profiles_tbl.ProfileDescription, " _
& "Supers.[NAME in HR db] AS ReportsTo, " _
& "Names.[Comp Rating], Names.Potential, Names.SumRatedBy, Names.SumRatedDate, " _
& "CorporateStructure_tbl.REGION_ID, CorporateStructure_tbl.SITE_ID " _
& "FROM (CorporateStructure_tbl INNER JOIN (([Names] LEFT JOIN [Names] AS Supers " _
& "ON Names.ReportsTo = Supers.PERSON_ID) INNER JOIN Positions_tbl " _
& "ON Names.Name_id = Positions_tbl.Name_id) " _
& "ON CorporateStructure_tbl.CorpLocation_ID = Positions_tbl.CorpLocation_ID) " _
& "INNER JOIN Profiles_tbl ON Positions_tbl.ProfileID = Profiles_tbl.ProfileID " _
& "WHERE (((CorporateStructure_tbl.REGION_ID) = Nz([Forms]![ExportToExcel_frm]![SelectedRegion]))) " _
& "ORDER BY [Names].[LastName] & "", "" & [Names].[FirstName] & "" "" & [Names].[MID_INIT];"
ElseIf Me.SelectedLevel = "Sites" Then
strSql = "SELECT DISTINCTROW Names.Employee_ID, " _
& "[Names].[LastName] & "", "" & [Names].[FirstName]& "" "" & [Names].[MID_INIT] AS Name, " _
& "Positions_tbl.PositionDescription, " _
& "Profiles_tbl.ProfileDescription, " _
& "Supers.[NAME in HR db] AS ReportsTo, " _
& "Names.[Comp Rating], Names.Potential, Names.SumRatedBy, Names.SumRatedDate, " _
& "CorporateStructure_tbl.REGION_ID, CorporateStructure_tbl.SITE_ID " _
& "FROM (CorporateStructure_tbl INNER JOIN (([Names] LEFT JOIN [Names] AS Supers " _
& "ON Names.ReportsTo = Supers.PERSON_ID) INNER JOIN Positions_tbl " _
& "ON Names.Name_id = Positions_tbl.Name_id) " _
& "ON CorporateStructure_tbl.CorpLocation_ID = Positions_tbl.CorpLocation_ID) " _
& "INNER JOIN Profiles_tbl ON Positions_tbl.ProfileID = Profiles_tbl.ProfileID " _
& "WHERE (((CorporateStructure_tbl.REGION_ID) = Nz([Forms]![ExportToExcel_frm]![SelectedRegion]))" _
& "AND ((CorporateStructure_tbl.SITE_ID)=Nz([Forms]![ExportToExcel_frm]![SelectedSite])))" _
& "ORDER BY [Names].[LastName] & "", "" & [Names].[FirstName] & "" "" & [Names].[MID_INIT];"
End If
CurrentDb.QueryDefs("ExportSummaryScores_qry").SQL = strSql
Call ExportSummary
Case "CompetencyDetailScores.xls"
If Me.SelectedLevel = "Regions" Then
strSql = "SELECT Names.[LastName] & "", "" & [FirstName] & "" "" & [MID_INIT] AS Name, " _
& "Positions_tbl.PositionDescription, " _
& "Profiles_tbl.ProfileDescription, " _
& "Competencies.Comp_Code, Competencies.Category, CompetencyScores_tbl.RatedLevel, " _
& "CompetencyScores_tbl.Certainty, CompetencyScores_tbl.[Rated By], " _
& "CompetencyScores_tbl.DateOfEvaluation, CompetencyScores_tbl.AssessedLevel, " _
& "CompetencyScores_tbl.AssessedBy, CompetencyScores_tbl.AssessedDate, " _
& "CorporateStructure_tbl.REGION_ID, CorporateStructure_tbl.SITE_ID " _
& "FROM [Names] INNER JOIN ((CorporateStructure_tbl INNER JOIN ((CompetencyScores_tbl " _
& "INNER JOIN Positions_tbl " _
& "ON CompetencyScores_tbl.Name_ID = Positions_tbl.Name_id) " _
& "INNER JOIN Competencies " _
& "ON CompetencyScores_tbl.CompetencyID = Competencies.Competency_ID) " _
& "ON CorporateStructure_tbl.CorpLocation_ID = Positions_tbl.CorpLocation_ID) " _
& "INNER JOIN Profiles_tbl " _
& "ON Positions_tbl.ProfileID = Profiles_tbl.ProfileID) " _
& "ON Names.Name_id = CompetencyScores_tbl.Name_ID " _
& "WHERE (((CorporateStructure_tbl.REGION_ID) = [Forms]![ExportToExcel_frm]![SelectedRegion])) " _
& "ORDER BY Names.[LastName] & "", "" & [FirstName] & "" "" & [MID_INIT], CategorySortOrder([Category]);"
ElseIf Me.SelectedLevel = "Sites" Then
strSql = "SELECT Names.[LastName] & "", "" & [FirstName] & "" "" & [MID_INIT] AS Name, " _
& "Positions_tbl.PositionDescription, " _
& "Profiles_tbl.ProfileDescription, " _
& "Competencies.Comp_Code, Competencies.Category, CompetencyScores_tbl.RatedLevel, " _
& "CompetencyScores_tbl.Certainty, CompetencyScores_tbl.[Rated By], " _
& "CompetencyScores_tbl.DateOfEvaluation, CompetencyScores_tbl.AssessedLevel, " _
& "CompetencyScores_tbl.AssessedBy, CompetencyScores_tbl.AssessedDate, " _
& "CorporateStructure_tbl.REGION_ID, CorporateStructure_tbl.SITE_ID " _
& "FROM [Names] " _
& "INNER JOIN ((CorporateStructure_tbl " _
& "INNER JOIN ((CompetencyScores_tbl " _
& "INNER JOIN Positions_tbl " _
& "ON CompetencyScores_tbl.Name_ID = Positions_tbl.Name_id) " _
& "INNER JOIN Competencies " _
& "ON CompetencyScores_tbl.CompetencyID = Competencies.Competency_ID) " _
& "ON CorporateStructure_tbl.CorpLocation_ID = Positions_tbl.CorpLocation_ID) " _
& "INNER JOIN Profiles_tbl " _
& "ON Positions_tbl.ProfileID = Profiles_tbl.ProfileID) " _
& "ON Names.Name_id = CompetencyScores_tbl.Name_ID " _
& "WHERE (((CorporateStructure_tbl.REGION_ID) = Nz([Forms]![ExportToExcel_frm]![SelectedRegion]))" _
& "AND ((CorporateStructure_tbl.SITE_ID)=Nz([Forms]![ExportToExcel_frm]![SelectedSite])))" _
& "ORDER BY Names.[LastName] & "", "" & [FirstName] & "" "" & [MID_INIT], CategorySortOrder([Category]);"
End If
CurrentDb.QueryDefs("ExportCompetencyScores_qry").SQL = strSql
Call ExportCompetencyScores
End Select
Exit_Command34_Click:
Exit Sub
Err_Command34_Click:
MsgBox Err.description
Resume Exit_Command34_Click
End Sub
================================================================================
================================================================================
Public Function ExportCompetencyScores() 'ExportCompetencyScores
On Error GoTo ExportCompetencyScores_Err
Dim strSql As String
Dim strQry As String
Dim strPath As String
Dim strFileName As String
Dim i As Integer
Dim xlApp As Object
Dim Sht As Object
' strSql = "" 'A SQL String could go here"
strQry = "ExportCompetencyScores_qry"
strPath = pubstrExcelPath 'public variable set at top of this module
strFileName = "CompetencyScoresExport.xls"
DoCmd.SetWarnings False
DoCmd.Hourglass True
MsgBox ("Exporting Competency Scores ' -- " & strFileName & "'")
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, strQry, strPath & strFileName, True
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
runExcel (strPath & strFileName)
Set Sht = Nothing
xlApp.Quit
Set xlApp = Nothing
DoCmd.Hourglass False
DoCmd.SetWarnings True
ExportCompetencyScores_Exit:
Exit Function
ExportCompetencyScores_Err:
MsgBox Error$
Resume ExportCompetencyScores_Exit
End Function
==========================================================================
I'm sure there are more elegant solutions but this works for me.
That's all I got for now. All this took me longer to figure out than I care to admit. Hope it helps.