VB.net: How to make original variable value fulfill 2 statements? - vb.net

I am creating an EXP multiplier and I do not know how to make the original variable do be separated to do fulfill 2 statements.
Aim:
I want it so that when checkbox1.checked = true checkbox2.checked = true checkbox5.checked = true, and the input = 2, then the answer will be 2 x 2x1.5 + 2 x 1.1 = 8.2 NOT 2 x 2x1.5x1.1 which gives me 6.6.
However, I tried using the separator to separate them, then add them together in the result as exptotal, but I made a mistake using exp2 with exp as inputs, as the answer is now 2 x 2x1.5 + 2 + 2 x 1.1 as my code writes exp + exp2 and exp2 is also an input so the result is now 8.2 instead of 6.2 with that extra input.
I do not know how to get around so that i can use the ORIGINAL exp such that exp2 = (exp * 1.1) without using the exp from checkbox1 and checkbox2
My code:
dim exp as double
dim exp2 as double
dim exptotal as double
If IsNumeric(TextBox9.Text) Then
exp = CDbl(TextBox9.Text)
Else
MsgBox("Please input a number.")
End If
If CheckBox1.Checked = True Then
exp = exp * 2
End If
If CheckBox2.Checked = True Then
exp = exp * 1.5
End If
If CheckBox5.Checked = True Then
exp2 = exp2 * 1.1
End If
exptotal = exp + exp2
This code makes the result includes the input 2 again as totalexp = exp + exp2 which is not what I want. I want to get rid of that extra input but still having the checkbox5 statement being ADDED together with checkbox1 and checkbox2 NOT multiplied. But I do not know how to do that. This is the closest I can come to. I hope you can understand me!
I am very confused right now, please help!!

Reading your question is soo confusing. Your variable also don't tell any story, what is CheckBox1, TextBox9 ? These are bad variable name.
Since your not telling what the logic should be but only talk about one scenario, I have to guess.
Dim input, total As Double
total = 0
If Not Double.TryParse(TextBox9, input) then
MessageBox.Show("Please input a number.")
Else
Dim part1, part2 As Double
part1 = 0
part2 = 0
If CheckBox1.Checked = True And CheckBox2.Checked = True Then
part1 = 3
Else If CheckBox1.Checked = True Then
part1 = 2
Else If CheckBox2.Checked = True Then
part1 = 1.5
End If
If CheckBox5.Checked = True Then
part2 = 1.1
End If
total = (input * part1) + (input * part2)
End If

Related

when divide is selected and a zero is entered for division display message

VB.Net,Calculator project that has 2 Text Boxes for number entry, a drop down list, "OperatorList", for math operator, and a Calculate button.
I need to display a message on the "Result Label", not a message box, stating "You cannot divide by zero". My VB.Net code is below but the message won't display. Please help.
Protected Sub CalculateButton_Click(sender As Object, e As EventArgs) Handles CalculateButton.Click
If ValueBox1.Text.Length > 0 AndAlso ValueBox2.Text.Length > 0 Then
Dim result As Double = 0
Dim value1 As Double = Convert.ToDouble(ValueBox1.Text)
Dim value2 As Double = Convert.ToDouble(ValueBox2.Text)
Select Case OperatorList.SelectedValue
Case "+"
result = value1 + value2
Case "-"
result = value1 - value2
Case "*"
result = value1 * value2
Case "/"
result = value1 / value2
End Select
If OperatorList.SelectedValue = "/" Then
If value2 <> 0 Then
ResultLable.Text = "You cannot divide by Zero"
Else
result = value1 / value2
End If
End If
ResultLable.Text = result.ToString()
Else
ResultLable.Text = String.Empty
End If
End Sub
You're over-writing the resultlable.text property with the value of the result variable after the divide by 0 check if statements.
Doing something such as exiting the sub after writing out the divide by zero message should give you the result you're hoping for.
Then secondly, you were checking if value2 was not 0 as the divide by zero check, when it needs to check to see if the value is zero.
If OperatorList.SelectedValue = "/" Then
If value2 = 0 Then
ResultLable.Text = "You cannot divide by Zero"
Exit Sub
Else
result = value1 / value2
End If
End If

getting a return value from a function into another sub routine

I have the below code which returns the premium amount but i am not sure how to get this to display in another function. The sub routine i want to get the value into just displays text about the options selected then i need to display the value calculated based on those results.
Function ProcessClaims(ClaimsList As claimsList, PremiumIn As Decimal) As Decimal
Dim adjustedPremium, originalPremium As Decimal
Dim declined As Decimal
originalPremium = ClaimsList.claimValue * 100 \ 5
If ClaimsList.claimValue <= 5000 And ClaimsList.isPersonalInjury = False Then
adjustedPremium = originalPremium
ElseIf ClaimsList.claimValue > 5000 And ClaimsList.claimValue <= 10000 And ClaimsList.isPersonalInjury = False Then
adjustedPremium = originalPremium * 100 / 10
ElseIf ClaimsList.claimValue > 10000 Or ClaimsList.isPersonalInjury = False Then
adjustedPremium = -1.0
declined = -1.0
End If
If adjustedPremium = -1.0 Then
PremiumIn = declined
Else
PremiumIn = adjustedPremium
End If
Return PremiumIn
End Function
Any help would be appreciated, thank you
It's pretty straightforward ..
Your sub/function that displays the result should be defined as something like
Private Sub DisplayStuff(premiumInResult as decimal)
'display the value premiumInResult in here
End Sub
You can then either do this which is clearer
Dim result As Decimal = ProcessClaims(yourClaimsList , yourPremiumIn)
DisplayStuff(result)
or
Displaystuff(ProcessClaims(yourClaimsList , yourPremiumIn))
which is shorter but less clear, and makes debugging more difficult

