I am fairly new to programming. I am making a program where you enter a number into a textbox and when you leave the textbox, it converts your number to currency. I have also added another textbox which does the same.
This is what I want to happen:
If TextBox1.Text + TextBox2.Text =< 10,000 Then
Sum = 10,000
Else
Sum = TextBox1.Text + TextBox2.Text
However, (i think) because I am converting the numbers to currency before I add them together, it always returns 10,000 no matter what.
I know there is probably something pretty obvious I am missing. If you need any clarification, just ask! I appreciate your help.
+ concatenates strings, so TextBox1.Text + TextBox2.Text is a string and comparing it to the integer 10000 doesn't do what you want.
I would look for a text box control that lets you display numbers with a given format, but also gives you access to the numerical value as a property. Then you could add the values together without having to do a type conversion that may not work on your formatted currency string.
MaskedTextBox might give you some of that, but it's been a long time since I've used it.
You need to convert String values to Double (or any other numeric type), before adding them up.
Dim tbSum as Double = CDbl(TextBox1.Text) + CDbl(Textbox2.Text)
If tbSum =< 10000 Then Sum = 10000 Else Sum = tbSum
Related
May seem like a stupid question, but how do you sum variables in VBA to check input data is correct?
I'm trying to check the user has inputted data correctly into a UserForm before they continue to the next page. To do this I want to sum some of their input variables. If they don't input it correctly, they get a message box telling them to revise the numbers. My code is:
Dim BinQnt As Double
Dim FillQnt As Double
Dim FineQnt As Double
Dim CoarQnt As Double
Dim RAPQnt As Double
Dim CRQnt As Double
If BinQnt + FillQnt + FineQnt + CoarQnt + RAPQnt + CRQnt = 100 Then
'Code here for inserting values into database. Omitted to save space and confusion.
Else
MsgBox "Error, please check mixture design sums to 100%."
End If
When I'm testing it, it always goes to the error message box and I'm not sure why. Very sure I am adding variables which sum to 100 (haha). I first tried it without defining the variables, and now I have it still doesn't work.
Am I missing something obvious?
Do you want to round it or not?
In your question, you have the variables as Doubles, and in the answer as integers. Having them as integers will make it easier to hit 100%, but it will round to the nearest integer (2,4 = 2 | 2,6 = 3)
I will assume you are using all texboxes in the form, and as such use this code that is universal:
Dim tot As Double
tot = 0
For Each c In Me.Controls
If TypeName(c) = "TextBox" And IsNumeric(c.Value) Then
tot = tot + c.Value
End If
Next c
If tot = 100 Then
'Code here for inserting values into database. Omitted to save space and confusion.
Else
MsgBox "Error, please check mixture design sums to 100%. Currently at " & tot & "%."
End If
This would give us something like this:
However, using Integer or Long, gives us this:
Also, the reason as to why I'm using IsNumeric(c.Value), is to stop the code from failing in case of empty boxes (or filled with invalid entries).
You could also place a check here in case no boxes are allowed to be empty.
I need to format the main part (whole) of a number without touching or affecting the decimal part:
12345.123456 becomes 12,345.123456
123.123 becomes 123.123
12345678.123 becomes 12,345,678.123
123 becomes 123
The fractional part length is variable in length of decimal places and need to be kept untouched (as is).
The formatting applies only to the whole number. Formatting the whole number is simple, but how to not affect the decimal part.
The Format parameter should work with any length of decimal places.
I am using the following:
Format(123456789.12345,"#,#.#############################")
However, the only problem with this solution is:
There is always an assumption on the maximum possible number of decimal places by the number of # used.
If the number is without a fraction say "123.0" or "123", the output will be "123." always with a decimal separator (dot).
Thanks
Like #nicomp said you'll want to break this into two parts.
dim num as string 'or a double converted to a string
dim nums() as string 'array
num = 123456789.123456
nums = split(num, ".") 'break into array at decimal
nums(0) = format(nums(0), "###,###") 'format whole numbers
num = nums(0) & "." & nums(1) 'recombine
This should add a comma after every three whole numbers
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
I know this has been asked quite a few times, but i'm having issues with the solutions found on most other pages.
I have a single datagridview column that i want to be sorted by number (1,2,10 instead of 1,10,2)
Best i can see online, i need to convert the column or cell to an integer value type - but i'm not sure how to do so.
I've tried grid.columns(4).valuetype = typeof(System.int32), and tried the same for cells individually.
Trying above always results in a "int32 is a type in 'system' and cannot be used as an expression" error - which i'm not sure about.
The data itself is obtained froma text file, and converted from string to integer when being added into the cell datagrid_alltracks.Rows(shutupaboutlambda).Cells(4).Value = CInt(numstring))
You can just set the DataGridView SortCompare Event to compare two integers (or two singles, or two doubles). Code wise (calling your datagridview "grid")
Private Sub grid_SortCompare(sender as Object, e as DataGridViewSortCompareEventArgs) Handles grid.SortCompare
e.SortResult = CInt(e.cellvalue1).CompareTo(CInt(e.cellValue2))
e.Handled = True
End Sub
if you are doing single or double variables, use CSng or CDbl instead of CInt
e.SortResult = CSng(e.cellvalue1).Compareto(CSng(e.CellValue2)
You can do more fancy sorting if you want, You basically need to know that e.SortResult is Positive, Negative or Zero, and your cells are sorted according to that result (Positive keep order, negative reverse order, Zero - matched - do nothing (yet)). The current row index(s) and column are available in the e arguments so you can also compare adjacent column data if the current cells are matched)
If the grid is bound to a data source you could try
datatable.Columns.Add("ColumnName", GetType(Integer))
Else you may need to use the SortCompare event on the gridview.
See here
I know I'm coming to the party late, but I found that I once I had added the data, I needed to convert the desired columns to have a data type.
I'm adding data like the following:
DataGridView1.Rows.Add(New String() {CInt(recordnum), True, "play", wszName.ToString, qiOffset.ToString, value.ToString, qiLength.ToString})
Then, after all the data has been added, I then do a simple loop and convert the column, where I can then sort it. It's set up so you can do multiple columns if need be.
Dim colnum As Integer
colnum = 0 ' set this as your column to change the data type to
For i As Integer = 0 To DataGridView1.Rows.Count - 1
Dim d As Double = Double.Parse(DataGridView1.Rows(i).Cells(colnum).Value.ToString())
DataGridView1.Rows(i).Cells(colnum).Value = CInt(d)
DataGridView1.Rows(i).Cells(colnum).ValueType = GetType(Double)
Next
Sorting can work for whatever column you adjusted. In this case, it's column 4.
DataGridView1.Sort(DataGridView1.Columns(4), System.ComponentModel.ListSortDirection.Ascending)
I am acutally using SSRS but it is for an expression so this is VB code. I am wondering how I would get a number such as 236.4723423 to appear at 236.4 instead of 236.5, so basically I jsut want to truncate it always after 1 decimal.
I tried Format = "N1" this rounds it
I tried Formate = "#######.0" and "######.#" and this rounds it as well.
Any ideas?
value = Math.Floor(value * 10) / 10
Use the format "########.00", then once it's in string form, trim the last char.
Edit:
Dim myString as String
myString = CStr(FORMAT(((SUM(Fields!Shipment_Weight.Value)) / 2000),"######.00"))
myString = myString.Substring(0, myString.Length - 1) & "T"
You will probably want to implement a custom IFormatProvider interface to do this. There is a great example on MSDN here.