I have a form with 3 tabs: one tab is for user entry and computations, a second tab shows values that will be used for computations which are read from one database (I call it settings in that the values are constants for calculations but it’s not application settings in the way people think of it, to be clear) and the third displays a table from another database. The second tab has a table adapter interface for the dataset that contains values used for calculations that can be changed (to do different scenarios.) Those numbers are read from a single record in a database that has 20 fields and was read once when the data is loaded into the second tab to be displayed.
The problem I am having is that I would like to use the numbers from the second tab (the constants) in computations to be shown on the first tab.
I have tried declaring the variables on the second tab as public but they are already “friends” with the form event. However, when I go to use them in calculations, it’s like they don’t exist.
For example, one calculation I am doing is fuel cost. The user enters miles and this is stored as a variable called mileage. To calculate the result fuelcost, I have to use two other fields that are shown in the second tab: MPG (miles per gallon) and fuelprice (the price of gas). So fuelcost = (mileage/mpg)*fuelprice. These are read from a datatable called testdata which is actually part of the project. I want to be able to use these variables throughout the program but declaring them as public isn’t working.
How do I make the variables from one tab usable elsewhere in the program? The other thing I noticed is since the values of the variables are loaded into textboxes and assigned variable names by naming the textboxes, using val([variablename].text) isn’t solving the problem either. For example, saying milespergallon = val(mpg.text) is not doing the trick because I am getting a message that ‘text’ is not an integer when I declare milespergallon.
Update
Here the code that is being used:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mileage = Int32.Parse(Miles.Text)
Dim rate = 0
Dim baserate = 0
Dim rateper = 0
Dim ratemile As Integer
Dim Mpg As Decimal
Dim milespergallon As Decimal
milespergallon = CDec(Mpg.Text)
' Ensure the value is numeric
If IsNumeric(Mpg) Then
Mpg = CDec(Mpg)
Else
milespergallon = CDbl(Mpg.Text)
End If
txtmilespergallon.Text = milespergallon.ToString
End Sub
TabPage2 is where the variables are displayed that I want to use in TabPage1.
Private Sub TabPage2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage2.Click
End Sub
This is related to another post I had regarding manipulating values from a database so my answer assumes that a user has loaded data from a single row database and displayed it in a tabpage, but wants to also use that data elsewhere in the program.
While you can drag the dataset into a tabpage to be displayed, the values from the dataset have to be stored to local variables. This has to happen in the form load event after the dataadapter fill statement in order for the numbers to be used anywhere in the form:
and so on. In my example, the dataset is called ‘foo’ so it is fooDataSet. The table is called ‘testdata’ and is a single row table with several records. Think of an Excel sheet with one row and several columns…this is what testdata looks like. I’ve generically called each ‘column’ variablen where n is the number of the column for my example, although you would give them more meaningful names. Example:
You can then DIM those variables. In the example of discountcost and markup, you would DIM them as decimal and then could use them in calculations in any tabpage on your form.