VBA Else without if error

I keep getting the " Else without if" error in VBA when I clearly do not have that issue. Does anyone know how to fix it? It takes me to the elseif statement that begins with elseif memb= "platinum"
Below is my code:
ElseIf memb = "Platinum" Then d = 0.01
ElseIf memb = "Black" Then d = 0.03
End If
If st >= min Then cb = st * d Else cb = 0
End If
If cb >= thresh Then MsgBox ("cb is greater than thresh")
End If
tac = st + cb
Range("B5").Value = st
Range("B7").Value = cb
Range("B9").Value = tac
I'm going to assume your first If statement goes something like this:
If memb = "Gold" Then d = 0.005
ElseIf memb = "Platinum" Then d = 0.01
ElseIf memb = "Black" Then d = 0.03
End If
If some processing is performed on the same line as the Then keyword, VBA treats it as a single, non-nested If statement. This means that anything after that will be treated as a new statement and not related to prior If statement.
What you can do is put the processing statement(s) on the next line after each If-Then and ElseIf-Then statements.
Example,
If memb = "Gold" Then
d = 0.005
ElseIf memb = "Platinum" Then
d = 0.01
ElseIf memb = "Black" Then
d = 0.03
End If
With this in mind, you may want to fix the succeeding If-Then-Else statements in your code. The End If part becomes meaningless if your If-Then-Else is in a single line.
Your code seems to have syntax error and error message tells you that.
Or you did not post all code?
Have a look on MS documentation: https://msdn.microsoft.com/de-de/library/752y8abs.aspx
Do you really stick to the syntax?
Without even having MS OFfice this should (be better readable and) work:
If memb = "Platinum" Then
d = 0.01
ElseIf memb = "Black" Then
d = 0.03
End If
If st >= min Then
cb = st * d
Else
cb = 0
End If
If cb >= thresh Then
MsgBox ("cb is greater than thresh")
End If
tac = st + cb
Range("B5").Value = st
Range("B7").Value = cb
Range("B9").Value = tac

convert a text to a number

I don't know why this code doesn't work ..i wanna just convert text to number .. It doesn't give me any error but it doesn't work
Public Function ConvertCOMPLEXITYToNumber(ByVal chain As String) As Integer
Select Case chain
Case "1 - Très difficile"
ConvertCOMPLEXITYTToNumber = 1
Case "2 - Difficile"
ConvertCOMPLEXITYTToNumber = 2
Case "3 - Modérée"
ConvertCOMPLEXITYTToNumber = 3
Case "4 - Facile"
ConvertCOMPLEXITYTToNumber = 4
Case Else
ConvertCOMPLEXITYTToNumber = 0
End Select
Exit Function
End Function
That may be because you may have unwanted leading or trailing spaces which fails the comparison. Also you do not need Exit Function at the end of the code. It will exit any ways :)
Try this
Public Function ConvertCOMPLEXITYToNumber(ByVal chain As String) As Integer
Dim Num As Integer
Select Case Trim(chain)
Case "1 - Très difficile": Num = 1
Case "2 - Difficile": Num = 2
Case "3 - Modérée": Num = 3
Case "4 - Facile": Num = 4
Case Else: Num = 0
End Select
ConvertCOMPLEXITYToNumber = Num
End Function
If IsNumeric(Left(Trim(chain),1)) Then
ConvertCOMPLEXITYToNumber = Left(Trim(chain),1)
Else
ConvertCOMPLEXITYToNumber = 0
End If
Here, my approach for you:
Public Function ConvertCOMPLEXITYToNumber(ByVal chain As String) As Integer
'Get the first character
chain = Left(Trim(chain), 1)
'If frist character is numeric
If IsNumeric(chain) Then
'If first number is less than 5, return value
If chain < 5 Then
ConvertCOMPLEXITYToNumber = CInt(chain)
End If
End If
End Function

Upper Case in VB 6 text box

