I would like to create a query that generates a randomized set of values for particular fields. For example, let's say eye color. When run, the query would return a randomized eye color.
I know how to create a table with eye color values, and how to randomize and select one value. (How to get random record from MS Access database)
However, I would also like to determine how likely that eye color is to be pulled as a value. For example, blue eyes might have a likelihood of 15/100, where brown eyes might have a factor of 60/100.
Is it possible to create a field and/or query/SQL formula to achieve this? The only thing I can think of is to actually add each value X number of times into the table, but that goes against normalization and it feels like there's probably a more elegant solution.
You could create a simple function like this:
Public Function RandomEyeColour() As String
Dim EyeColours As Variant
Dim EyeColourRange As Variant
Dim EyeColour As String
Dim EyeColourValue As Single
Dim index As Integer
EyeColours = Array("Brown", "Blue", "Grey", "Hazel", "Green", "Amber")
' Eye colour distribution:
' 83%, 8%, 3%, 2%, 2%, 2%.
' Add up the values.
EyeColourRange = Array(0.83, 0.91, 0.94, 0.96, 0.98, 1)
Randomize
EyeColourValue = Rnd()
For index = LBound(EyeColours) To UBound(EyeColours)
If EyeColourValue < EyeColourRange(index) Then
EyeColour = EyeColours(index)
Exit For
End If
Next
RandomEyeColour = EyeColour
End Function
Related
Trying to figure out conditional formatting for SSRS/Visual Studio and trying to get the figures to show this as an expression as follows;
Below benchmark needs to be Red
Above but within 5% needs to be Gold
More than 5% above needs to be Green
Benchmark 60%
Year 1 62.2%
Year 2 67.4%
Year 3 43.6%
First time in writing something like this so unsure what the best step is!
=iif("Textbox83" > "Textbox82", "SpringGreen", iif("Textbox83" < "Textbox82", "Red", iif("Textbox83" = "Textbox82" + 0.05,"Gold", "Transparent")))
A few things here..
Use SWITCH rather than nested IIFs, it much easier to read and debug.
You would normally work against the dataset fields rather than the textboxes displayed on the rendered report. If you do refence the textboxes, you probably have to convert the values to numeric before comparison
If you reference an object, either a dataset field or an rendered textbox, you must state the property you want, typical .Value
If this was being done against dataset fields you would do something like this
=SWITCH (
Fields!myValue.Value < Fields!Benchmark.Value, "Red",
Fields!myValue.Value <= (Fields!Benchmark.Value * 1.05), "Gold",
Fields!myValue.Value > (Fields!Benchmark.Value * 1.05), "Green",
True, Nothing
)
Switch stops at the first expression that results in True so the sequence of conditions is important. The final expression pair True, Nothing acts like an ELSE and returns nothing (which is the default backgroundcolor property, which appears as transparent)
If you wanted to replicate this using the textboxes (don't unless you really have to!) then it's basically the same but a bit more messy.
=SWITCH (
VAL(ReportItems!TextBox83.Value) < VAL(ReportItems!TextBox82.Value), "Red",
VAL(ReportItems!TextBox83.Value) <= (VAL(ReportItems!TextBox82.Value) * 1.05), "Gold",
VAL(ReportItems!TextBox83.Value) > (VAL(ReportItems!TextBox82.Value) * 1.05), "Green",
True, Nothing
)
The above is from memory and therefore untested but it should be OK.
Hi I am trying to include Variance in my SSRS matrix for Percentages on a month by month basis.
This is what I want to achieve:
the expression I am trying to use which I have got from a forum is as follows:
=ReportItems!Textbox15.Value - Code.GetPreviousValue(ReportItems!Textbox15.Value)
And this references custom code again from the same forum as follows:
Private previousValue As Integer = 0
Public Function GetPreviousValue(ByVal runningValue) As Integer
Dim temp As Integer = previousValue
previousValue = runningValue
Return temp
End Function
My issue is this does not work for the variance between April and May for example giving me results that are incorrect.
To be clear April% and May% figures are correct but the variances do not appear as in my example. When I check them they are incorrect.
If I alter the (variance) query so instead of taking the query that calculates the % I just calculate variance between the numerator of April% and numerator of May%, variances are correct.
I think the issue is that percentages don't go through the custom code portion correctly. Could this be due to the fact that this works according to INT?
I appreciate this may be as clear as mud... Struggling a bit to explain my problem. Thanks
Yes the integer makes the problem, because you have values with dots (97.3, 91.8, etc...). When these values get converted to integer, they lose the right decimal places. Just replace the datatype in your function like this:
Private previousValue As Double = 0
Public Function GetPreviousValue(ByVal runningValue) As Double
Dim temp As Double = previousValue
previousValue = runningValue
Return temp
End Function
I have a monitoring in datagridview that has a display of list of vehicles and their types. I want to count the number of each type of the vehicle.
Example : there are 8 Sedans and 5 Motorcycles in the datagridview and I want it to display in the label.
here is my code:
Dim table As New DataTable()
Dim command As New MySqlCommand("select count(ctype) from tblreport where ctype='SUV'", conn)
command.Parameters.Add("count(ctype)", MySqlDbType.VarChar).Value = Label6.Text
Dim adapter As New MySqlDataAdapter(command)
adapter.Fill(table)
DataGridView2.DataSource = table
After you assign the datasource, just add this :
Label1.Text=datagridview1.rows.count
Text is a property of many controls like label,button and others.This property can be used to display an overlay of text over the control. Datagridview has rows ,right ? So, as u said, u want to display the number of rows the datagridview contains.So all you need to do is use Datagridview1.rows.count,i don't think this one needs any explanation at all.
Anyway, let's advance a bit further.
You also said
there are 8 Sedans and 5 Motorcycles in the datagridview and I want it to display in the label.
By default,no application can understand how many records match Sedans and Motorcycles .But as mentioned above, vb is able to calculate the rows of a datagridview(by default/by simple codes).So try to think for once,if the datagridview only had the rows that contained Sedans or Motorcycles in any cell, vb.net could've easily counted it..right ??
So, if u ask me, how do we get only the rows containing Sedans or Motors , then i'ld as u, how does google show only the results u r looking for instead of showing everything it has ?? The answer is simple,google shows exactly what you Search for. When it comes to databases,we call it filtering/sorting/even seaching.So, all you need to do is filter your database.There are couple of ways,which,if i start explaining,would take a lotta time.However,check these :
• http://csharp.net-informations.com/datagridview/csharp-datagridview-filter.htm
• Filtering DataGridView without changing datasource
Now, let's go deeper.If you go through the links above,you'll be able to show only the rows that contain Sedans or so in any of it's cells.What if you want all the rows but only want the label to display the number(row count) of Sedans or so ?
In this case,you can use a Datatable.
Learn more about Datatable here
You can easily use a datatable to count the rows containg only the QUERY u r looking for.For that, u can code as follows :
Dim cmd as new MySqlCommand("Select * from [tablename](columnname)(#value)",con)
cmd.parametres.addwithvalue("#value","Sedan")
Dim table as new DataTable
Dim adap as new MySqlDataAdapter
adap.fill(table)
If table.rows.count <=0 then
Else
LAbel1.text=table.rows.count
Let me explain from dim table.Here i am declaring a datatable.But before that i am filtering the dataset by using cmd.parametres.addwithvalue.Then,i am using a DataAdapter and filling the DataTable with only th e rows containing SEDAN. Now, focus on If table.Rows.Count <=0 Then ...This indicates if there are no rows(which is less than or equals to 0) containing the query.Then i am using the Else statement which means if the datatable contains rows matching the query.In the else statement,i am adding the row count of the Datatable as a string/text in the Label..
I hope this is enough and this explanation will help u in future as well.
Use the same DataTable you have used to fill the DataGridView. Only change your select statement to Select * from tableName. I used the index of 2 but change to whatever it is in your table. The column index starts at zero.
Dim totalSedans As Integer = 0
Dim totalMotorcycles As Integer = 0
Dim totalConvertable As Integer = 0
For Each row As DataRow In table.Rows
Select Case row(2).ToString
Case "Sedan"
totalSedans += 1
Case "Motorcycle"
totalMotorcycles += 1
Case "Convertable"
totalConvertable += 1
Case Else
Debug.Print("Something doesn't match categories")
End Select
Next
lblConvertable.Text = totalConvertable.ToString
lblMotorcycles.Text = totalMotorcycles.ToString
lblSedan.Text = totalSedans.ToString
I have data in an SSRS report that groups and provides percentages. I need to change the font color for the percentages and I can't figure out how to create the expression.
Currently I obtain the percentage with this formula:
Sum(Fields!Time_Msg.Value)/Count(Fields!Driver.Value)
The Time_Msg field is obtained from SQL. I want the font expression to be the greater of three colors using this formula:
IIF(Fields!Time_Msg.Value = 1, "green",
IIF(Fields!Time_Msg.Value = 0
And Fields!Time.Value > Fields!TimeB.Value, "red", "yellow"))
So essentially if the record is on time the font will be green, if the record is late then red and early then yellow.
But what do you do when providing grouping sums? If from all the records the greatest value is green, then the font for the grouping sum should be green, etc.
Sorry for formatting as I'm new to this forum..
go to Textbox properties-> Font -> Color and type an expression like this
Instead of using IIF please try to use Switch statement.
= switch(Fields!Time_Msg.Value = 1, "green",
Fields!Time_Msg.Value = 0
And Fields!Time.Value > Fields!TimeB.Value, "red")
Note: Put your condition in switch statement.
Let me know if this works
You can use custom code in your project
Function SetColor(ByVal Value As Integer) As String
Dim Ret as string
Dim doubleVal As Double
Ret = "Blue"
Dim Val As Double
doubleVal = System.Convert.ToDouble(Value)
if Value >= .86 then
Ret = "OliveDrab"
ElseIf Value <=.86 then
Ret = "IndianRed"
end if
End Function
assign whatever numbers you want to whichever colours you are using (I'm using greater or less than 86%) and set the font colour expression to =Code.SetColor(Me.Value)
Here's my problem. I am passing in a parameter let say it's called ShapesSelected.
ShapeSelected = ",Square, Triangle, Circle,".
The problem is ShapeSelected could be any of the the shapes so it is never static.
Base on this parameter I want to add 3 column to the right of a table in the report. Is this possible? I've starting coding it in Custom Code in Report Properties but I am stuck as in how to add the column.
Public Function GetReportShapes( ByVal ShapesSelected As String )
Dim Shapes() As String
Dim result As String
Dim i As Integer
Entities = Split(ShapesSelected ,", ")
For i = 0 To UBound ( Shapes)
Select case Shapes(i)
case "Square": 'add Square Column here
case "Rectangle": add Rectange Column here
case "Triangle": add Triangle Column here
End Select
Next i
End Function
Thus rendering the columns like this:
Square Triangle Circle
Add all the columns you need and use the hidden or visible property (I forget which) with vb expressions to turn them on and off.