thread705-338328
I have a command on a form with a multi select list box (simple) which runs a report based on a query populated with values pulled from the selection made in the list box.
What I would prefer to do is export the query to excel and open the spreadsheet all based on the click of the command, I know it requires me to provide instruction on the last line, but not even sure where to start ! ...
Here is the code I already have that works but requires the user to export a report to excel and then open the spreadsheet.
Private Sub Command16_Click()
On Error GoTo ErrorHandler
Dim strWhere As String
Dim ctl As Control
Dim varItem As Variant
'make sure a selection has been made
If Me.List12.ItemsSelected.Count = 0 Then
MsgBox "Must select at least 1 company"
Exit Sub
End If
'add selected values to string
Set ctl = Me.List12
For Each varItem In ctl.ItemsSelected
strWhere = strWhere & ctl.ItemData(varItem) & ","
'Use this line if your value is text
'strWhere = strWhere & chr34 & ctl.ItemData(varItem) & chr34 & ","
Next varItem
'trim trailing comma
strWhere = Left(strWhere, Len(strWhere) - 1)
'open the report, restricted to the selected items
DoCmd.OpenReport "Report1", acPreview, , "[PortalData].[Company id] IN(" & strWhere & ")"
ExitHandler:
Exit Sub
ErrorHandler:
Select Case Err
Case Else
MsgBox Err.Description
DoCmd.Hourglass False
Resume ExitHandler
End Select
End Sub
I have a command on a form with a multi select list box (simple) which runs a report based on a query populated with values pulled from the selection made in the list box.
What I would prefer to do is export the query to excel and open the spreadsheet all based on the click of the command, I know it requires me to provide instruction on the last line, but not even sure where to start ! ...
Here is the code I already have that works but requires the user to export a report to excel and then open the spreadsheet.
Private Sub Command16_Click()
On Error GoTo ErrorHandler
Dim strWhere As String
Dim ctl As Control
Dim varItem As Variant
'make sure a selection has been made
If Me.List12.ItemsSelected.Count = 0 Then
MsgBox "Must select at least 1 company"
Exit Sub
End If
'add selected values to string
Set ctl = Me.List12
For Each varItem In ctl.ItemsSelected
strWhere = strWhere & ctl.ItemData(varItem) & ","
'Use this line if your value is text
'strWhere = strWhere & chr34 & ctl.ItemData(varItem) & chr34 & ","
Next varItem
'trim trailing comma
strWhere = Left(strWhere, Len(strWhere) - 1)
'open the report, restricted to the selected items
DoCmd.OpenReport "Report1", acPreview, , "[PortalData].[Company id] IN(" & strWhere & ")"
ExitHandler:
Exit Sub
ErrorHandler:
Select Case Err
Case Else
MsgBox Err.Description
DoCmd.Hourglass False
Resume ExitHandler
End Select
End Sub