I am trying to upload a filtered form to excel. Using code someone posted to me I tried to make this work. But like all great code for a newbee.. IT DOSENT WORK! Can some one help me out.
Code:
Code:
Code:
Private Sub Excel_Click() 'Be sure to set your References vis Tools on the Menu Bar to 'Microsoft Excel 10.0 Object Library or to what ever 'version excel you are running Dim rs As Recordset Dim intMaxCol As Integer Dim intMaxRow As Integer Dim objXL As Excel.Application Dim objWkb As Workbook Dim objSht As Worksheet Dim f As field Dim i As Long Dim objXLApp As Object Dim objXLws As Object Dim db As DAO.Database Dim rst As DAO.Recordset Dim strDocPath Dim strPath As String Dim sSql As String Dim sCriteria As String sCriteria = "WHERE 1=1 " If Me![cboFilterPONumber] <> "" Then sCriteria = sCriteria & " AND [Milestone Serch Query].[Purchase Order] = """ & cboFilterPONumber & """" End If If Me![cboFilterDescription] <> "" Then sCriteria = sCriteria & " AND [Milestone Serch Query].Description like """ & cboFilterDescription & "*""" End If If Me![cboFilterBuilding] <> "" Then sCriteria = sCriteria & " AND [Milestone Serch Query].Building Like """ & cboFilterBuilding & "*""" End If If Me![txtStartDate] <> "" And txtEndDate <> "" Then sCriteria = sCriteria & " AND [Milestone Serch Query].[Ship Date] between #" & Format(txtStartDate, "dd-mmm-yyyy") & "# and #" & Format(txtEndDate, "dd-mmm-yyyy") & "#" End If If Me![txtFilterTAG] <> "" Then sCriteria = sCriteria & " AND [Milestone Serch Query].TAG like """ & txtFilterTAG & "*""" End If sSql = "SELECT [Milestone Serch Query].[TAB], [Milestone Serch Query].[Purchase Order], [Milestone Serch Query].[Description], [Milestone Serch Query].[Supplier], [Milestone Serch Query].[Building], [Milestone Serch Query].[Tag Number], [Milestone Serch Query].[Ship Date], [Milestone Serch Query].[Intermediate Delivery], [Milestone Serch Query].[Intermediate Shipment], [Milestone Serch Query].[Arrival Date], [Milestone Serch Query].[Length], [Milestone Serch Query].[Width], [Milestone Serch Query].[Height], [Milestone Serch Query].[Weight], [Milestone Serch Query].[Shipping Probability], [Milestone Serch Query].[Notes], [Milestone Serch Query].[Shipment Release Number] FROM [Milestone Serch Query]" & sCriteria Forms![frmSearchCriteriaMain]![Milestone Serch Query subform].Form.RecordSource = sSql Forms![frmSearchCriteriaMain]![Milestone Serch Query subform].Form.Requery Set rs = CurrentDb.OpenRecordset(sSql, dbOpenSnapshot) intMaxCol = rs.Fields.Count If rs.RecordCount < 1 Then MsgBox "The report you are trying to produce does not contain any data!" & vbCr & vbCr & _ "Please check that there is data for this report.", vbCritical Else ' Populate the excel object Set objXLApp = CreateObject("Excel.Application") ' Open the template workbook objXLApp.Workbooks.Open ("Milestone Serch Query") ' Save the template as the file specified by the user objXLApp.ActiveWorkbook.SaveAs ("Milestone Serch Query") ' Select the 'Raw Data' worksheet Set objXLws = objXLApp.ActiveWorkbook.Worksheets(1) ' Activate the selected worksheet objXLws.Activate ' Ask Excel to copy the data from the recordset objXLws.Range("A1").CopyFromRecordset rs ' Select the main worksheet objXLApp.Worksheets("Milestone Serch Query").Activate ' Activate the selected worksheet Set objXLws = objXLApp.ActiveWorkbook.Worksheets("Milestone Serch Query") ' Populate the criteria box on the main form (so the user knows what was exported) objXLws.Cells(1, 2).Value = sCriteria End If ' Destroy the recordset and database objects Set rst = Nothing Set db = Nothing ' Hide warnings on the spreadsheet objXLApp.DisplayAlerts = False ' Refresh the root PivotTable (which refreshes all) objXLApp.ActiveWorkbook.Save ' Turn spreadsheet warnings back on objXLApp.DisplayAlerts = True ' Make it visible objXLApp.Visible = True '**error handling, in the function exit - make sure you set the object references to nothing as shown below. FunctionExit: Set objXLws = Nothing Set objXLApp = Nothing End Sub
Comment