I'm working in MS Access. I have a field of names. Some of them have three names (their first, middle, and last). How would I write a query to extract all characters before the second space so that I will be left with just their first and middle name?
If I have a name like John Joe Doe, I just want John Joe portion
Any help would be appreciated. Thank You.
Nick
You can use InStrRev to find the position of the last space and then use Mid to extract the first 2 names. Open the query design and inside one of the field, you can click the expression builder to add InStrRev and Mid functions.
Assuming that your table has 2 fields, ID and UserName. Open the query designer and add this table. Then add both of the 2 fields to the query and add a third field as NewName. Below is how it should look like on the query design. Hope this is clear.
Related
Ok so I have a super simple database I am using to try to implement things for a more complicated database however I keep hitting walls on what seems like simple things
The database has one table contactsT with the fields ID first_name last_name I have one form with two combo boxes cboFirstName and cboLastName. I have some repeat first names and I only want the combo box to show unique names. I found this absolutely by the numbers tutorial,
https://www.techonthenet.com/access/comboboxes/unique_values2013.php
however it doesn't work which is baffling because it seems very simple. All that happens is I get nothing now showing in the combo box. Don't see an option to attach the database but here is the table
ID first_name last_name
1 Oliver North
2 Oliver Twist
3 Ren Saturn
4 John Smith
5 John Ringo
the Row source is
SELECT DISTINCT contactsT.first_name FROM contactsT
Your assessment is correct, DISTINCT does not work if you create a ComboBox and use the wizard to link it to a specific source.
The easiest way to bypass this is to simply create a ComboBox, hit cancel on the wizard and then type the query in manually into the property sheet.
This should solve your problem.
FYI: This is due to the fact that Access will try and base the ComboBox on the ID field by default, because of that, the fields you are seeing are technically unique, but only based on the the ID column.
Additionally, this can be circumvented in another method, perhaps you would prefer this (although I find it to be more difficult and slightly frustrating):
You can go into the Property Sheet of the ComboBox and change the Column Widths to include only one column, it most likely currently looks something like 0";1". So to fix it, remove the 0, like 1", this is where Access will "hide" the ID field on the ComboBox. If you then manually edit the query to be DISTINCT for the field you are looking for, that should also fix the problem.
I have a column in a table that contains people's names. I want to create a computed column that removes all the spaces in a given person's name and then takes the first four characters of their name.
Please see attached screenshot for an illustration. Column A contains people's names. What I have managed to end up with is in Column B (which is incorrect) by using the Left function. As you can see, it includes the spaces as one of the four characters. What I want is in Column C.
When I tried to create a computed column using the Replace function to replace " " with "", MS Access throws up an error stating that the expression cannot be used in a calculated column. Hence, I am unable to combine the Replace function with the Left function.
Help needed. Much appreciated!
I feel like this is only a part of what you're trying to accomplish with these names (and perhaps an XY-Problem2). For example, what are you going to do about multiple employees with the same abbreviation?
Anyhow...
The right way (via query)
SELECT Left(Replace([Name]," ",""),4) AS NewName FROM Table1;
How you want to do it (via calculated field)
I spent a 20+ minutes trying various ways to get this working in a calculated table field (to provide an alternate, but not-recommended, solution) before giving up. I couldn't find any combination of built-in functions that will work since various functions are simply not available in calculated fields, for various reasons.
"Calculated fields belong in queries, not tables."
— Quote from Allen Brown, Microsoft MVP and "t̲h̲e̲" Excel Guru.
There is a section of this page titled "You want to store a calculated result anyway?"
(...but don't come crying to us when you regret it later!) ☺
You're not the first person who was determined to use calculated fields in an Access table.
Here's a few thousands posts from people who decided it was a terrible idea.
(I heard a rumour that Microsoft is planning of finally going to remove Calculated from the table Data Type drop-down with the next release of Access.)
André and Tim are right: Use a query and an expression similar to Tim's:
Select *, Left(Replace([Name], " ", ""), 4) As Name4
From YourTable
How do I split a table into two tables? For example, I have a form of classes (in school) and semi form of students that study in the same class (just first and last name), and made a report for this form that above there are the details of the class and below there is a table of all the students with their name and last name. I wanted to ask how do I split the table to not continue page number 2
if I have alot of students so when the table reach the end of the page it will continue on the other side of the page (small table)?
I don't think you actually want to split the tables if you are designing a report. What you want to do is adjust the report so that the field for Class the students are studying is in a group header. In design mode, click "add a group" and then select the Class field. Then below that is where you will want to put the first and last names in the detail part.
You can also adjust where the pages split and you will need to play around with the design view options to get exactly what you want.
Ultimately, I think you want your report to look something like this:
Geometry
Josetta
Scott
John
Mark
English
Scott
Josetta
History
John
Mark
I would like to add a 'rank' field to a query. I have a simple query that orders the results based on a field called 'score'. I want to add another field than than auto-fills with 1 for the first record, then 2, for the second, etc... so it shows a rank position. I have tried creating a field using the following - rank: [rank]+1 but no luck. Any ideas?
There really isn't a good way to do this in MS-Access. Here's a link that may shed some light on this issue:
http://databases.aspfaq.com/database/how-do-i-return-row-numbers-with-my-query.html
I created a combo box for a persons last name and first name. The combo box works fine you can see a complete list of all the people, however a problem arises when I select someone who shares a surname with someone else.
For example:
Surname | First Name
Romas Issac
Romas Tony
If I select Romas Tony in the combo box I get the value for Romas Issac for the first name. This is a real problem as I'm trying to launch a query based on what is selected - how can I ensure that the correct first name is selected?
Thanks
Add the primary key from the table where that data originates to your combobox as a third, hidden column (to make the column hidden, you can just set the column width to 0; actually, I usually add this as the FIRST column, because then you can leave the entries in the 'Column Widths' property empty for all the other columns). Set its 'Bound Column' property to the number of the primary key column. It is generally good practice to do this with ALL comboboxes that get their data from tables.
If you haved stored the names in two seperate collumns, you have to make sure you are getting both .collumn(0) & .collumn(1), but wakjah's answer is always a good idea ;)