How to make first letter in upper case while pressing tab or space in vb 6.0 ?
My code is as follows
txtFirstName.Text = UCase$(txtFirstName.Text)
but it doesn't change after tab or space
It's just simple just do this in the text box keypress events...
Private sub textbox_keypress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub
Use the LostFocus event
Private Sub yourTextBox_LostFocus()
With yourTextBox
'first letter in upper case, the rest, untouched.
.Text = UCase(Mid(.Text, 1, 1)) & Mid(.Text, 2, Len(.Text))
End With
End Sub
Apply the same logic to the KeyDown event and check if the pressed key is the space key.
Private Sub yourTextBox_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 32 Then
With yourTextBox
'first letter in upper case, the rest, untouched.
.Text = UCase(Mid(.Text, 1, 1)) & Mid(.Text, 2, Len(.Text))
.SelStart = Len(.Text) 'put the cursor at the end of the textbox...
End With
End If
End Sub
StrConv Function
Returns a Variant (String) converted as specified.
Syntax
StrConv(string, conversion, LCID)
The StrConv function syntax has these named arguments:
Part Description
string Required. String expression to be converted.
conversion Required. Integer. The sum of values specifying the type of conversion to perform.
LCID Optional. The LocaleID, if different than the system LocaleID. (The system LocaleID is the default.)
Settings
The conversion argument settings are:
Constant Value Description
vbUpperCase 1 Converts the string to uppercase characters.
vbLowerCase 2 Converts the string to lowercase characters.
vbProperCase 3 Converts the first letter of every word in string to uppercase.
AND THERE IS MORE ...
TO GSERGE
$ means nothing when applied to a function name as opposed to a variable name. VBA uses $ AND B as a suffix to denote similar functionality.
VB6 IS VBA the person who said maybe in VB6 but not in VBA. VB6 program host VBA as their programming language. VB6 on it's own are some app objects and the forms package only - no programming language. It's best to think of VB6 as a VBA host like Office.
If you want to proper case see this WORDBASIC Ver 6 code, (which word 2003 helpfully converted to vba).
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Public Sub MAIN()
Select Case WordBasic.Int(GetModifer)
Case 0
WordBasic.ChangeCase
Case 1
WordBasic.ChangeCase 4
Case 2
WordBasic.ChangeCase 2
Case 3
ProperCase
Case Else
WordBasic.ChangeCase
End Select
End Sub
Private Sub ProperCase()
Dim F
Dim z
Dim a$
Dim P
F = 1
WordBasic.ChangeCase 2
WordBasic.EditBookmark Name:="SerenityChangeCase", SortBy:=0, Add:=1
z = WordBasic.GetSelEndPos()
WordBasic.CharLeft 1
While WordBasic.GetSelEndPos() < z And Not WordBasic.AtEndOfDocument()
WordBasic.SelectCurWord
a$ = WordBasic.[Selection$]()
P = 0
If LCase(a$) = "a" Then
P = 1
ElseIf LCase(a$) = "an" Then
P = 1
ElseIf LCase(a$) = "as" Then
P = 1
ElseIf LCase(a$) = "at" Then
P = 1
ElseIf LCase(a$) = "be" Then
P = 1
ElseIf LCase(a$) = "by" Then
P = 1
ElseIf LCase(a$) = "in" Then
P = 1
ElseIf LCase(a$) = "is" Then
P = 1
ElseIf LCase(a$) = "of" Then
P = 1
ElseIf LCase(a$) = "on" Then
P = 1
ElseIf LCase(a$) = "or" Then
P = 1
ElseIf LCase(a$) = "to" Then
P = 1
ElseIf LCase(a$) = "and" Then
P = 1
ElseIf LCase(a$) = "are" Then
P = 1
ElseIf LCase(a$) = "for" Then
P = 1
ElseIf LCase(a$) = "the" Then
P = 1
ElseIf LCase(a$) = "from" Then
P = 1
ElseIf LCase(a$) = "what" Then
P = 1
ElseIf LCase(a$) = "with" Then
P = 1
End If
If P = 1 And F = 0 Then WordBasic.Insert LCase(a$)
WordBasic.WordRight 1
F = 0
Wend
WordBasic.WW7_EditGoTo Destination:="SerenityChangeCase"
WordBasic.EditBookmark Name:="SerenityChangeCase", SortBy:=0, Delete:=1
End Sub
Private Function GetModifer()
Dim a
Dim B
Dim c
Dim X
a = GetAsyncKeyState(16)
B = GetAsyncKeyState(17)
c = GetAsyncKeyState(18)
X = 0
If a < 0 Then X = X + 1
If B < 0 Then X = X + 2
If c < 0 Then X = X + 4
GetModifer = X
End Function
OK. Yeah txtFirstName is a good indicator of usage here.. So I'd use (sort of) Title Caps And I'd do it on the Validate event.. So
Private Sub txtFirstName_Validate(Cancel As Boolean)
Dim p As Integer ' i doubt we'll use more than 32K for a name....
Dim mName As String
p = 1
' first off lets trim any leading blanks.. assume NOTHING and make sure its all lower case..
mName = LCase(LTrim(txtFirstName))
Do While p > 0 And p <= Len(txtFirstName) ' start with the first non-blank
Mid(mName, p, 1) = UCase(Mid(mName, p, 1))
p = InStr(p, mName, " ")
If p > 0 And p < Len(mName) Then p = p + 1
Loop
Cancel = False
txtFirstName = mName
End Sub
Works every time, and capitalizes each word.. Didn't add any code to to do TRUE title caps but this is close, and short & easy...