All using ms 2010. I have a form with unbound fields to filter to view report. The form uses a query and the report uses only a few fields from the query. This works fine. Now I want to be able to export the query with all the fields based on the filter from the form to excel. All I have been able to export is the fields from the report. Can someone help me export all the fields in the query but only the records from the filtered form?
Heres the code I used for the report:
Code:
Private Sub cmdExport_Click()
On Error GoTo Err_cmdExport_Click
Dim strSQL As String, intCounter As Integer
Dim db As Database, rs As Recordset
Dim ctl As Control, strname As String, strnewquery As String
Dim stDocName As String
Dim strRptSel As String
Dim stMessage As String
Set db = CurrentDb
'Build SQL String
For Each ctl In Me.Form
If ctl.Tag = "input" Then
strname = "me." & ctl.Name
If ctl.Value > "" Then
strSQL = strSQL & "[" & ctl.Name & "] " & " like " & Chr(34) & ctl.Value & Chr(34) & " And "
End If
End If
Next ctl
' Set the value of the parameter.
If Me.cboWEdateFrom & vbNullString <> "" And Me.cboWEdateTo & vbNullString <> "" Then
strSQL = strSQL & ("[Week Ending] BETWEEN #" & cboWEdateFrom & "# And #" & cboWEdateTo & "# And " & "'")
End If
strnewquery = "Select qryQAReport.* FROM qryQAReport"
If strSQL <> "" Then
strSQL = Left(strSQL, (Len(strSQL) - 5))
strnewquery = strnewquery & " WHERE " & strSQL & ";"
End If
Debug.Print strnewquery
' Create the recordset
Set rs = db.OpenRecordset(strnewquery)
If rs.RecordCount > 0 Then
DoCmd.OpenReport "rptQAReport", acViewPreview, , strSQL
DoCmd.Close acForm, "frmReportbuilder"
Else
MsgBox "There are no records that match your criteria! Please select new criteria.", vbInformation
Exit Sub
End If
Exit_cmdExport_Click:
Exit Sub
Err_cmdExport_Click:
Select Case Err.Number
Case 2501 'OpenQuery action was cancelled
Resume Exit_cmdExport_Click
Case Else
MsgBox Err.Description
Resume Exit_cmdExport_Click
Resume
End Select
End Sub
It's from the cmdPreview and now I am calling it cmdExport so I can export the filtered data to the query in excel.
Thanks