Export Or Print Data selected from Combo boxes

Groundrush

Registered User.
Local time
Today, 13:06
Joined
Apr 14, 2002
Messages
1,376
Using various combo boxes I have a system that limits & displays records depending on what has been selected. it works well using the code below but I need to be able to also output the results to an excel spreadsheet or maybe be able to print print.

can someone please assist me & see if this code can be altered to allow this as I can't seem to work it out.

Code:
Private Sub cmdSearch_Click()
'Set the Dimensions of the Module
Dim strsql As String, strOrder As String, strWhere As String
Dim dbNm As Database
Dim qryDef As QueryDef
Set dbNm = CurrentDb()

'Constant Select statement for the RowSource
strsql = "SELECT qryTaskControl.TaskID,qryTaskControl.EformRef,qryTaskControl.Contract,qryTaskControl.JobNo, qryTaskControl.DateReported,qryTaskControl.DueBy,qryTaskControl.ContractName,qryTaskControl.Priority, qryTaskControl.PropertyName, qryTaskControl.PropertyAddress,qryTaskControl.Location,qryTaskControl.JobDetails,qryTaskControl.FurtherDetails,qryTaskControl.Category,qryTaskControl.Trade,qryTaskControl.CallersName,qryTaskControl.CallersPhoneNumber,qryTaskControl.EstimatedCost,qryTaskControl.LOC,qryTaskControl.Status,qryTaskControl.Cancelled,qryTaskControl.History, qryTaskControl.Printed " & _
"FROM qryTaskControl"

strWhere = "WHERE"

'strOrder = "ORDER BY qryTaskControl.DateReported;" ' do not remove the comment as it will reverse the order of the records

'Set the WHERE clause for the Listbox RowSource if information has been entered into a field on the form
If Not IsNull(Me.txtEformRef) Then
strWhere = strWhere & " (qryTaskControl.EformRef) Like '*" & Me.txtEformRef & "*'  AND"
End If
If Not IsNull(Me.cboContract) Then '<--If the textbox cboContract No contains no data THEN do nothing
strWhere = strWhere & " (qryTaskControl.Contract) Like '*" & Me.cboContract & "*'  AND"
End If
If Not IsNull(Me.cboJobNo) Then
strWhere = strWhere & " (qryTaskControl.JobNo) Like '*" & Me.cboJobNo & "*'  AND"
End If
If Not IsNull(Me.txtDateReported) Then
strWhere = strWhere & " (qryTaskControl.DateReported) Like '*" & Me.txtDateReported & "*'  AND"
End If
If Not IsNull(Me.cboPriority) Then
strWhere = strWhere & " (qryTaskControl.Priority) Like '*" & Me.cboPriority & "*'  AND"
End If
If Not IsNull(Me.cboProperty) Then
strWhere = strWhere & " (qryTaskControl.PropertyName) Like '*" & Me.cboProperty & "*'  AND"
End If
If Not IsNull(Me.txtJobDetails) Then
strWhere = strWhere & " (qryTaskControl.JobDetails) Like '*" & Me.txtJobDetails & "*'  AND"
End If
If Not IsNull(Me.cboTrade) Then
strWhere = strWhere & " (qryTaskControl.Trade) Like '*" & Me.cboTrade & "*'  AND"
End If
If Not IsNull(Me.cboCallersName) Then
strWhere = strWhere & " (qryTaskControl.CallersName) Like '*" & cboCallersName & "*'  AND"
End If
If Not IsNull(Me.cboLOC) Then
strWhere = strWhere & " (qryTaskControl.LOC) Like '*" & Me.cboLOC & "*'  AND"
End If
If Not IsNull(Me.cboStatus) Then
strWhere = strWhere & " (qryTaskControl.Status) Like '*" & Me.cboStatus & "*'  AND"
End If

'Remove the last AND from the SQL statment
strWhere = Mid(strWhere, 1, Len(strWhere) - 5)
'Pass the SQL to the RowSource of the listbox
Me.lstTasks.RowSource = strsql & " " & strWhere & "" & strOrder
End Sub


Thanks :-)
 
Here's some code I use to export to xls. I create a module called basExportFormRecordsource and put the following code in it:

Code:
Public Sub CopyFromRecordsetPassed(rst As Object, Optional blnHeader As Boolean = True)
    
    Dim objExcel As Excel.Application
    Dim objwb As Excel.Workbook
    Dim rng As Excel.Range
    Dim intI
    
    If rst Is Nothing Then
        Exit Sub
    End If
    
    'Create Excel instance with new book
    Set objExcel = New Excel.Application
    With objExcel
        Set objwb = .Workbooks.Add
        'Show and persist it
        .Visible = True
        .UserControl = True
    End With
    
    If blnHeader Then
        'Insert columns headers
        For intI = 0 To rst.Fields.Count - 1
            objwb.ActiveSheet.Cells(1, intI + 1).value = rst.Fields(intI).Name
        Next
    End If
    
    If TypeOf rst Is DAO.Recordset Then
        rst.MoveLast
        rst.MoveFirst
    End If
    intI = rst.RecordCount
    
    'create data row
    objwb.ActiveSheet.Range("A2").CopyFromRecordset rst
     
    Set rst = Nothing
    Set objwb = Nothing
    Set objExcel = Nothing
 
End Sub

Then call the routine CopyFromRecordsetPassed() with the following code:

Code:
Private Sub cmdExport_Click()
'====================================================================================================
' Name: cmdExport_Click
' Use: Export current form recordset
' Created By: Ken Higginbotham
' Modified By:
' Date: 5/21/2008
' Called By:
' Calls:basExportFormRecordsource.CopyFromRecordsetPassed
' Update:
' Notes:
'====================================================================================================
On Error GoTo Err_cmdExport_Click
    
    'Initiate recordset object
    Dim rst As DAO.Recordset
        
    'Confirm task
    If MsgBox("Exort List?", vbYesNo + vbQuestion, "Export...") = vbNo Then Exit Sub
    
    'Set recordset object object
    Set rst = Me.ctlsfrmMRO_01.Form.RecordsetClone
    
    'Set rst = Me.sfmDeals.Form.Recordset.Clone
    CopyFromRecordsetPassed rst
    
    'Kill recordset object
    Set rst = Nothing
    
Exit_cmdExport_Click:
    Exit Sub
    
Err_cmdExport_Click:
    MsgBox Err.Description
    Resume Exit_cmdExport_Click

End Sub

All you should have to do is alter the 'set rst' line. I like it because you're brought right into excel with the data. I didn't write the module code, forget where it came from - :)
 
Thank you.

I look forward to having a go.
 

Users who are viewing this thread

Back
Top Bottom