Concatenate name fields in an Access query using IIF() without printing spaces? - ms-access

First of all, I am a newbie. Please keep that in mind...
That said, I have used a simple formula to concatenate name fields in a student database in Access using a query (I am using Access 2013 but must maintain compatibility with Access 2010). It reads as such:
Student Name: IIf(IsNull([Preferred First Name]),[First Name],[Preferred First Name]) & " " & [Last Name]
Which shows the student's preferred first name and last name, or first name and last name. If it turns out first (preferred or given) is null and/or last is null, obviously it prints null where appropriate.
Example of formula output using fictional names
Well, that is where the issue comes in. As you can see in the example, one student only has first name Christine, which prints as "Christine ". Another only has last name Alexander, which prints as " Alexander". It is printing the space no matter what.
Now I now it is possible using Iif to get around this -- it's used in a sample database like so:
Student Name: IIf(IsNull([Last Name]),IIf(IsNull([First Name]),[First Name]),IIf(IsNull([First Name]),[Last Name],[First Name] & " " & [Last Name]))
I want to incorporate the "Preferred First Name" field into that formula, but I just can't wrap my head around it. It seems to work backwards from how I would say this in natural speech, and it seems to be missing values. In plain English I envision it like this:
If [Last Name] is null, print only [Preferred First Name], unless that is null, in which case print only [First Name], unless that is also null, in which case print null.
Otherwise if [Last Name] has a value, print [Preferred First Name] & " " & [Last Name], unless [Preferred First Name] is null, in which case print [First Name] & " " & [Last Name], unless [First Name] is also null, in which case just print [Last Name].
(To clarify, there are sometimes cases where we are unable to get a student's full name, so I can't simply make all fields required, which is why I want to make sure it prints correctly regardless of what data is available.)
I apologize if this is too simple a question for this site, but it seemed like the best place to ask. Thank you for any help.

You want to use the Trim function. So, your string would look like:
Student Name: Trim(IIf(IsNull([Preferred First Name]),Trim([First Name]),
Trim([Preferred First Name])) & " " & Trim([Last Name]))
Trim each individual field (just in case the data got entered with an extra space or 2, which happens occasionally), and then trim the entire resultant string to get rid of any spaces as well.

Related

How to concatenate three fields in table into one table?

I would like to concatenate three fields in my MS Access table into one field by using VBA. How can I do this?
I have tried using query to concatenate it and it works, but I would like it to be concatenated and saved in my table instead.
My 3 fields I want to concatenate into 1 field are: CompanyCode,YearCode and PO number.
Currently my table look like this:
Company code YearCode PONumber
ABC 17/ 200
What I want:
PONumber
ABC17/200
Use a query:
Select *, [Company Code] & [YearCode] & [PONumber] As FullNumber
From YourTable
If you insist on having a calculated field in the table itself, use this expression when you - in design view - add the calculated field:
[Company Code] & [YearCode] & [PONumber]
As mentioned #Andre, highly not recommended to use calculated fields in Access tables at all, this feature is quite buggy. But in some cases it's reasonable to store combined code in separate regular field for performance improvement, despite this denormalizes the database structure. You can store combined code in form's BeforeUpdate event:
Me!PONumberFull = Me![Company code] & Me!YearCode & Me!PONumber
Also I'd recommend to do not store "/" in YearCode, just number. In this case the code will be
Me!PONumberFull = Me![Company code] & Me!YearCode & "/" & Me!PONumber

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.

Reusing Parameters Passed to Query to Generate Form

I have a form in MS Access that uses a query to generate a form. The query looks something like this:
SELECT *
FROM PEOPLE
WHERE LAST_NAME LIKE [Enter Last Name] & "%";
This of course creates a pop-up to enter the last name to search. I want to do an action on the parameter [Enter Last Name], but I cannot figure out how to access this parameter. In a report I just set the value of a textbox to =[Enter Last Name]. Can anyone help me figure this out?
One thing you could do would be to add the parameter as an output column, e.g.
PARAMETERS [Enter Last Name] Text ( 255 );
SELECT People.*, [Enter Last Name] AS theParameter
FROM People
WHERE (((People.LastName) Like [Enter Last Name] & "*"));
Then you could refer to the [theParameter] column in the result set.

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.

Concatenate two columns in an Access table

very simple one. I have got two fields in a table called [First Name] and [Last Name]. I would like to add a new column that is just a combination of [First Name] and [Last Name] - is there a way to do this in Access? I know this can be done in Excel using the Concatenate function.
I would like to do this in the existing table and not in a new query.
Thanks!
As #paxty says, do not do this. You have a simple answer in Access that is not available in Excel, and that is a query. You can base any output that requires that the two names be concatenated on a query.
SELECT FirstName & " " & LastName FROM MyTable
In Access 2010 you can create a "calculated field" for your table; Access applies a formula to create the content of the field. You can enter a formula such as:
FirstName & " " & LastName
You could even do fancier things like have an initial then the last name, if there is a last name, or else show the complete first name, using the Access IIf() and Len() functions.
IIf (Len(LastName) > 0, Left(FirstName, 1) & ". " & LastName, FirstName)
By using a calculated field to join the two names directly in the table... It creates issues down the road. I have two fields First & last name I used a calculated field in the table to join the two together. When you try do a report with those calculated names, it will show only the employee number, not the names. Now I am trying to correct my mistake without having to rebuild the multiple tables I have used this field as a look up in.