VB ElseIf Statement Limited to 1 - vb.net

I was forced by college to do a application in Visual Studio (Basic) and I never had expierence with this language before so I'm confused. I did If statement which is working and then I've added EleseIf which is also working but after I've added Second ElseIf it doesn't work. It seems like only If statement and the first ElseIf is working for me but I need more than just 1 ElseIf statement.
Public Class receipt
Private Sub receipt_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim outputValue As Decimal = My.Settings.outputamount.Remove(4)
Dim calculation As Decimal = outputValue * My.Settings.inputamount
Dim totalwithoutcharge As String = calculation.ToString
customername.Text = "Name: " + My.Settings.Username
Label6.Text = "Entered Money: " + My.Settings.inputamount + " " + My.Settings.currency
Label7.Text = "Converted To: " + totalwithoutcharge + " " + My.Settings.outputcurrency
If calculation < 100 Then
Label8.Text = "Charge: 0%"
Label9.Text = "Total: " + totalwithoutcharge + " " + My.Settings.outputcurrency
ElseIf calculation > 100 Then
Label8.Text = "Charge: 1%"
Label9.Text = "Total: " + totalwithoutcharge + " " + My.Settings.outputcurrency
ElseIf calculation > 500 Then
Label8.Text = "Charge: 2%"
Label9.Text = "Total: " + totalwithoutcharge + " " + My.Settings.outputcurrency
ElseIf calculation > 1000 Then
Label8.Text = "Charge: 3%"
Label9.Text = "Total: " + totalwithoutcharge + " " + My.Settings.outputcurrency
Else
Label8.Text = "Something went wrong"
End If
End Sub
End Class
I've entered many values I've add over 600 and it was still showing 'Charge: 1%' where it should show 2% instead. The same happen with values higher than 1000 it will still show the 1%. But when the value is below 100 then is OK and it is showing 0%.
Over 100 works fine too
Over 500 doesn't work
I've also tried to do it with two conditions where it would look at range but it didn't work too.
I've tried 'ElseIf calculation > 100 And calculation > 500 Then' but there is no difference.

First of all use & instead of + when concentating strings.
Second if calculation is > 500 it is also greater than 100, 200 etc. So you need to implement a range. So think about the logic.

Based on my comment
If calculation >= 1000 Then
ElseIf calculation >= 500 Then
ElseIf calculation >= 100 Then
Else
'lass than 100
End If

Related

SQL Syntax error, expression of non-boolean type

