Textbox value summation - vb.net

I have two textboxes in which users enter a number, Data1.text and Data2.text. I need to do a simple check before proceeding and closing a form, i.e. the process should check if the sum of the values in Data1 and Data2 textbox is greater or lesser than the value from a label text named FinalValue.text, which gets its value from my main form.
If Data1.Text + Data2.Text <> FinalValue.Text Then
MessageBox.Show("The sum of the entered values are different from the final value!")
Exit Sub
End If
The problem is that whatever the user enters in the two textboxes, the messagebox pops up.
For example...if the FinalValue label has a value of 58.50, and if the user enters in one textbox 50 and in the other one 8.50, the messagebox pops up. I suppose I have to format my textboxes or something like that, but not sure in which way.
Thanks,

When you do Data1.Text + Data2.Text, you are actually just concatenating the values, not doing a number Sum, you need to convert to decimal.
Something like :
If Decimal.Parse(Data1.Text) + Decimal.Parse(Data2.Text) <> Decimal.Parse(FinalValue.Text) Then
MessageBox.Show("The sum of the entered values are different from the final value!")
Exit Sub
End If

Assuming the values are integers , change your condition to match the integers sum, You are currently concatenating the strings
CInt(Data1.Text) + CInt(Data2.Text) <> CInt(FinalValue.Text)
Or for Decimals use Decimal.Parse() in place of CInt()

Related

Check if textbox contains specific decimal numbers

I'm doing a school project but we weren't really taught about complex parts like this, so I'm hoping someone can help me.
I want my program to check whether my textbox contains 0.1-0.4 decimal numbers when I click a button. If yes, a message box will show that 0.5 or any half number must be entered. But, if the textbox contains 0.6-0.9, the message box will say to enter a whole number.
I know the message box part, I just don't know how to check those specific decimal numbers in the textbox. And also, if the textbox contain a whole number or a half number (.5), it will proceed to do what is stated.
If (Textbox1=0.1-0.4) Then
MsgBox("Enter a half number.")
ElseIf (TextBox1=0.6-0.9) Then
MsgBox("Enter a whole number.")
ElseIf (TextBox1=Half Number or TextBox1=Whole Number) Then
MsgBox("Transaction complete.")
End If
You can use a Select statement once you have converted the TextBox's Text to a numeric value:
Sub CheckDecimals(number As String)
If IsNumeric(number) Then
' Convert number to Single
Dim singleNumber As Single = CSng(number)
' Use Mod to get fraction only
Dim modNumber As Single = singleNumber Mod 1
Select Case modNumber
Case 0, 0.5
MsgBox("Transaction complete.")
Case Is < 0.5
MsgBox("Enter a half number.")
Case Is > 0.5
MsgBox("Enter a whole number.")
End Select
Else
MsgBox("Please enter a number.")
End If
End Sub
In your button Click event handler, use:
CheckDecimals(TextBox1.Text)

Filter as you type combo box me.recordsource

I can upload a file if someone tells me how.
Need help replicating a filter as you type on a combo box that is being used at the record level. Example: Instead of having an open text box for the prefix (e.g. Mr. Mrs. Ms. Dr.), I'm using a combo box that looks up from a reference table. I want to be able to type the letter "r" in the combo box and have it filter out Ms. and showing the remaining values. Once I make a selection store the selected value in the Name table.
Issue: When I add a new value in Combo4 the other rows above clear out if they don't match the value I just typed into the cell. Something likely with the RowSource in the below formula. Do I have something out of sequence or a flawed formula?
What I think I'm trying to do:
1) If Prefix value populated w/ value in t_Name THEN show the matching value in t_ref_Prefix
2) If Combo4 is Blank / Null THEN then open Combo4 and show all values in t_ref_Prefix so a value can be selected.
3) If user is typing text into Combo4 THEN filter on change using * on both sides of the typed value.
Option Compare Database
Option Explicit
Private Sub Combo4_Change()
'https://stackoverflow.com/questions/48133260/display-records-in-access-db-combobox-on-any-text-typed-by-user
'test number of characters entered - if greater then 0 then assign rowsource
If Len(Me.Combo4.Text) > 0 Then
'set the rowsource to match user search criteria
Me.Combo4.RowSource = "SELECT * FROM t_ref_Prefix WHERE Prefix LIKE '*" & Me.Combo4.Text & "*'"
'show the search in real-time
Me.Combo4.Dropdown
Else
'set to no
Me.Combo4.RowSource = "SELECT t_ref_Prefix.auto, t_ref_Prefix.prefix, t_ref_Prefix.sort FROM _
t_ref_Prefix ORDER BY t_ref_Prefix.sort, t_ref_Prefix.prefix"
End If
End Sub
You need to set
Combo4.AutoExpand = False
This will do it.

Forcing user to enter one entry from two options not to write it together

i have two text boxes : percentage and txtamount
i want to force user to enter percentage or amount not to write it together
casue if he did entered % and amount it makes some problems with me as i have alot of formulas
thanks
What I have tried:
privite sub post_click()
If (Me.percentage.Value >= 0 And Me.txtamount.Value >= 0) Then MsgBox " You should select % or amount ": Exit Sub
If you want to ensure that one (and only one) of your two textboxes has a value in it, use the following:
Private Sub post_Click()
If Not (Me.percentage.Value = "" Xor Me.txtamount.Value = "") Then MsgBox "You should select % or amount": Exit Sub
Testing for >= 0 won't work if the textbox is empty (because "" >= 0 will give a type mismatch). Testing for whether the textbox is "" will give a better indication of whether data has been entered.
Using Xor (which returns True iff one operand is True and the other is False) will therefore determine whether only one textbox has been filled in and the other left empty, and the Not will display the message if that is not the case.

Error on comparison of numbers from textbox

I have 2 textboxes which inputs 2 values from the user and compares the first value with the second. If the first value is less than the second value, it enters the loop where the user has to input the values again.
The problem I am getting is if I input 10 and 9.9 in the text boxes, it is entering into the loop and saying that 10 is less than 9.9. This happens only with 10,100 and 1000. Please help.
I even tried defining Long type variables for both the integers and assigned them and compared them, excel just hangs. Please help
Here is the code below.
If Mean < LSLValue Then
MsgBox "Please enter a numeric value greater than LSL as Nominal Value"
Me.DimnTxt.Value = InputBox("Enter the Nominal")
Me.LSLTxt.Value = InputBox("Enter the LSL")
Mean = Me.DimnTxt.Value
LSLValue = Me.LSLTxt.Value
End If
Loop Until Mean >= LSLValue
May be because you are comparing text instead of number.
Try convert to number then compare. EG:
if CDbl(Textbox1.value) < CDbl(Textbox2.value) then
'....
end if

Concat decimal value with string

I am retrieving data from the database and displaying in Gridview. I have some data in which I want to display % sign at the end (in gridview) what would be the best way to achieve this. Can I concat % sign in sql select statement or I have to add using vb code behind.
The best way is to use DataFormatString property from the Gridview columns and set it to something like "{0:0.00}%". This will format your results by using the number, 2 decimal positions and adding the % symbol to the end.
If you have rows with numeric values and rows that already contain % or other special characters, the best way to assure that the data is correctly visualized would be to remove the % or special characters before setting the Gridview data source. This way you will allways get the same result for each row.
I loop through the Gridview row and concat % sing at the end
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(6).Text = 4 Then
e.Row.Cells(5).Text += " %"
End If
End If