How to search all records on one field in sql | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to search all records on one field in sql

Hi all I want filter on 2 field but for field1 get input by textbox and FIELD2 search on ALL RECORDS but by where on 2 fields : "Select * from tbl where field1="& textbox1.text &" and FIELD2=* " But by * it not work Please skip syntax4 error

25th Jun 2020, 3:34 AM
Hadi
Hadi - avatar
7 Answers
+ 1
Just leave it away? SELECT f1, f2 FROM tbl WHERE f1 = "x" [optional: AND f2 != ""] ORDER BY f1, f2;
25th Jun 2020, 5:08 AM
Sandra Meyer
Sandra Meyer - avatar
+ 1
Sandra Meyer Ok Thank you thats right If i want leave check box for two field to select optiinal user to 1 filter or 2 filter in this case what is solution? My Code: field1 =field2 = "*" If (Me.textf1= True) Then field1 = Me.txtfield1 End If If (Me.textf2= True) Then field1 = Me.txtfield2 End If
25th Jun 2020, 7:18 AM
Hadi
Hadi - avatar
+ 1
Then just ask if either field 1 matches OR field 2: SELECT f1, f2 FROM tbl WHERE f1 = " textbox value 1" OR f2 = " textbox value 2 " ORDER BY f1, f2; Result is every record, that matches one condition.
25th Jun 2020, 7:33 AM
Sandra Meyer
Sandra Meyer - avatar
+ 1
in this way if user select two checkbox , any record that is on first field match and on other field no match is will show and return
25th Jun 2020, 10:30 AM
Hadi
Hadi - avatar
+ 1
If you want to change they logic dependent from the input (f1 or f2 or (f1 and f2)), then you either have to implement it (possibly more user friendly) or to add it to your form. Use it as input: SELECT f1, f2 FROM tbl WHERE f1 = " textbox value 1" " operator (AND | OR) " f2 = " textbox value 2 " ORDER BY f1, f2;
25th Jun 2020, 1:04 PM
Sandra Meyer
Sandra Meyer - avatar
0
if user want select only one field1 to filter then above code is working?
25th Jun 2020, 1:26 PM
Hadi
Hadi - avatar
0
You need something like a checkbox... o AND o OR ... and then have to use the label value of the chosen one or map it hard coded (easier). This is how such search applications work on many web pages. Alternatively you can determine by code if both are filled > then use AND, otherwise use OR.
25th Jun 2020, 1:34 PM
Sandra Meyer
Sandra Meyer - avatar