How can I use MS Access Expression to count unique values in multiple columns as shown below?
I used Countif in Excel to get the "Yes" counts in the status column and now I want to use MS Access expression to get the same results.
use the function to make a row aggregate.
chek this out
Public Function count_sum(col1 As String, col2 As String, col3 As String) As Integer
Dim count_yes As Integer
count_yes = 0
If (col1 = "YES") Then
count_yes = count_yes + 1
End If
If (col2 = "YES") Then
count_yes = count_yes + 1
End If
If (col3 = "YES") Then
count_yes = count_yes + 1
End If
count_sum = count_yes
End Function
call this function using the following query
SELECT col1,col2,col3, count_sum([col1],[col2],[col3]) as Status
FROM Table1;
you can also use this fuction in contionous form.
In status textbox add control source like this OR directly use the above query and select the control source as status.
=Nz(count_sum([col1];[col2];[col3]);0)
Related
I have a field called 'Specimen' that has entries formatted like 'CM-Z-01', 'TOR-XY-03', etc. I want to populate 2 additional fields in the same table, called 'TestType' and 'Axis', with the first two sections of the 'Specimen' entry, respectively. So 'TestType' would have 'CM' and 'TOR'; Axis would have 'Z' and 'XY'
I can do the first one just fine, with
UPDATE MechanicalData
SET MechanicalData.TestType = Left(Specimen,InStr(Specimen,"-")-1);
Is there an easy way to grab the middle portion?
You can use the Split function which returns an array with the string parts:
firstPart = Split("TOR-XY-03", "-")(0)
secondPart = Split("TOR-XY-03", "-")(1)
Make a function that you can call in your query based on this function. You cannot use Split directly as it is not supported in SQL.
Public Function SpecimenPart(ByVal s As Variant, ByVal partNo As Long) As Variant
If Nz(s) <> "" Then
Dim parts As Variant
parts = Split(s, "-")
If partNo - 1 <= UBound(parts) Then
SpecimenPart = parts(partNo - 1)
End If
End If
End Function
The parameter partNo is one-based (1 for first part).
UPDATE MechanicalData SET
TestType = SpecimenPart(Specimen, 1),
Axis = SpecimenPart(Specimen, 2);
I have a column (impact/hr) which gets populated after some calculations. Now I would like to assign each row a rank. For example Maximum dollar amt gets the rank of 1. Any tips to write an expression would be appreciated. Thank you.
Just add some custom code in Report-Report Properties...-Code:
Dim Rank AS Integer
Function GetRank() AS Integer
Rank = Rank + 1
Return Rank
End Function
In your tablix's detail cell where you want the rank, use the following formula:
=Code.GetRank()
Each time it processes that cell, it adds one to the rank and displays the result.
It may be your secondary option !
You can arrange the Impact\Hr in ascending order and the show the Rank by using RowNumber function.
For More info see the RowNumber function.
Use the Rank function.
select [Impact per Hr], [My Rank] = Rank() over (order by [Impact per Hr] desc)
then use asc/desc depending on which value you want to be ranked high or low.
This is independent of any order by statement as well
Rank() is preferable to Row_Number() for applications like this because rows of the same value will have the same Rank, but will have a different Row_Number.
Step 1: Add the following code in code section of reports properties in Reports Builder.
Public dim previousValue as Integer = 0
Public dim i as Integer = 0
Public dim flag as Boolean = False
Public dim lastRank as Integer = 1
Public Function Rank(sourceValue as Integer ) As Integer
if previousValue = sourceValue
i = i + 1
flag = True
end if
if previousValue > sourceValue
lastRank = lastRank + 1
flag = False
end if
previousValue =sourceValue
if flag = True
return lastRank
else
lastRank = lastRank + i
i = 0
return lastRank
end if
End Function
Step 2: Call the above function from the cell where you want to display rank like this:
=code.Rank(SUM(Fields!SumTotal_Ending_Balance.Value))
How can I select the maximum value from a column in a listbox, and display that value in a textbox on the same form? The listbox itself is populated by a query that depends on user inputs, so its values are unknown in advance.
I could sort the listbox by value and select the first value, but it is already sorted by date on another column, for a different purpose. What I want to know is the Date on which that maximimum value occurred in column 2.
The next step is to display or all the values in column 4 which occure before that date as blank or N/A.
You may find the following VBA code helpful. It scans values inside the .Column data for a list box named List0, for example...
2013-04-18 | 123
2013-04-17 | 77
2013-04-16 | 345
2013-04-15 | 34
...finds the date (first column) corresponding to the maximum value in the second column of the list box, and puts that date into a text box named Text3. Note that the CompareNumeric flag controls whether the comparison is string-based ("77" would win), or number-based (345 would win).
Private Sub Command2_Click()
Const DateCol = 0 '' column numbers start with 0
Const MaxCol = 1 '' second column has column index of 1
Const CompareNumeric = True '' convert strings to numbers for finding maximum
Dim RowIdx As Long, MaxItem As Variant, MaxIdx As Long, CurrItem As Variant, NewMaxFound As Boolean
MaxIdx = -1
MaxItem = Null
For RowIdx = 0 To Me.List0.ListCount - 1
CurrItem = Me.List0.Column(MaxCol, RowIdx)
If CompareNumeric Then
CurrItem = Val(CurrItem)
End If
If IsNull(MaxItem) Then
NewMaxFound = True '' first one
Else
NewMaxFound = (CurrItem > MaxItem)
End If
If NewMaxFound Then
MaxItem = CurrItem
MaxIdx = RowIdx
End If
Next
If MaxIdx >= 0 Then
Me.Text3.Value = Me.List0.Column(DateCol, MaxIdx)
End If
End Sub
I have a table with two columns (value1 and value2) the values are sorted from lowest to highest. example:
Value1
20
40
43
90
100
122
Value2
4
5
9
10
15
18
I ask the user to enter an input value and then I calculate the value of CalcFinalValue which can be calculated in one of the following:
if the user input value already exist in value1 field, then return the corresponding value of in field value2. for example if the user input is 100 then CalcFinalValue will be 15
if the user input value does not exist in value1 field, then locate the two values in value1 field that the input value is between them(for example if the input value is 42, the I want to locate 40 and 43 from value1 field). Calculate CalcFinalValue as:
CalcFinalValue=(40*9)+(43*5)/42
in other words the formula will be as:
CalcFinalValue=(LowerValue of the inbetween values *lookup value of the HigherValue of the inbetween values)+(HigherValue of the inbetween values *lookup value of the LowerValue of the inbetween values)/(user input value)
I want to perform this in Access 2007 VBA.
I hope this is clear. Thanks for your help in advance!
Dim rs AS DAO.Recordset
Set rs = CurrentDb.OpenRecordset("TableName", dbOpenTable)
' inp stores the user input value; i counts the number of records that have been accessed
Dim inp, i as Integer
' r2c1 and r2c2 store the data of the second row in case the two-row calculation is needed
Dim r2c1, r2c2 as integer
' Since I mostly use forms for I/O that's what I've used here
' Change this to whatever method you use to get the user input
inp = Forms!FormName.InputTextBoxName.Value
i = 0
rs.MoveFirst
Do While (Not rs.EOF)
i = i + 1
' Check if the user input exists in the table
If (rs.Fields("Value1") = inp) Then
' Again use whatever output method you want
Set Forms!FormName.OutputTextBoxName.Value = rs.Fields("Value1")
End Do
' Otherwise, check if the current value in column 1 is greater than the input
Else If (rs.Fields("Value1") > inp) Then
' If this is true for the first row then the input is less than the lowest number.
' I assume this is out-of-bounds, but use whatever boundary condition you want
If (i = 1) Then
MsgBox("Out of Bounds", vbOkOnly)
Else
' Save the required values from this row
r2c2 = rs.Fields("Value2")
r2c1 = rs.Fields("Value1")
' Goto previous row which and calculate according to your formula
rs.MoveLast
Set Forms!FormName.OutputTextBoxName.Value = (r2c1*r2c2 + rs.Fields("Value1")*rs.Fields("Value2"))/inp
End If
End If
rs.MoveNext
Loop
' If nothing was found, the input was larger than all values in 'Value1'
If (rs.EOF) Then
MsgBox("Out of Bounds", vbOkOnly)
End If
Substitute Value1 and Value2 with whatever column names you use
Hopefully someone can help me out with this. I have written a query in Access 2003 that combines a linked table "taxon_group_max_per_site" and a cross tab query "Distinct Species by group_Crosstab".
From the table I have the field "Taxonomic Group" and "Max", and from the cross tab the fields "Total_Of_Species_S".
The table and the cross tab are linked and the query works fine until I add in some VBA to give each Taxonomic group a score based on "Max" and "Total_Of_Species_S".
The code below brings up "Error 13: type mismatch"
Public Function Invert_Diversity_Score (Total_Of_Species_S As Integer) As Integer
If Total_Of_Species_S < Round("[Max]*0.5", 0) Then
Invert_Diversity_Score = 0
Else
If Total_Of_Species_S < Round("[Max] * 0.75", 0) Then
Invert_Diversity_Score = 1
Else
If Total_Of_Species_S < Round("[Max] * 0.875", 0) Then
Invert_Diversity_Score = 2
Else
Invert_Diversity_Score = 3
End If
End If
End If
End Function
The debugger shows that "[Max]*0.5" and the other multiplications do not produce a number it says "[Max] * 0.5"= "[Max] * 0.5", which I think is the source of the type mismatch. How do I get the field to multiple properly? It looks exactly like the format shown in the VBA help.
The round function is expecting a number as parameter, not a string! Assuming (max) is a number, you can then calculate:
Round([Max] * 0.75, 0)
But
Round("[Max] * 0.75", 0)
Will definitely not return anything viable
"[Max] * 0.875" is just a string, how is VBA supposed to know that you are referring to the column [Max] from one of your tables?
Shouldn't [Max] be passed into the function as a second integer parameter? Something like this:
Public Function Invert_Diversity_Score (Total_Of_Species_S As Integer,
MaxVal as Integer) As Integer
We need the code that shows how you are calling the function to really sort this out ...
For one, you should use the ElseIf keyword, there is no need to stack the Ifs here.
Second - what is "[Max]*0.5" supposed to mean? To VB, it is a string, which unsurprisingly you can't multiply with an integer.
Assuming Max is some kind of global constant:
Public Function Invert_Diversity_Score(Total_Of_Species_S As Integer) As Integer
If Total_Of_Species_S < Round(Max * 0.5, 0) Then
Invert_Diversity_Score = 0
ElseIf Total_Of_Species_S < Round(Max * 0.75, 0) Then
Invert_Diversity_Score = 1
ElseIf Total_Of_Species_S < Round(Max * 0.875, 0) Then
Invert_Diversity_Score = 2
Else
Invert_Diversity_Score = 3
End If
End Function
If it is not a constant, then you must pass it into the function as well:
Public Function Invert_Diversity_Score( _
Total_Of_Species_S As Integer, _
Max as Integer _
) As Integer