My table table1 has 3 fields: FName, LName, Phone. I am using Microsoft Access 2010 for running the SQL query. Some rows has empty / null phone values.
I have a VB form which accepts search parameters. A user can enter (FName and LName) or (Phone), but not both at the same time.
When I try:
SELECT table1.LName, table1.FName, table1.Phone
FROM table1
WHERE table1.LName Like ('*' & Forms!frmSearchMain!LName & '*') AND table1.FName Like ('*' & Forms!frmSearchMain!FName & '*')
ORDER BY table1.LName, table1.FName;
It gives me a list of user matching the given (FName and LName) parameters. It works fine.
Similarly, when I try:
SELECT table1.LName, table1.FName, table1.Phone
FROM table1
WHERE table1.Phone Like ('*' & Forms!frmSearchMain!Phone & '*')
ORDER BY table1.LName, table1.FName;
It gives me a list of user matching the given (Phone) parameter. It works fine too.
But, when I combine both these queries:
SELECT table1.LName, table1.FName, table1.Phone
FROM table1
WHERE table1.Phone Like ('*' & Forms!frmSearchMain!Phone & '*') AND table1.LName Like ('*' & Forms!frmSearchMain!LName & '*') AND table1.FName Like ('*' & Forms!frmSearchMain!FName & '*')
ORDER BY table1.LName, table1.FName;
It doesn’t give me the expected results.
I even tried OR condition between (FName and LName) and (Phone) search parameters. It doesn’t work. I have tried nz(frmSearchMain!Phone,"") for all the search params, % instead of *, but no luck so far.
I am new to the MS Access SQL Query format. I have used this type of queries a lot of times in MySQL. But I couldn’t get the expected results from this one.
Any help would be appreciated.
I think it should be easier to sort this one out if we simplify it. So I arbitrarily decided to ignore
LNamefor now, and do the searching based only onFNameorPhone.In my version of
frmSearchMain, I named the text box which holds the search target value forFNameastxtSearchFNamesimply because I prefer to name the control differently than the record source field. Similarly, I chosetxtSearchPhonefor thePhonesearch target text box.Since I already had a table named
Table1, I called minetblKeerthiraminstead.With those changes, this query gives me rows which match
txtSearchFNameortxtSearchPhone. If bothtxtSearchFNameandtxtSearchPhoneare Null, the query return all rows from the table … which is what I hope you want.A consequence of this approach is that, if both
txtSearchFNameandtxtSearchPhoneare non-Null, the query will return only the rows which match both. As I understood your description, that is not what you want. You want to search by only one or the other, not both at the same time.In that case I suggest you use the after update events for the two search text boxes to set the other Null when a non-Null value has been entered into one of them. I’m unsure how clear that sentence was, so just add this code to your form’s module. It will ensure that only one of the two text boxes will contain a non-Null value.
Finally if that all works, then you only need to revise it to deal with
LName. Hope that part won’t be too daunting. Good luck.