SQL SELECT Query on related table - ms-access

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.

Looks to me like you want to do some simple join queries. First check out these references here and here to get you more familiar with the approach.
Secondly, there is generally no reason to have the extra 'Lookup' columns in the child tables as these values can be accessed from an appropriate JOIN query.
I suspect the query you are looking for is:
SELECT SLVALUE FROM
T_STOP_LOSS
INNER JOIN
NR_LIVES
ON T_STOP_LOSS.ID=NR_LIVES.ID
WHERE
NR_LIVES_FROM <= LNr_Lives
AND
NR_LIVES_TO >= LNr_Lives ;

Thanks to Nicolas, I found what I needed.
Dim cQueries As clsQueries
Dim oRst As DAO.Recordset
Dim sSQL_Select As String
Dim LNr_Lives As Long
Dim iNr_Countries As Integer
Dim i As Integer
If Not IsNull(Me.txt_Nr_Lives) Then
LNr_Lives = Me.txt_Nr_Lives
Else
MsgBox "No number of lives defined!", vbCritical
Exit Sub
End If
If Not IsNull(Me.cbb_Nr_Countries) Then
iNr_Countries = Me.cbb_Nr_Countries
Else
MsgBox "No number of countries defined!", vbCritical
Exit Sub
End If
Set cQueries = New clsQueries
sSQL_Select = "SELECT " & _
"* " & _
"FROM " & _
"T_STOP_LOSS " & _
"INNER JOIN " & _
"NR_LIVES ON (T_STOP_LOSS.LIVES_FROM = NR_LIVES.ID) " & _
" WHERE " & _
"NR_LIVES.NR_LIVES_FROM <= " & LNr_Lives & _
" AND " & _
"NR_LIVES.NR_LIVES_TO >= " & LNr_Lives & _
" AND " & _
"T_STOP_LOSS.NR_COUNTRIES_FROM <= " & iNr_Countries & _
" AND " & _
"T_STOP_LOSS.NR_COUNTRIES_TO >= " & iNr_Countries & ";"
Set oRst = cQueries.Select_Statement(sSQL_Select)
oRst.MoveFirst
Do While Not oRst.EOF
Me.txt_FSL = oRst![SL_VALUE]
oRst.MoveNext
Loop

Related

Clear Columns with Button vba

This appears easy but has crushed me into posting this: I need to clear 5 columns with a button click. That's it. I do not need to delete the columns because they are used in a sum calculation. I just need to clear the values greater than zero.
Table Name is Customer
Column 1 = Week 1
Column 2 = Week 2
ETC
Here is my garbage code that does not run. Any help would be a great gesture this Holiday season. I am using Access 2016.
Private Sub Command80_Click()
Dim SQL As String
SQL = "UPDATE Customer " & _
"SET Week1 = NULL " & _
"WHERE Week 1 > 0"
DoCmd.RunSQL SQL
End Sub
If the field name to be updated is Week1 (and not Week 1) then this is the code:
Private Sub Command80_Click()
Dim sql As String
sql = "UPDATE Customer " & _
"SET Week1 = NULL " & _
"WHERE Week1 > 0"
DoCmd.RunSQL sql
End Sub
You've used Week 1 (with a blank) in your code.
If "Week 1" is the actual field name, wrap it in brackets:
"SET [Week 1] = NULL " & _
"WHERE [Week 1] > 0"

How to use sum function based on other columns in MySQL

This is my table:
SubjectCode | Unit | Instructor
IS 26 3 James
IS 26 3 James
The result should be:
SubjectCode | Unit | Instructor
IS 26 3 James
My code:
msql = "SELECT Sum(Unit) AS Unit" & _
" From classprogram " & _
" WHERE Instructor= '" & cboInstructor.Text & "'and Semester='" & cboSemester.Text & "'" & _
" GROUP BY CONCAT_WS(' ', Instructor, SubjectCode);"
I want the answer of that is 3.
How can I proceed further?

Need help to update a sql table from excel sheet using vb script

