ZKHADI
Member
- Local time
- Today, 21:16
- Joined
- Apr 5, 2021
- Messages
- 118
Dim xFld as Access.Field
Dim strSQLSel as String, Dim strSQLWhere as String, Dim strField as String
strSQLSel = "SELECT "
strSQLWhere = "WHERE "
For Each xFld In StudentDetailsTable.Fields
strField = xFld.Name
strSQLSel = strSQLSel & "[" & xFld & "], "
strSQLWhere = strSQLWhere & "[" & xFld & "] LIKE '*" & Forms!StudentRecord!txtSearch & "*' AND "
Next xctl
strSQLSel = Left( strSQLSel, Len(strSQLSel) - 2 )
strSQLWhere = Left( strSQLWhere, Len( strSQLWhere ) - 4 )
strSQLSel = strSQLSel & " FROM StudentDetailsTable " & strSQLWhere & " ;"
[
Hmm, maybe. You could try adding a calculated column where you concatenate all the columns with an unusual (read: non-typical) delimiter and then just apply your criteria, with the same delimiters, just to that calculated column.hy buddies!
check below image i put the code in every column of query for multiple search.
this look not so good but it working. is there any short code to filter every field in query.
Something error......Short code? No, not in Access. That is definitely a "long code" event. What you are doing in that query grid is filling in a virtual "table" that Access will use to build SQL to test every field. You have to specify each field for which you want SQL to apply this filter, whatever it is.
You CAN write a loop to build the SQL based on the structure of a table or query.
Code:Dim xFld as Access.Field Dim strSQLSel as String, Dim strSQLWhere as String, Dim strField as String strSQLSel = "SELECT " strSQLWhere = "WHERE " For Each xFld In StudentDetailsTable.Fields strField = xFld.Name strSQLSel = strSQLSel & "[" & xFld & "], " strSQLWhere = strSQLWhere & "[" & xFld & "] LIKE '*" & Forms!StudentRecord!txtSearch & "*' AND " Next xctl strSQLSel = Left( strSQLSel, Len(strSQLSel) - 2 ) strSQLWhere = Left( strSQLWhere, Len( strSQLWhere ) - 4 ) strSQLSel = strSQLSel & " FROM StudentDetailsTable " & strSQLWhere & " ;" [
The two "Left" functions remove an extra comma and space or "AND" and space from the end of the strings you are building. This will create the SQL for you, after which you have to do something with it.
NOTE that if ANY of those fields is not text, you will have to do something special to avoid a run-time error.
not workingNo idea as to why it is saying that.
However you would need a space after the SELECT anyway?
Plus if you are going to try and write code like that, at least put a Debug.Print of the final string somehwre to see what you actually have, not what you think you have.
So stop trying to be clever and build it bit by bit. You have not even copied Doc's code correctly?not working
if i am clever why i am here. and stop abusing me if you cant help. you all my teachers and I respect all of you. and I copied same code to module just change names of table and form but it stuck on select. even I give spaceSo stop trying to be clever and build it bit by bit. You have not even copied Doc's code correctly?
You are using xfld in the loop, when I would have thought you would be using strField? You need to understand code even when you copy anything offered. SO whilst the xfld is not of your creation, the same logic applies?.
You can always put "SELECT " at the start of the final string.
I doubt that code is going to work anyway, as I would have thought you would need OR ? and remove the last one ?
Also post the code within code tags. That way someone can copy and paste and try it for themselves in someway.
Sub testSearch()
Dim xFld As Field, tdf As TableDef
Dim strSQLSel As String, strSQLWhere As String, strField As String
strSQLSel = "SELECT "
strSQLWhere = "WHERE "
Set tdf = CurrentDb.TableDefs("Transactions")
For Each xFld In tdf.Fields
strField = xFld.Name
strSQLSel = strSQLSel & "[" & strField & "], "
Debug.Print "SQL " & strSQLSel
strSQLWhere = strSQLWhere & "[" & strField & "] LIKE '*" & Forms!StudentRecord!txtSearch & "*' AND "
Debug.Print "WHERE " & strSQLWhere
Next xFld
strSQLSel = Left(strSQLSel, Len(strSQLSel) - 2)
strSQLWhere = Left(strSQLWhere, Len(strSQLWhere) - 4)
strSQLSel = strSQLSel & " FROM StudentDetailsTable " & strSQLWhere & " ;"
End Sub
Sub testSearch()
Dim xFld As Field, tdf As TableDef
Dim strSQLSel As String, strSQLWhere As String, strField As String, strCriteria As String
strSQLSel = "SELECT "
strSQLWhere = "WHERE "
'strCriteria = Forms!StudentRecord!txtSearch
strCriteria = "Test"
For Each tdf In CurrentDb.TableDefs
If tdf.Name = "Transactions" Then
For Each xFld In tdf.Fields
strField = xFld.Name
strSQLSel = strSQLSel & "[" & strField & "], "
Debug.Print "SQL " & strSQLSel
strSQLWhere = strSQLWhere & "[" & strField & "] LIKE '*" & strCriteria & "*' AND "
Debug.Print "WHERE " & strSQLWhere
Next xFld
strSQLSel = Left(strSQLSel, Len(strSQLSel) - 2)
strSQLWhere = Left(strSQLWhere, Len(strSQLWhere) - 4)
strSQLSel = strSQLSel & " FROM StudentDetailsTable " & strSQLWhere & " ;"
Exit For
End If
Next
End Sub
SQL SELECT [ID],
WHERE WHERE [ID] LIKE '*Test*' AND
SQL SELECT [ID], [TransactionDate],
WHERE WHERE [ID] LIKE '*Test*' AND [TransactionDate] LIKE '*Test*' AND
SQL SELECT [ID], [TransactionDate], [Account],
WHERE WHERE [ID] LIKE '*Test*' AND [TransactionDate] LIKE '*Test*' AND [Account] LIKE '*Test*' AND
SQL SELECT [ID], [TransactionDate], [Account], [Num],
WHERE WHERE [ID] LIKE '*Test*' AND [TransactionDate] LIKE '*Test*' AND [Account] LIKE '*Test*' AND [Num] LIKE '*Test*' AND
SQL SELECT [ID], [TransactionDate], [Account], [Num], [Description],
WHERE WHERE [ID] LIKE '*Test*' AND [TransactionDate] LIKE '*Test*' AND [Account] LIKE '*Test*' AND [Num] LIKE '*Test*' AND [Description] LIKE '*Test*' AND
SQL SELECT [ID], [TransactionDate], [Account], [Num], [Description], [Memo],
WHERE WHERE [ID] LIKE '*Test*' AND [TransactionDate] LIKE '*Test*' AND [Account] LIKE '*Test*' AND [Num] LIKE '*Test*' AND [Description] LIKE '*Test*' AND [Memo] LIKE '*Test*' AND
SQL SELECT [ID], [TransactionDate], [Account], [Num], [Description], [Memo], [Category],
WHERE WHERE [ID] LIKE '*Test*' AND [TransactionDate] LIKE '*Test*' AND [Account] LIKE '*Test*' AND [Num] LIKE '*Test*' AND [Description] LIKE '*Test*' AND [Memo] LIKE '*Test*' AND [Category] LIKE '*Test*' AND
SQL SELECT [ID], [TransactionDate], [Account], [Num], [Description], [Memo], [Category], [Clr],
WHERE WHERE [ID] LIKE '*Test*' AND [TransactionDate] LIKE '*Test*' AND [Account] LIKE '*Test*' AND [Num] LIKE '*Test*' AND [Description] LIKE '*Test*' AND [Memo] LIKE '*Test*' AND [Category] LIKE '*Test*' AND [Clr] LIKE '*Test*' AND
SQL SELECT [ID], [TransactionDate], [Account], [Num], [Description], [Memo], [Category], [Clr], [Amount],
WHERE WHERE [ID] LIKE '*Test*' AND [TransactionDate] LIKE '*Test*' AND [Account] LIKE '*Test*' AND [Num] LIKE '*Test*' AND [Description] LIKE '*Test*' AND [Memo] LIKE '*Test*' AND [Category] LIKE '*Test*' AND [Clr] LIKE '*Test*' AND [Amount] LIKE '*Test*' AND
@Gasman I think OP needs OR not AND.strSQLWhere = strSQLWhere & "[" & strField & "] LIKE '*" & strCriteria & "*' AND "
@The_Doc_Man I wonder why this works:The reason is that LIKE doesn't work on any field that is numeric or that is derived from a numeric datatype (e.g. DATE).
SELECT tblMachines.MachinePK, tblMachines.MachineName,
tblMachines.ShowOrder, tblMachines.ClassFK
FROM tblMachines
WHERE (((tblMachines.ShowOrder) Like '*1*'));