MS Access: lookup values for string-based multi-value field - ms-access

Here is my situation:
linked table (so I can't actually modify the column config) has a column with a comma-separated list of values. this is a text string -- not a true multi-value field.
I need to map those values to a lookup table and return a comma-separated list using the lookup values
So let's say a row has the values: A,B,C in this column. The mapping table maps: A|1, B|2, C|3. The resulting column in my query should list 1,2,3 (the mapped values).
I can add a column in my query, indicate the display control is combo box (which is what you would typically do for a multi-value field), select my row source, and bind the columns -- but only rows with a single option value will map correctly. If it has a comma-separated list, per the above example, it does not map the values. How would I go about this mapping?

To display such results, you would need a VBA function.
The code would look like:
Public Function TransComma(MyList As Variant) As String
Dim TransList As Variant
Dim Token As Variant
Dim result As String
If IsNull(MyList) = False Then
TransList = Split(MyList, ",")
For Each Token In TransList
If result <> "" Then result = result & ","
result = result & DLookup("Color", "tblColors", "ID = " & Trim(Token))
Next Token
TransComma = result
End If
End Function
The above code is to be placed in a standard code module.
Now in a form you can place a text box, and set the control source to this
=(TransComma([name of field]))
And for a report, or sql query, simply go:
Select firstName, LastName, ColorList, TransComma([ColorList)
as translated from tblCustomers
So once you build this translate function, it can be used translate the numbers to some text.

Related

Obtain list of occurring field values in report footer - ssrs

I'm trying to obtain a list of the different field values within an SSRS Report, and then display the result set on the page header.
See link below for an example.
I know that usually this would be completed simply with groups but the user does not want the Tasks split out.
Thanks
Example Report:
To get the unique values into a single cell, you will need to utilize the code behind functionality in SSRS.
Click on the background of your report and go to report properties -> Code. Paste this function into the window:
Public Shared Function RemoveDuplicates(ByVal items As Object()) As String()
System.Array.Sort(items)
Dim k As Integer = 0
For i As Integer = 0 To items.Length - 1
If i > 0 AndAlso items(i).Equals(items(i - 1)) Then
Continue For
End If
items(k) = items(i)
k += 1
Next
Dim unique As String = New [String](k - 1) {}
System.Array.Copy(items, 0, unique, 0, k)
Return unique
End Function
In the expression in your table where you want the unique values list, insert this expression:
=Join(Code.RemoveDuplicates(LookupSet(1,1, Fields!ID.Value, "DataSet1")), ",")
"Fields!ID.Value" is a reference to the field in your dataset that you want unique values from.
"DataSet1" is the name of the dataset that your field is located in.
What happens is that at runtime, the entire set of values from your query column are passed to the VB function which uses returns an array of unique values. The expression in the report Joins the unique array values back together with a comma deliminator.

filter dropdown in sqlform,'Query' object is not callable

I want to insert a form include two fields:coursename,teacher_id(reference auth_user),but the teacher_id field show all the id in auth_user,I only want the user's id whoes role=teacher to show in the dropdown.as all the users are in the same table(auth_user)
this is what i have done:but it show the error:'Query' object is not callable.
query=(db.auth_membership.user_id==db.auth_user.id)(db.auth_membership.group_id==db.auth_group.id)(db.auth_group.role=='teacher')
db.courses.teacher_id.requires=IS_IN_DB(db(query),'auth_user.id')
form=SQLFORM(db.courses)
return dict(form=form)
Thanks
jian
query=(db.auth_membership.user_id==db.auth_user.id)(db.auth_membership.group_id==db.auth_group.id)(db.auth_group.role=='teacher')
The above is not valid syntax. A Set object is callable, and when called, it returns another Set object, so you can chain Sets:
myset = db(query1)(query2)(query3)
If you instead want to work with Query objects, you have to make explicit conjunctions using the "&" operator:
myquery = query1 & query2 & query3

Display the non selected parameter in SSRS

In case of Multi-valued parameters,we usually use join function to display the selected values into a Text-box.But what if I wanted Show only the non selected parameters?IE If there are 10 values in the drop down list of a parameter and I selected the first 5 and wanted to display only the remaining 5 parameter instead of the first 5.What do i do?
I have created a multivalue parameter with the name Param which has had its labels and values set like so:
Label Value
====== =====
Label1 1
Label2 2
Label3 3
Label4 4
Label5 5
I then created the following code in the Report Properties --> Code menu:
'Global array objects to hold the total and selected values
Private Dim parameterList() AS string
Private Dim selectedParameters() AS string
'populates the list of all parameters using split and returns the input string
Public Function SetParameterList(nextParameter as String) AS String
parameterList = Split(nextParameter ,",")
Return nextParameter
End Function
'populates the list of selected parameters using split and returns the input string
Public Function SetSelectedParameters(delimitedParameters as String) AS String
selectedParameters = Split(delimitedParameters,",")
Return delimitedParameters
End Function
'Returns the not selected parameters
Public Function GetNotSelectedParameters() AS String
Dim notSelected As String
Dim i as Integer
Dim x as Integer
'Loop through each value in the all parameters array...
For i = 0 to parameterList.GetUpperBound(0)
'...for each one of those values check it against the selected parameters
For x = 0 to selectedParameters.GetUpperBound(0)
'Where there is a match, set the all parameters value to a string unlikely to be a genuine parameter value
IF parameterList(i) = selectedParameters(x) Then
parameterList(i) = "!*!"
End IF
Next
Next
'Join the all parameters array back into a string
notSelected = Join(parameterList, ", ")
'Remove the !*! values added earlier from the middle and the end of the string
notSelected = Replace(notSelected, "!*!, ", "")
notSelected = Replace(notSelected, ", !*!", "")
Return notSelected
End Function
To use this code I created 3 textboxes with the following expressions:
=Code.SetParameterList(Join(LookUpSet(1,1,Fields!ParamLabel.Value,"DataSet1"),","))
=Code.SetSelectedParameters(Join(Parameters!Param.Label, ","))
=Code.GetNotSelectedParameters()
Note: to hide the output of any of these textboxes, you could set the function return value to be "".
I imagine my code could be improved upon significantly, but this gets the job done and should at least point you in the right direction.
First create a multivalued parameter ("param1"), with available values ranging from 1 to 10.
Then create a query (query1), which returns the parameters from 1 to 10 filtering out the
selected values from "param1" -> where query1.col NOT IN (#param1)
Then create another multivalued parameter("param2"), set a default value (get values from a query) point to "query1" to fill in the unselected values
use a text box with the following code "=Join(Parameters!param1.Value,",")"
To make query1 you can use unions.
You will get back the values not selected,

Multi-value parameters in ssrs

I have a multi-value parameter. how I get one by one values from this parameter.
value=new_index and label=new_french
and want to insert these values into these labels
You can access the individually selected multi-value parameters by their index (the index is zero-based). So if you want the first selected parameter value (for example, to put it into a label), you can address it like so:
=Parameters!MyParameter.Value(0)
You could access them all using custom code:
Function DoSomething (ByVal parameter As Parameter) AS String
Dim Result As String
If parameter.IsMultiValue then
For i As integer = 0 To parameter.Count-1
Result = Result + CStr(parameter.Value(i)) + ", "
Next
Result = Left(Result, Result.Length - 2)
Else
Result = CStr(parameter.Value)
End If
Return Result
End Function
then use this expression to access the result:
=Code.DoSomething(Parameters!MyParameter)
Note that you are passing the parameter object here, not the Value property. We access the Value property in the custom code function.

NOT IN in SSRS TextBox

How can I write NOT IN in TextBox expression?
I must check if some field value not belong to some list of strings, and then do some work.
Example:
Iif(SomeField.Value NOT IN ('Text1', 'Text2'), DoSomething, nothing)
I wrote code like this and got error when previewing report, and error was :
Overload resolution failed because no accessible 'Iif' accepts this number of type arguments
How can I do this stuff?
Try this small piece of custom code that accepts a string array. Just paste it into the report code section of the report..
Public Shared Function ValueExists(ByVal arr() as string, checkVal as string)
Dim i As Long
For i = LBound(arr) To UBound(arr)
If arr(i) = checkVal Then
return true
Exit Function
End If
Next i
return false
End Function
Usage would involve splitting the string into an array using the Split function
like so:
=iif(Code.ValueExists(Split("Your,comma,separated,string,in,here",","),"StringYouWantToFind")
,"Your value exists"
,"your value does not exist")
You can simply write the code like this:
Iif(SomeField.Value <> 'Text1' AND Field.Value <> 'Text2' , DoSomething, nothing)
I got this one in one report:
=iif(join(Parameters!Parameter1.Value,",") like "*" & Fields!Field1.Value & "*","Color1","Color2")
This instruction helps me to determine the fill colour of a cell inside a tablix, where:
Parameter1 is a multivalue parameter.
"Join" lets me have a string with all selected values from a multivalue parameter, eg. "value1,value2,value3,value4"
Field1 is the field that contains the values filtered by Parameter1
Color1 is the color if the value of the cell is included in the selection of parameter
else Color2
works well