How can I show the results from my keyword search without change the RecordSource to my sql string? Reason is, if I change the RecordSource, the standard filter from the datasheet changes the tablename to the subform
Ok here is my sql query from my 7 tables:
SELECT table1.*, table2.*, table3.*, table4.*, table5.*, table6.*, table7.* FROM table1 LEFT JOIN (((table2 LEFT JOIN table3 ON table2.ID = table3.ID) LEFT JOIN table7 ON table3.ID = table7.ID) LEFT JOIN ((table4 LEFT JOIN table5 ON table4.ID = table5.ID) LEFT JOIN table6 ON table5.ID = table6.ID) ON table7.ID = table4.ID) ON table1.ID = table2.test;
here my search button:
Private Sub btnSearch_Click()
Dim sql As String
Dim query As QueryDef
' sql query for search with keywords
sql = " SELECT * FROM qryData WHERE " & BuildFIlter
'set the new sql as RecordSource
Me.sfrmSearch.Form.RecordSource = sql
'Delete the query if it already exists
On Error Resume Next
DoCmd.DeleteObject acQuery, "qryExport"
'Set the query and save it in current database
Set query = CurrentDb.CreateQueryDef("qryExport", sql)
End Sub
Private Function BuildFIlter() As Variant
Dim varWhere As Variant
varWhere = Null 'Main Filter
' Check for LIKE test_Name
If Me.txtKeywords > "" Then
varWhere = varWhere & " [test_name] LIKE '*" & Me.txtKeywords & "*' "
End If
' Check if there is a filter to return...
If IsNull(varWhere) Then
varWhere = ""
End If
BuildFIlter = varWhere
End Function
now my export button:
Private Sub btnExport_Click()
Dim filterSQL As String
Dim sql As String
Dim query As QueryDef
Dim x As Integer
filterSQL = " SELECT * FROM qryData "
If Me.sfrmSearch.Form.OrderBy <> vbNullString And Me.sfrmSearch.Form.OrderByOn = True And Me.sfrmSearch.Form.Filter <> vbNullString And Me.sfrmSearch.Form.FilterOn = True Then
filterSQL = filterSQL & " WHERE " & Me.sfrmSearch.Form.Filter & " ORDER BY " & Me.sfrmSearch.Form.OrderBy
If Me.txtKeywords > "" Then
filterSQL = filterSQL & " WHERE " & Me.sfrmSearch.Form.Filter & " AND " & "(" & BuildFIlter & ")" & " ORDER BY " & Me.sfrmSearch.Form.OrderBy
End If
x = 1
End If
If Me.sfrmSearch.Form.Filter <> vbNullString And Me.sfrmSearch.Form.FilterOn = True And Me.sfrmSearch.Form.OrderByOn = False Then
filterSQL = filterSQL & " WHERE " & Me.sfrmSearch.Form.Filter
If Me.txtKeywords > "" Then
filterSQL = filterSQL & " AND " & "(" & BuildFIlter & ")"
End If
x = 1
MsgBox (filterSQL)
End If
If Me.sfrmSearch.Form.OrderBy <> vbNullString And Me.sfrmSearch.Form.OrderByOn = True And Me.sfrmSearch.Form.FilterOn = False Then
If Me.txtKeywords > "" Then
filterSQL = filterSQL & " WHERE " & BuildFIlter & " Order By " & Me.sfrmSearch.Form.OrderBy
Else
filterSQL = filterSQL & " Order By " & Me.sfrmSearch.Form.OrderBy
End If
x = 1
End If
If x = 1 Then
On Error Resume Next
'Delete the query if it already exists
DoCmd.DeleteObject acQuery, "qryExport"
'Set the query and save it in current database
Set query = CurrentDb.CreateQueryDef("qryExport", filterSQL)
Else
Call btnSearch_Click
End If
DoCmd.OutputTo acOutputQuery, "qryExport", "Excel Workbook (*.xlsx)", , , , , acExportQualityPrint
End Sub
i can only set filter in one column all other values are not available and if i set it by one column and export it access ask for parameter value (access puts the subform name as table name and not the queryname as tablename so the columns are not available)
i must set up the recordsource to the sql because i want so see my filtered data drom keywords in my subform
a few of tables
: provide table structure.One query
: provide query SQL,if i use my keyword search
: how exactly are you searching.i must set a new recordsource
: Why?i cant set up the filters
: Why?. Answer all these questions properly in a readable format, and I will try to answer your question. – Erik A