oxicottin
Learning by pecking away....
- Local time
- Today, 13:52
- Joined
- Jun 26, 2007
- Messages
- 883
I have a continuous form that's a search form and I need to export the data/criteria it populates in the detail section. I pieced together what I thought would work but im getting an error 3075 and debug takes me to
Export to Excell button.....
Filter function code used in my form...
Code:
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qry_AdvancedSearch", outputFileName, True
Export to Excell button.....
Code:
Private Sub cmdExportToExcel_Click()
Dim outputFileName As String
Dim XL As Object
Dim strSQL As String
outputFileName = "C:\Documents and Settings\" & Environ("username") & "\Desktop\Production_Export_" & Format(Date, "MM-dd-yyyy") & ".xls"
If Len(Dir$(outputFileName)) > 0 Then
Kill outputFileName
End If
strSQL = Me.RecordSource = "SELECT * FROM qry_AdvancedSearch " & BuildFilter
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qry_AdvancedSearch", outputFileName, True
Set XL = CreateObject("Excel.Application")
XL.Workbooks.Open outputFileName
XL.Visible = False
With XL
.Range("E2:E1000").NumberFormat = "hh:mm" '"h:mm AM/PM" 'specified Time format was showing 1/0/1900
.ErrorCheckingOptions.NumberAsText = False 'Clears the green error arrows
.Range("A1:T1").Font.Bold = True
.Range("A1:T1").Font.Name = "Segoe UI Light"
.Range("A1:T1").Font.Size = 12
.Range("A1:T1").Interior.ColorIndex = 44 'http://dmcritchie.mvps.org/excel/colors.htm
.Range("A2:T1000").Font.Name = "Segoe UI Light"
.Range("A2:T1000").Font.Size = 10
.Columns("A:T").EntireColumn.AutoFit 'Auto fits colums to the largest text
End With
XL.ActiveWorkbook.Save
XL.Application.Quit
Set XL = Nothing
MsgBox "Congrats your data has been uploaded to your desktop in a .xls file", vbInformation, "Upload Complete"
End Sub
Filter function code used in my form...
Code:
Private Function BuildFilter() As Variant
Dim varWhere As Variant
Dim lngLen As Long
varWhere = Null ' Main filter
'*************************************************************************************
'Look at each search box, and build up the criteria string from the non-blank ones.
'*************************************************************************************
'-------------------------------------------------------------------------------------------------------
' Check For Employee
If Not IsNull(Me.cboEmployees) Then
varWhere = varWhere & "[EmployeeID] = " & Me.cboEmployees & " AND "
End If
'-------------------------------------------------------------------------------------------------------
' Check For Product
If Not IsNull(Me.cboProduct) Then
varWhere = varWhere & "[ProductID] = " & Me.cboProduct & " AND "
End If
'-------------------------------------------------------------------------------------------------------
' Check For Length
If Not IsNull(Me.cboLength) Then
varWhere = varWhere & "([Length] = '" & Replace(Me.cboLength, "'", "''") & "') AND "
End If
'-------------------------------------------------------------------------------------------------------
' Check For Machine
If Not IsNull(Me.cboLine) Then
varWhere = varWhere & "[MachineID] = " & Me.cboLine & " AND "
End If
'-------------------------------------------------------------------------------------------------------
'Check for NOT LIKE in Keyword Search
If Me.txtNotInKeyword > "" Then
varWhere = varWhere & "[ProductionProblems] NOT LIKE ""*" & Me.txtNotInKeyword & "*"" AND "
End If
'-------------------------------------------------------------------------------------------------------
'Check for LIKE in Keyword Search
If Me.txtSearchKeyword > "" Then
varWhere = varWhere & "[ProductionProblems] LIKE ""*" & Me.txtSearchKeyword & "*"" AND "
End If
'-------------------------------------------------------------------------------------------------------
' Check For Shift
If Not IsNull(Me.cboShift) Then
varWhere = varWhere & "[ShiftID] = " & Me.cboShift & " AND "
End If
'-------------------------------------------------------------------------------------------------------
' Check if there is a filter to return...
If IsNull(varWhere) Then
varWhere = ""
' msg if no data
'MsgBox "No criteria", vbInformation, "Nothing to do."
Me.FilterOn = True
Else
varWhere = "WHERE " & varWhere
' strip off last "AND" in the filter
If Right(varWhere, 5) = " AND " Then
varWhere = Left(varWhere, Len(varWhere) - 5)
End If
End If
BuildFilter = varWhere
End Function