Is there an alternative method to using 15 If statements? - vb.net

I have 15 items in my ComboBox and when the user selects an item I want to present something different in my TextBox.
At the moment I have:
If cb_dropdown.SelectedIndex = 0 Then
RTB_Sql.Text = "update access
set accessdesc = 'Less than 5' where accessID < '5'"
Else
If cb_dropdown.SelectedIndex = 1 Then
RTB_Sql.Text = "update access
set accessdesc = 'More than 5' where accessID > '5' and < '10' "
Else
If cb_dropdown.SelectedIndex = 2 Then
RTB_Sql.Text = ""
etc....
Is there a nicer and more methodical way to approach this as it looks quite scruffy?

Yes, it is called select.
Select Case cb_dropdown.SelectedIndex
Case 0 To 4
RTB_Sql.Text = "update access
set accessdesc = 'Less than 5' where accessID < '5'"
Case 5
RTB_Sql.Text = [...]
Case Else
RTB_Sql.Text = [...]
End Case
Although in your case I think what you are looking for is < (less than) and > (greater than).
If cb_dropdown.SelectedIndex < 5 Then
RTB_Sql.Text = "update access set accessdesc = 'Less than 5' where accessID < '5'"
ElseIf cb_dropdown.SelectedIndex < 10 Then
RTB_Sql.Text = "update access set accessdesc = 'More than 5' where accessID > '5' and < '10' "
End If
Not really sure what you are trying to do though. Maybe if you explain in more detail someone can provide a better answer. So let me take a wild guess:
Dim n As Integer = cb_dropdown.SelectedIndex * 5
RTB_Sql.Text = "update access set accessdesc = 'More than " + n + "' where accessID > '" + n + "' and < '" + (n+6) + "' "
This will give you the following result based on SelectedIndex:
0 = from 1 to including 5
1 = from 6 to including 10
etc...
If you want to shift it down one (include 0 and not 5 in first batch) then just change > to >= and 6 to 5.

You can also use something like this if you want to check range of values:
Sub Main()
Dim i As Integer = 6
Select Case True
Case 0 <= i AndAlso i < 5
Console.WriteLine("i is between 0 and 4")
Case 6 <= i
Console.WriteLine("i is 6 or greater")
End Select
Console.ReadLine()
End Sub
Be aware that it stops in the first true condition.

Related

SQL - JOIN with condition CASE - how to make - if condition is met stop to meet next one

Cashier can input to field C40_DATA 3 different values (9962**********, id_invoice, web_order)
what I would like to have as result:
if substr from C40_DATA contains p.id_invoice => show these lines
if C40_contains contains id_web_order or id_incvoice => show these lines
-> it means point 1 shows always but point 2 -> do not show both, if exist id_web_order, show it and stop, if it not exists find id_incvoice
When I create this (below), result contains all 3 together, but I need 1 + (if exist 2 and if not find 3)
select * from web_data p
inner join POS_DATA re on
(case
when substr(re.C40_DATA,5,8) = p.id_invoice and substr(re.C40_DATA,1,4) ='9962' then 1 else
(case
when re.C40_DATA = p.id_web_order and p.d_mnozstvi < '0' then 1 else
(case
when re.C40_DATA = p.id_invoice and p.d_mnozstvi < '0' then 1 else 0 END)
END)
END=1)
Could you please help how to make contitions in JOINs which do - if one condition is met stop and do not find the next one?
Thank you,
Your attempted query would have been better written this way:
select * from web_data p
inner join POS_DATA re on
substr(re.C40_DATA, 5, 8) = p.id_invoice and substr(re.C40_DATA, 1, 4) = '9962'
or re.C40_DATA = p.id_web_order and p.d_mnozstvi < '0'
or re.C40_DATA = p.id_invoice and p.d_mnozstvi < '0'
Had you really needed to do this as a case expression it could have been done as below. The cases fall through in order:
case
when substr(re.C40_DATA, 5, 8) = p.id_invoice and substr(re.C40_DATA, 1, 4) ='9962' then 1
when re.C40_DATA = p.id_web_order and p.d_mnozstvi < '0' then 1
when re.C40_DATA = p.id_invoice and p.d_mnozstvi < '0' then 1
end = 1
As far as matching only on a single condition, I think you want something like this:
with data as (
select *,
case
when re.C40_DATA = '9962' + p.id_invoice then 1
when re.C40_DATA = p.id_web_order and p.d_mnozstvi < '0' then 2
when re.C40_DATA = p.id_invoice and p.d_mnozstvi < '0' then 3
end as match_rank,
min(case
when re.C40_DATA = '9962' + p.id_invoice then 1
when re.C40_DATA = p.id_web_order and p.d_mnozstvi < '0' then 2
when re.C40_DATA = p.id_invoice and p.d_mnozstvi < '0' then 3
) over (partition by p.id_invoice, p.web_order) as min_match_rank
from web_data p
inner join POS_DATA re on
re.C40_DATA = '9962' + p.id_invoice
or re.C40_DATA = (p.id_web_order, p.id_invoice) and p.d_mnozstvi < '0'
)
select * from data where match_rank = min_match_rank;
I've taken the liberty of rewriting some of your conditions in case those options are useful to you. Also, I'm not sure what < '0' is supposed to mean. Finally, I'm not sure which values to be partitioning over because I don't understand the relationship between id_invoice and id_web_order.

How can I reduce the value of a column in my table but not allow it to go below zero?

