I have a question for which I found a rather long solution of which I don’t think that it is good practise.
However I feel there must be a syntax available that is able to solve this issue right away without too many complexities.
I bumped into this problem a few times already, and I’d like to know for once and for all.
I have a table called “T_STOP_LOSS”, in which I have three columns “LIVES_FROM”, “LIVES_TO” and “SL_VALUE” (All number types + the column containing ID with AutoNumber type).
I want to retrieve the record containing the SL_VALUE where a variable value lies in the interval between LIVES_FROM and LIVES_TO.
ID LIVES_FROM LIVES_TO NR_COUNTRIES_FROM NR_COUNTRIES_TO SL_VALUE
1 0 999 2 2 39,3
12 0 999 3 3 34,6
23 0 999 4 4 29,6
34 0 999 5 9 25,7
45 0 999 10 100 22,17
46 1000 1499 2 2 31,1
2 1000 1499 3 3 27,4
13 1000 1499 4 4 23,3
24 1000 1499 5 9 20,4
35 1000 1499 10 100 17,5
36 1500 1999 2 2 23,6
47 1500 1999 3 3 20,7
3 1500 1999 4 4 17,7
14 1500 1999 5 9 15,4
25 1500 1999 10 100 13,3
26 2000 2999 2 2 23,6
37 2000 2999 3 3 20,7
48 2000 2999 4 4 17,7
4 2000 2999 5 9 15,4
15 2000 2999 10 100 13,3
Normally I would use:
lNr_Lives = <Something> '(Retrieved via Textbox)
sSQL_Select = "SELECT SLVALUE FROM T_STOP_LOSS WHERE LIVES_FROM <= " & LNr_Lives & " AND LIVES_TO >= " & LNr_Lives & ";"
However, LIVES_FROM and LIVES_TO are related to the table NR_LIVES through a foreign key.
The above statement will never work because table ‘NR_LIVES’ contains an ID field as Primary Key, which is used to link to the T_STOP_LOSS table.
(The columns in NR_LIVES are named: ID, NR_LIVES_FROM and NR_LIVES_TO (not that it matters)).
ID NR_LIVES_FROM NR_LIVES_TO
1 0 999
2 1000 1499
3 1500 1999
4 2000 2999
Please tell me, what would you consider to be the briefest way to access the real value of LIVES_FROM and LIVES_TO that is stored in the NR_LIVES table and get my SELECT statement working the way I expect it (Return the record that contains the SL for the right interval).
I hope that my question is clear. If not, don’t hesitate to mention.
Thanks to Nicolas, I found what I needed.