This code is supposed to create a graph of revenue from money made through sales tickets at an event.
The code only executes up to da.Fill(ds) when it returns the error, which can be seen at the end of the code.
Does anybody know why
Private Sub frmRevenue_Load(sender As Object, e As EventArgs) Handles Me.Load
frmMDI.addFormToCMS()
Dim dt As DataTable
dt = New DataTable
dt.Columns.Add("Fee")
Dim sales As Integer = 0
Dim gridtable As New DataTable
gridtable.Columns.Add("Month")
gridtable.Columns.Add("Total")
gridtable.Columns.Add("#")
For i = 1 To 12
sql = "SELECT Fee FROM tblTickets WHERE MONTH(DatePurchased) = " & i & " AND (YEAR(DatePurchased) = " & Today.Year & " OR " & Year(Today.AddYears(1)) & ") AND (Status = 'SOLD' OR RESERVED" _
& " = 'AWAITING CONFIRMATION' OR Status = 'AVAILABLE' OR Status = 'AWAITING PAYMENT');"
da = New OleDb.OleDbDataAdapter(sql, con)
ds = New DataSet
da.Fill(ds)
Dim dr As DataRow
For Each dr In ds.Tables(0).Rows
monthly(i) = monthly(i) + 1
contracts = sales + 1
total(i) = total(i) + dr.Item("Fee")
yearlytotal = yearlytotal + dr.Item("Fee")
Next
Next
For i = 1 To 12
Dim month As String
Select Case i
Case 1
month = "Jan"
Case 2
month = "Feb"
Case 3
month = "Mar"
Case 4
month = "Apr"
Case 5
month = "May"
Case 6
month = "Jun"
Case 7
month = "Jul"
Case 8
month = "Aug"
Case 9
month = "Sep"
Case 10
month = "Oct"
Case 11
month = "Nov"
Case 12
month = "Dec"
Case Else
month = "ERR"
End Select
gridtable.Rows.Add(month, FormatCurrency(total(i)), monthly(i))
Next
ugTickets.DataSource = gridtable
ugTickets.DisplayLayout.Bands(0).Columns("Month").Width = 35
ugTickets.DisplayLayout.Bands(0).Columns("#").Width = 20
ugTickets.DisplayLayout.Override.AllowUpdate = DefaultableBoolean.False
txtAnnual.ReadOnly = True
txtAnnual.BackColor = Color.White
txtAnnualContracts.ReadOnly = True
txtAnnualContracts.BackColor = Color.White
chRevenue.Titles("chTitle").Text = "Predicted revenue for " & Today.Year & " - " & Year(Today.AddYears(1))
txtAnnual.Text = FormatCurrency(yearlytotal, 2)
txtAnnualContracts.Text = contracts
chRevenue.Series("Series1").Name = "Revenue"
For i = 1 To 12
chRevenue.Series("Revenue").Points.AddY(total(i))
Next
Try
chRevenue.BackColor = Color.Transparent
chRevenue.Legends("Revenue").BackColor = Color.Transparent
chRevenue.Series("Revenue").ChartArea = "ChartArea1"
chRevenue.Series("Revenue").Color = Color.SkyBlue
chRevenue.Series("Revenue").ToolTip = FormatCurrency("#VALY", 2)
Catch
End Try
End Sub
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
The problem is with this bit of the SQL:
(YEAR(DatePurchased) = " & Today.Year & " OR " & Year(Today.AddYears(1)) & ")
It should probably be
(YEAR(DatePurchased) = " & Today.Year & " OR YEAR(DatePurchased) = " & Year(Today.AddYears(1)) & ")
The OR in SQL (and in most other languages) needs to have two independently valid conditions on each side. The left hand side currently looks like this:
YEAR(DatePurchased) = 2016
...which is fine. But the right looks like this:
2017
...which isn't a valid boolean.
When you get an error like this on the da.Fill() line (ie. the line that's actually running the SQL in the database), the easiest way to debug it is to print out the value of the "sql" variable.
Often, you can just look at the SQL it's generated and the problem will be obvious. Other times you have to copy it and run it directly against your database to see what the problem is.
Might be your SQL, try:
"SELECT Fee FROM tblTickets WHERE MONTH(DatePurchased) = '" & i &
"' AND (YEAR(DatePurchased) = '" & Today.Year &
"' OR '" & Year(Today.AddYears(1)) & "') " &
"AND (Status = 'SOLD' OR RESERVED = 'AWAITING CONFIRMATION' " &
"OR Status = 'AVAILABLE' " &
"OR Status = 'AWAITING PAYMENT');"

VB - comparing numbers in two labels

I'm doing a school project in Visual Basic (using visual studio 2015) and i'm kinda stuck.
My goal is to create a lottery, where player chooses 6 numbers from checkboxes, then he generates six random numbers (1 - 49) and finally, those two sets should be compared and needed result is the number of correctly guessed numbers.
I have both results (guessed numbers, generated numbers) saved in two different labels.
The checkboxes itself are genereted like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lev = 20
tt = 0
For j = 1 To 50
tt = tt + 1
n = n + 1
box(j) = New CheckBox
box(j).Name = "box(" & Str(j) & ")"
If n = 11 Then lev = lev + 110 : n = 1 : tt = 1
box(j).Left = lev
box(j).Parent = Me
box(j).Top = tt * 20
box(j).Tag = j
box(j).Text = j
box(j).Visible = True
Next
box(50).Enabled = False
End Sub
First label (guessed numbers) is filled this way (i'm not posting whole code)
For j = 1 To 50
If box(j).Checked = True Then Label9.Text = Label9.Text + " " + box(j).Text
Next
and the second one (generated numbers) like this:
Do
rn = rg.Next(1, 50)
If Not r.Contains(rn) Then
r.Add(rn)
End If
Loop Until r.Count = 6
Label1.Text = r(0).ToString + " " + r(1).ToString + " " + r(2).ToString + " " + r(3).ToString + " " + r(4).ToString + " " + r(5).ToString
any idea how to compare numbers stored in those labels and get the result (number of correctly guessed numbers).
thanks in advance
You can compare numbers in the labels by splitting the Text properties of the labels into arrays of strings and converting them to integer arrays. First though there is a tiny problem with your code that adds the guessed numbers to the label.
For j = 1 To 50
If box(j).Checked = True Then Label9.Text = Label9.Text + " " + box(j).Text
Next
The " " should be moved to the end of the line because at the moment, the label will always start with a space and that messes with the function below. So you should have -
For j = 1 To 50
If box(j).Checked = True Then Label9.Text = Label9.Text + box(j).Text + " "
Next
Ok. The function below splits the two text labels into their own array and loops through the guesses and checks if any number is contained in the generated numbers. It then returns the number of matches.
Private Function ComparePicks() As Integer
Dim numbersMatched As Integer
Dim picks(5) As Integer
Dim generatedNumbers(5) As Integer
For i As Integer = 0 To 5
picks(i) = CInt(Split(Label9.Text, " "c)(i))
Next
For i As Integer = 0 To 5
generatedNumbers(i) = CInt(Split(Label1.Text, " "c)(i))
Next
For i As Integer = 0 To 5
If generatedNumbers.Contains(picks(i)) Then
numbersMatched += 1
End If
Next
Return numbersMatched
End Function

ElseIf statement wont run the whole code VB

Ok so I've been working on a program I'm making and for some reason when I put a bit of my code into an else if statement, it won't run the statement at all not even the original If statement, However when I put it in a regular If and else statement it works perfectly.
This bit of code works:
If TextBox5.Text > TextBox1.Text & TextBox6.Text > TextBox2.Text Then 'NE
Bearing = Atan(X2 / Y2) * 57.3
Else
Bearing = Atan(Y2 / X2) * 57.3
Bearing = Bearing + -Bearing + -Bearing + 90
If BOFF > 0 Then
Bearing = Bearing - Math.Round(BOFF)
Else
Bearing = Bearing + Math.Round(BOFF)
End If
End If
And this bit of code doesnt work:
If TextBox5.Text > TextBox1.Text & TextBox6.Text > TextBox2.Text Then 'NE
Bearing = Atan(X2 / Y2) * 57.3
ElseIf TextBox5.Text > TextBox1.Text & TextBox6.Text < TextBox2.Text Then
Bearing = Atan(Y2 / X2) * 57.3
Bearing = Bearing + -Bearing + -Bearing + 90
If BOFF > 0 Then
Bearing = Bearing - Math.Round(BOFF)
Else
Bearing = Bearing + Math.Round(BOFF)
End If
End If
Could be a syntax error something, but it doesn't come up with an error at all, runs the code perfectly and all that. I don't know what I'm doing wrong here.
You should use AND instead of &
AND is the logical operator on the other hand & is for joining strings
It is the logical as well as bitwise AND operator. If both the
operands are true, then condition becomes true. This operator does not
perform short-circuiting, i.e., it evaluates both the expressions.
If TextBox5.Text > TextBox1.Text And TextBox6.Text > TextBox2.Text Then 'NE
'Code
ElseIf TextBox5.Text > TextBox1.Text And TextBox6.Text < TextBox2.Text Then
'Code
End If
Note:
If values of TextBox1.Text =4, TextBox6.Text=1 then TextBox1.Text & TextBox6.Text will yields 41
See this demo
& is used to concatenate two strings

Converting numbers to text (2 existent words, same number)

I'm trying to convert the numerical result into words.
The sample I found works well for English words, but as I change them to my language, bunch of superficial problems came out.
What I need help in particular with is building the same code for 100 as its for 10. The reason I am doing this is because in my language we don't have One standing in front of every Hundred/Thousand
For example: Let's say the first number in Ones is called Taff
As it is, the program will make Taff Hundred where it should make make something like Taffss Hundred
So I created the Hundreds() which includes the correct callings.
Ones() contains the words from 1 - 9
Teens() contains the words from
11 - 19
Tens() contains the words from 10 - 90 (10,20,30 etc)
Hundreds() contains the words from 100-900 (100,200,300 etc)
Here is my code :
intAmount = eAmount
Dim nTousands As Integer = intAmount \ 1000 : intAmount = intAmount Mod 1000
Dim nHundred As Integer = intAmount \ 100 : intAmount = intAmount Mod 100
Dim nTen As Integer = intAmount \ 10 : intAmount = intAmount Mod 10
Dim nOne As Integer = intAmount \ 1
If nTen > 0 Then
If nTen = 1 And nOne > 0 Then
wAmount = wAmount & Teens(nOne) & " "
Else
wAmount = wAmount & Tens(nTen) & IIf(nOne > 0, " и ", " ")
If nOne > 0 Then wAmount = wAmount & Ones(nOne) & " "
End If
End If
ElseIf nOne > 0 Then
wAmount = wAmount & Ones(nOne) & " "
wAmount = wAmount & HMBT(nSet) & " "
wAmount = AmountInWords(CStr(CLng(nAmount) - _
(eAmount * multiplier)).Trim & tempDecValue, wAmount, nSet - 1)
Being the noob I am, I figured that by copy pasting the code for 10s, and changing the values to 100s will make things work, but it only works for 100 200 300 etc, not for the numbers in-between.
ElseIf nHundred > 0 Then
If nHundred = 1 And nOne > 0 Then
'Don't know how to properly add things here.
Else
wAmount = wAmount & Hundreds(nHundred) & IIf(nOne > 0, " и ", " ")
If nOne > 0 Then wAmount = wAmount & Ones(nOne) & " "
End If
Update
I made some changes, this time it partially works...
It shows 100s, 200s, 300s etc etc.
But it only shows from 100 to 109 when it "increments" If it goes above 109, the 100s aren't show, and it goes back to showing 10s, 11s, 12s etc.
ElseIf nHundred > 0 Then
If nHundred = 1 And nTen > 0 And nOne > 0 Then
wAmount = wAmount & Teens(nTen) & " "
Else
wAmount = wAmount & Hundreds(nHundred) & IIf(nOne > 0, " и ", " ")
If nOne > 0 Then wAmount = wAmount & Ones(nOne) & " "
End If
Made some changes... Is this closer to what you're after?
Private list_ones() As String = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
Private list_teens() As String = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
Private list_tens() As String = {"ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"}
Private list_hundreds() As String = {"ones", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
Private Function get_string_from_numeric(ByVal curr_number As Integer) As String
Dim to_return As String = ""
Try
Select Case curr_number
Case Is < 10
to_return = list_ones(curr_number)
Case 10 To 19
to_return = list_teens(curr_number - 10)
Case 20 To 99
to_return = get_return_value(curr_number, 10, list_tens)
Case 100 To 999
to_return = get_return_value(curr_number, 100, list_hundreds, "hundred")
Case 1000 To 9999
to_return = get_return_value(curr_number, 1000, list_hundreds, "thousand")
Case Is > 9999
to_return = "out of range"
End Select
Catch
Finally
End Try
Return to_return
End Function
Private Function get_return_value(ByVal curr_number As Integer, ByVal curr_pace_holder As Integer, ByVal curr_array_list As String(), Optional ByVal str_term As String = "") As String
Dim to_return As String = ""
Dim curr_iter As Integer = Math.Floor(curr_number / curr_pace_holder)
Dim curr_remainder As Integer = curr_number - curr_iter * curr_pace_holder
If (str_term <> "") Then str_term = " " & str_term
If (curr_remainder > 0) Then
to_return = curr_array_list(curr_iter - 1) & str_term & " " & get_string_from_numeric(curr_remainder)
Else
to_return = curr_array_list(curr_iter - 1) & str_term
End If
Return to_return
End Function
Private Sub cmd_submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_submit.Click
Try
If (IsNumeric(txt_input.Text)) Then
txt_output.Text = get_string_from_numeric(CInt(txt_input.Text))
Else
MsgBox("Invalid input.")
End If
Catch
Finally
End Try
End Sub

Loops - Adding Numbers - Visual Basic

So the program has to add all the numbers from "x" to "y".
But it also has to display all the numbers added :
i.e. 10 to 20 should display 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 = 165
Here's what I have:
Dim firstnum As Integer = Val(TextBox1.Text)
Dim secondnum As Integer = Val(TextBox2.Text)
Dim sum As Integer = 0
While firstnum <= secondnum
sum = sum + firstnum
firstnum = firstnum + 1
Label3.Text = firstnum & "+"
End While
suum.Text = " = " & Val(sum)
With the following:
Label3.Text = firstnum & "+"
You are overwriting the value in Label3 every time you go through the loop. What you probably want to do is concatenate the existing value with the next number.
This should get you on your way:
Label3.Text = Label3.Text & firstnum & " + "
Is Linq ok? Then you can use Enumerable.Range and Enumerable.Sum:
Dim startNum = Int32.Parse(TextBox1.Text)
Dim endNum = Int32.Parse(TextBox2.Text)
Dim numbers = Enumerable.Range(startNum, endNum - startNum + 1) 'inclusive, therefore + 1
Label3.Text = String.Join(" + ", numbers)
suum.Text = numbers.Sum()
your Label3.Text will only contain the last num and "+" at the end of the algorithm. You should replace
Label3.Text = firstnum & "+"
with
Label3.Text = Label3.Text & firstnum & "+ "