I have this SQL that reduces a score by 1
db2.Execute("UPDATE Score " +
"SET EnglishCorrect = CASE WHEN EnglishCorrect > 0 THEN (EnglishCorrect -1) ELSE 0 END, " +
"KanaCorrect = CASE WHEN KanaCorrect > 0 THEN (KanaCorrect -1) ELSE 0 END, " +
"RomajiCorrect = CASE WHEN RomajiCorrect > 0 THEN (RomajiCorrect -1) ELSE 0 END, " +
"KanjiCorrect = CASE WHEN KanjiCorrect > 0 THEN (KanjiCorrect -1) ELSE 0 END");
I would now like to change this so that the score is reduce by a variable number. The number will be stored in a C# integer called pts.
So I am will change the -1 into "- " + pts + ")"
But I have a problem as the values of EnglishCorrect, KanaCorrect, RomajiCorrect and KanjiCorrect cannot be less than zero.
Can someone suggest how I can make it so that points are reduced to zero and nothing less.
So for example if EnglishCorrect is 5 and pts is 6 then I want EnglishCorrect to change to 0.
"SET EnglishCorrect = CASE WHEN (EnglishCorrect -pts)>= 0 THEN (EnglishCorrect -pts) ELSE 0 END, " +
"KanaCorrect = CASE WHEN KanaCorrect -pts >= 0 THEN (KanaCorrect -pts) ELSE 0 END, " +
"RomajiCorrect = CASE WHEN RomajiCorrect-pts >= 0 THEN (RomajiCorrect -pts) ELSE 0 END, " +
"KanjiCorrect = CASE WHEN KanjiCorrect-pts >= 0 THEN (KanjiCorrect -pts) ELSE 0 END");
Hope this helps.

VBA - Check if recordset has been deleted before updating DB

I'm experimenting with VBA and i made the following thingy.
Dim rs1 As Recordset
Set rs1 = CurrentDb.OpenRecordset("TBL_Registratie")
With rs1
.AddNew
!leerling_Id = Me.leerling_Id
datum = DateValue(!datum_tijd)
tijd = TimeValue(!datum_tijd)
weekDag = Weekday(datum, vbMonday)
Select Case weekDag
Case 1, 2, 4
Select Case tijd
Case "07:00:00" To "08:00:00"
!score = !score + 1
Case "16:00" To "16:30"
!score = !score + 1
Case "16:31" To "17:00"
!score = !score + 2
Case "17:01" To "22:00"
!score = !score + 3
Case Else
Me.txt_resultaat.Caption = "Het is geen opvang"
Me.txt_resultaat.Visible = True
( rs1.close ? )
End Select
other cases
.Update
.Close
QUESTION: How can i check if my last record in there recordset has a score added or not? if not, i want to delete it and close Else Update & Close
A much easier way: Calculate the score before adding the record, save all needed data in variables instead of recordset fields.
Then only if score > 0 do the rs1.AddNew etc.

Update a column by comparing two different columns in the same table

I have a table TEST with columns VALUE,VALUE_SIM,SIM_STATUS,ID. I want to update the column SIM_STATUS for the ID = 288. I also want to display the columns after updating.
The conditions are :
1. SIM_STATUS = 0 when VALUE = VALUE_SIM.
2. SIM_STATUS = 1 when VALUE < VALUE_SIM.
3. SIM_STATUS = 2 when VALUE > VALUE_SIM.
I wrote the following query but it is showing an error.
("UPDATE TEST"
"SET SIM_STATE = ( CASE WHEN VALUE = VALUE_SIM THEN SIM_STATE = 0 END )"
"SET SIM_STATE = ( CASE WHEN VALUE < VALUE_SIM THEN SIM_STATE = 1 END )"
"SET SIM_STATE = ( CASE WHEN VALUE > VALUE_SIM THEN SIM_STATE = 2 END )"
"where ID = 288 ");
The query that you want is:
UPDATE TEST
SET SIM_STATE = (CASE WHEN VALUE = VALUE_SIM THEN 0
WHEN VALUE < VALUE_SIM THEN 1
WHEN VALUE > VALUE_SIM = 2
END)
WHERE NUMBER = 288;
Your query has several syntax errors. I don't even know if you intend for the double quotes to be part of the query.
This would do i guess
UPDATE TEST
SET
SIM_STATE =
CASE WHEN VALUE < VALUE_SIM THEN 1
+ CASE WHEN VALUE > VALUE_SIM THEN 2
+ CASE WHEN VALUE = VALUE_SIM THEN 0
WHERE ID = 1

visual basics equal to or greater than

I am trying to write some code that says if textbox1 is equal to 0 between 10 then HandDecimal = 1. Else if textbox1 is equal to 10.1 through 100, then HandDecimal = 3. Else if textbox1 is equal to 100.1 and greater than HandDecimal = 5.
Here is my code, but it does not seem to work for me.
If WeightDecimal = 0 <= 10 Then
HandDecimal = 1
ElseIf WeightTextBox.Text = 10 <= 100 Then
HandDecimal = 3
ElseIf WeightTextBox.Text >= 100.1 Then
HandDecimal = 5
End If
How do I have to change the code to make it work?
Dim weight as Decimal = Decimal.Parse(WeightTextBox.Text)
If weight >= 0 AndAlso weight <= 10 Then
HandDecimal = 1
ElseIf weight > 10 AndAlso weight <= 100 Then
HandDecimal = 3
ElseIf weight > 100 Then
HandDecimal = 5
End If
Select Case statement with To operator
Select Case WeightDecimal
Case 0 To 10
HandDecimal = 1
Case 10.1 To 100
HandDecimal = 3
Case Else
HandDecimal = 5
End Select