Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15213

Constructing SELECT statement based on user selected options

$
0
0
I'm having trouble constructing some SELECT statements based on options selected by user at runtime. More to the point how the AND's and OR's must work.

The search criteria will be based on the following fields:
AvNum (text) that can either have no text (empty string - no NULL string) or some text e.g. A01.
AvYear (text) that will have a year in text form e.g. 2019 or 2020.
AvTests (memo) that can either have no text or a hell of a long string (derived from a LynxGrid control that saves the cells into this long string seperating cell values with vbTAB and rows with vbCR.

The user can select the options:
All Birds - then AvNum do not have to be set as all birds need to be included for search.
Only Birds In Aviaries - then AvNum will have a value so AvNum > "" so only birds with a AvNum will be searched.

Specific Year - If no year is entered then AvYear do not have to be set. If a year is entered then AvYear = cValue so only birds with that year will be searched.

Include Tests and Exclude Tests - Is where it gets tricky. There are two lists that the user can select tests from. One for tests to Include in search and one for tests to Exclude in search. Both listboxes contain the same tests but a test can never be selected in both boxes. However, multiple tests can be selected in each listbox. Furthermore the user can select to include ALL (AND?) the selected tests in the lists or ANY (OR?) of the selected tests in the different lists.

Name:  Options.png
Views: 71
Size:  18.8 KB

So based on the selections made in the screen capture, how would the SELECT statement be constructed?

Possibilities where Must Include and Must Exclude could be selected as ALL or ANY:
Include ALL of the selected tests - select record only if all the selected tests are in AvTests
Include ANY of the selected tests - select record if any of the selected tests are in AvTests
Exclude ALL of the selected tests - select record if all of the selected tests are not in AvTests
Exclude ANY of the selected tests - select record if any of the selected tests ane not in AvTests

E.g. in words: Select all from Birds WHERE AvNum > '' AND AvYear = '2020' AND/OR AvTests LIKE '*PBFD Virus*' AND/OR AvTests LIKE '*Necropsy*' AND/OR AvTests NOT LIKE '*Circovirus*' AND/OR AvTests NOT LIKE 'Avian Polyoma Virus*'

I hope I'm making at least some sense here:-)
Attached Images
 

Viewing all articles
Browse latest Browse all 15213

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>