MS Access Calculated Field - Creating dynamic expression - ms-access

I need to be able to create an ID CODE from the plant name to be in the format of AAAA_AAAAAAAAA_A00000 (A for alpha). Some thing like this;
Acacia dealbata
ACAC_DEALBATA_T00001
I can do the first section using Left([FieldName],4) but the middle section I'm having trouble with as it will differ depending on the remaining number of words in the name.
Acer palmatum Atropurpureum would become ACER_PALMAATRO_T00002
Acer palmatum Dissectum Rubrum would become ACER_PALDISRUB_T00003
My attempt at the middle section is;
IIf(Len([FieldName]) - Len(Replace([FieldName], " ", "")) + 1 = 2, Mid([FieldName], InStr(1, [FieldName], " "), (9 / (Len([FieldName]) - Len(Replace([FieldName], " ", ""))))), "")
And returns the following Error Message.
The end section depends on the SUPPLIER of the image of the plant and is given a specific prefix; L, H, or R. Followed by the records autonumber in the table. This prefix is stored in another table I have called SUPPLIER.
I do not know how to collect values from fields of other tables into another so any advice on this is appreciated.
If anyone is able to help please.

Related

SSRS 2008 - How to link 2 or more year parameters into 1

After some research, I see that blending parameters is a popular topic but I haven't found a solution to this specific situation. Shortly said, I have 3 parameters from various datasets all stating the same year and I want to blend them (visually) into 1 so to eliminate this redundancy for my report's end-user.
In the "Available values" from "Parameter properties", all the values are taken from a dataset (hidden) and the "value field" is [Measures].[ParameterValue] from their corresponding hidden dataset.
When I "show hidden datasets" and go into "text mode" of each of these hiden datasets are these are the 3 MEMBER [Measures].[ParameterValue] AS :
[Time].[Financial Year].CURRENTMEMBER.UNIQUENAME
[Financial Period].[FP - Year].CURRENTMEMBER.UNIQUENAME
[Time exit].[Financial Year Exit].CURRENTMEMBER.UNIQUENAME
When you look at what value this gives, they have the following output (accordingly):
[Time].[Financial Year].&[2017]
[Financial Period].[FP - Year].[FP - Year].&[2017]
[Time exit].[Financial Year Exit].Financial Year Exit].&[2017]
A. So, in my opinion, since the "roots" of each [Year] parameter value are different I need to create a dataset or a member within each of these hidden datasets that replaces this [2017]. Is that "doable" and, if yes, any guidance on how?
B. If not, another outcome would be acceptable as well: the end user would need to generate mostly current year and current year-1. To tackle this I made a Template "Year" DS with the same content as in FP - Year and added the following members:
Member [Measures].[Last Year] as 'YEAR(NOW())-1'
Member [Measures].[Current Year] as 'YEAR(NOW())'
I inserted them into SELECT and previewed, it displays 2 new columns with 2017 and 2016. Then I went and changed in that PF-Year parameter's DS, value and label and I get an error. Any suggestions?
Any suggestions on this?
EDIT1: I have tackled B. and found a solution to it, I simply created the following 2 Default values for each parameter:
Current year: ="[Time].[Financial Year].&[" + CStr(Year(Now())) + "]"
Last year: ="[Time].[Financial Year].&[" + CStr(Year(Now())-1) + "]"
However, even though with this result I presume it will minimize the number of actions for the end-user, I would still prefer having 1 parameter showing instead of 3...
EDIT2 & partial solution I found another solution for A., it's not exactly what I wanted but it kind of gets the job done.
Created a new Year parameter with 2 available values: [CurrentYear] and [LastYear]
For each of the default values of each other parameter, I swaped the 2017 and 2016 values for CStr(Parameters!Year.Value(0)) and for CStr(Parameters!Year.Value(1)) accordingly. Example: ="[Year exit].[Financial Year exit].&[" + CStr(Parameters!Year.Value(0)) + "]".
In the options of each parameter, I set them as "Hidden" expect for my new Year parameter.
Now, I have 1 Year parameter with a dropdown list displaying Current Year and Last Year.
Even though this alleviates the issue, is there anyway to have a similar outcome but with the drop down menu displaying the actual years? i.e. it would be dynamic so in years to come, there would be 2018, 2019, etc. that would pop up? This way the end user would be able to compare not only current and past year but any 2 years?
Thanks to all!
EDIT3 Here is a screen shot of where I am at, at this point:
EDIT4
I have arrived to a fix: since I know that the end-users will only be comparing 2 years at a time (so 2 default values per parameter), I left hidden 2 parameters out of 3 and for the 2 hidden, I changed their default values.
So, I kept #[Financial Year] as my main parameter and the others have the following default values:
#[FP - Year]:
="[Financial Period].[FP - Year].[FP - Year].&[" + CStr(Parameters!Financial Year(0)) + "]"
="[Financial Period].[FP - Year].[FP - Year].&[" + CStr(Parameters!Financial Year(1)) + "]"
#[Financial Year exit]:
="[Year exit].[Financial Year exit].&[" + CStr(Parameters!Financial Year(0)) + "]"
="[Year exit].[Financial Year exit].&[" + CStr(Parameters!Financial Year(1)) + "]"
And voilĂ  the result:
I'd suggest two ways:
1: SSRS: add an invisible parameter (i.e. CurrentYear) and run in your MDX query the following:
StrToMember('[Time].[Financial Year].&[' +#CurrentYear + ']')
2: MDX: add a dynamic calculated members for each hierarchy:
member [Time].[Financial Year].[Current Year] as
StrToMember('[Time].[Financial Year].&[' + Format(Now(),'yyyy') + ']')
See my blog post for more details.

How do I split a full name field (Report Builder)

I have a name field that displays as "Mr A Test" for every record (eg. Title initial Surname)
I need to split the full name into 3 Individual fields, Title, Initial and surname.
Is anyone able to offer any assistance? I'm fairly new to Report Builder so I have already exhausted my skill base and my searches here have so far been unsuccessful.
Thanks in advance
Can you not arrange your query to provide individual fields? This would be best.
If not, one way might be to use the split function in the expression builder eg
=Split(Fields!Fullname.Value, " ")
This results in an array of strings separated by the space. You can then access each element thus
=Split(Fields!Fullname.Value, " ").GetValue(x)
where x is a zero based index

SSRS Reporting Expression to Split Full Name Field

I've been trying to get this to behave with no luck.
The database base field only has FULL NAME field, which includes Middle initials on some cases and no middle initials in some other cases.
I've been trying to display LastName, FirstName by using this ssrs expression below: While this works in names that doesn't have middle initials or name on the field.... for those names that includes middle initials/name, we run into issues with displaying.
=Right(Fields!agent_name.Value,Len(Fields!agent_name.Value)-Instr(Fields!agent_name.Value," ")) & ", " & (Left(Fields!agent_name.Value,Instr(Fields!agent_name.Value," ")))
But it includes the Middle Initials to display first.. As an example:
If the fullname field is John S Doe, above expression displays as:
S Doe, John
What I need to display is:
Doe, John
How do I set my expression to get rid of the Middle initial/name to display?
I've done a great amount of research and tried many diff expressions but no luck.. thanks in advance.
While slightly inefficient, this is a clean way to accomplish the result:
=Split(Fields!agent_name.Value," ")(Split(Fields!agent_name.Value," ").Length-1) + ", " + Split(Fields!agent_name.Value," ")(0)
Edit: The above indeed does not work with suffixes. The expression below will remove a middle initial if its the second name and has a length of 1, which you could use in a calculated field. But it would fail for first name "Ann Marie" and the first/last swap would need to be handled separately.
=IIF(Split(Fields!agent_name.Value," ")(1).Length=1,Replace(Fields!agent_name.Value, " " + Split(Fields!agent_name.Value," ")(1) + " "," "),Fields!agent_name.Value)
The short of it is that you really would be best served handling this in code. But even so, if your fullname field is space-inclusive beyond what you want to seperate on there arises the potential for ambiguous cases, like the suffix- and initial-less "Ann Marie van Der Beek".
Try using this expression:
=RIGHT(
Fields!agent_name.Value,LEN(Fields!agent_name.Value)-InStrRev(Fields!agent_name.Value," "))
& ", " & LEFT(
Fields!agent_name.Value,
InStr(Fields!agent_name.Value," ")-1
)
It is a native SSRS solution however I recommend using custom code to get an maintainable solution.
UPDATE: Regex solution:
=System.Text.RegularExpressions.Regex.Replace(
Fields!agent_name.Value,
"(\w+)\s+(\w+\.*\s)?(\w+)",
"$3, $1"
)
Let me know if this helps.

How to iterate over table in Access 2010

I would like to iterate over the rows in my Payment table. User chose for what month and year has wants to book and I want to check in the each raw if this house was booked for this year and month. I want to compare if
HouseID == chosenHouseID && BookingMonth == chosenBookingMonth &&
BookingYear==chosenBookingYear.
If this is true it should pop out message box with info that house was already booked for this month. Also if user chose more than one month i.e. numMonths would be 3, it should increment value of the month (which is a text) it should go to the next value (if there is no next value then it should do mod 12) and do the checking again. Maybe it will be necessary to switch data type of BookingMonth to numeric?
However I hope I was clear what I want to do. I have experience with Java, C, Python and Visual Basic, but I did not do much in Access so it is quite confusing. I could not find the any useful info how to perform this operation. Please advise me on my issue.
Thank you
Yes, you definitely should store the [BookingMonth] as numeric. Maintaining a "month" column as text will be a nuisance in the long run, since "August"<"January" and "12"<"2". You'd have to do at least a certain amount of juggling to convert the text values to numeric values, so make like easy for yourself and just maintain them as numeric. (Note that you can always format them as text if you want to use them in reports.)
As for your search requirements, if the user supplies a [chosenBookingYear], [chosenBookingMonth], and [numberOfMonthsToBook] then you can use the VBA DateAdd function to derive [endOfBookingYear] and [endOfBookingMonth] safely, accounting for "next year" values...
endOfBookingYear = Year(DateAdd("m", numberOfMonthsToBook - 1, DateSerial(chosenBookingYear, chosenBookingMonth, 1)))
...and...
endOfBookingMonth = Month(DateAdd("m", numberOfMonthsToBook - 1, DateSerial(chosenBookingYear, chosenBookingMonth, 1)))
Finally, to perform the lookup without looping through individual rows you can concatenate [BookingYear] and [BookingMonth] together to create something like "2013/05" using...
BookingYear & "/" & Format(BookingMonth, "00")
...so then you can create a SELECT query something like this:
SELECT * FROM Payment
WHERE HouseID = chosenHouseID AND
(
(BookingYear & "/" & Format(BookingMonth, "00"))
BETWEEN (chosenBookingYear & "/" & Format(chosenBookingMonth, "00"))
AND (endOfBookingYear & "/" & Format(endOfBookingMonth, "00"))
)

Access / DCount gives a TExt field and not INT field?

In Access 2010, I have two tables 'Contact' and 'PhoneCalls'.
I created this Query for 'Contact' as I want to see how many times I called a contact.
Query:
SELECT Contact.*, DCount("[ID]","ColdCall"," [ColdCall]![ContactID] = " & [Contact.ID]) AS Call
FROM Contact
I have build this query using the following expression:
Call: DCount("[ID]","ColdCall"," [ColdCall]![ContactID] = " & [Contact.ID])
It works fine except that it creates a TEXT field instead of a NUMBER field. For instance, I need to sort this query out, but I can only sort it "A to Z" and not "smallest to largest" as it should be.
Do you have any idea on how I can solve that?
You could use CInt() to force the call count to be an integer:
SELECT Contact.*, CInt(DCount("[ID]","ColdCall"," [ColdCall]![ContactID] = " & [Contact.ID])) AS Call
FROM Contact;
Note also that using DCount() in this way is rather inefficient. If that approach works to your satisfaction then continue to use it for now, but don't be surprised if it starts to bog down as the tables grow.