Thank you in advance.
My aim is to update sql table from excel. So i wrote a vbscript to get sql table from my-sql to excel on a single click. Table looks like this:-
id rule length isactive date
1 rule 1 3 1 NULL
2 rule 2 5 0 NULL
3 rule 3 6 1 NULL
4 rule 4 7 1 8/10/2015
5 rule 5 3 0 NULL
Now i will update length, isactive, date fields and this data should be updated in the table on a single click. Please help me in this script. I tried update query which is failing with run time error-
80040e14.
part of script is
For iCount = 2 To 6
intRLength = CInt(objSheet.Cells(iCount, 3).Value)
intID = CInt(objSheet.Cells(iCount, 1).Value)
strQuerie = "UPDATE" & strDatabase & "." & strTable & "SET retentionlength=" & intRLenth & "where id= " & intID
rs.Open strQuerie ' --- Getting run time error- 80040e14 message here
Next
That's most probably because of spacing issue in some part of your formed query
Your query should rather look like
strQuerie = "UPDATE " & strDatabase & "." & strTable & " SET retentionlength=" & intRLenth & " where id= " & intID
Otherwise, as it stands; your query would look like below which clearly way wrong
UPDATEDB_NAME.TABLE_NAMESET retentionlength=somevaluewhere id= someid

VB.NET adding Rows and columns

I'm after a way to add multiple amounts in different columns in a certain date_code, i've used the select sum to add amounts in a single column, but i have no an idea on how to add multiple column rows up, if you get what I mean, my sql database is as follows:
Date amount1 amount2 amount3 amount4 amount5
02/10/2014 2 3 6 0 5
02/10/2014 3 1 2 0 3
02/10/2014 0 4 6 6 2
02/10/2014 2 0 9 3 6
02/10/2014 7 0 8 2 9
Im need the total of all amounts in the same date range.
Total would be: 71
Any help would be great,
Many Thanks,
Pete
This is the code i've got so far which adds the amount, but only in one column:
connect() cmd.CommandText = "SELECT SUM(amount1) FROM sqlccmbuildtimecount WHERE datevar = '" & temp_day & "/" & temp_month & "/" & temp_year & "'"
endvalue.Text = cmd.ExecuteScalar() disconnect()
i've set temp_day to "02", temp_month to "10" and temp_year to "2014"
Sum is not 71, should be 89
Like the_lotus said,
do:
SELECT SUM(amount1) + SUM(amount2) + SUM(amount3) + SUM(amount4) + SUM(amount5) FROM sqlccmbuildtimecount WHERE datevar = '" & temp_day & "/" & temp_month & "/" & temp_year & "'"

Run a for Loop in Access Query

I have a Query that generates a list of 2% finance or service charges on completed orders whose balance is overdue this works fine. I then have a second query that totals the finance charges per customer (this bit also works fine) what I would like to get in that second totals query is a comma (or semicolon or space or pipe or ...) separated list of orders in another field
so for the first query I have:
CustomerID OrderID ContactName FinanceChargeAmmount
218 31901 Joe Schmoe Construction 23.43
218 31927 Joe Schmoe Construction 15.78
218 31929 Joe Schmoe Construction 8.91
231 33403 Billy Bob Construction 0.43
258 33369 XYZ Corp 0.77
258 33546 XYZ Corp 1.23
and the second I have:
CustomerID ContactName SumOfFinanceChargeAmmount
218 Joe Schmoe Construction 48.12
231 Billy Bob Construction 0.43
258 XYZ Corp 2.00
I would like to add another column to the second table like:
CustomerID ContactName SumOfFinanceChargeAmmount Orders
218 Joe Schmoe Construction 48.12 31901, 31927, 31929
231 Billy Bob Construction 0.43 33403
258 XYZ Corp 2.00 33369, 33546
but can't figure out a way to loop in the query or get a list of the summed values and while I understand that I should be able to do this in VBA I'm trying to avoid doing so if possible
I know you said you want to avoid VBA, but I don't see another way to do it. So here is a proposal:
public function concatOrders(customerId as Integer) as String
On Error GoTo Oops
dim db as DAO.database, rec as DAO.recordSet
dim ans as String : ans = ""
dim first as Boolean : first = true
dim strSQL = "select orderId from [yourTable] " & _
"where customerID=" & customerId & " order by orderId";
set db = currentDb()
set rec = db.openRecordset(strSQL, dbOpenDynaset, dbReadOnly)
with rec
.moveFirst
do
if first then
first = false
else
strSQL = strSQL & ", "
end if
strSQL = strSQL & !orderId
.moveNext
loop until .EOF
.close
end with
db.close
function_exit:
concatOrders = ans
set rec = Nothing
set db = Nothing
exit function
Oops:
' Handle the errors here
goto function_exit
end function