Concatenate lookup fields in a calculated filed in Microsoft Access - ms-access

I have a table in a Microsoft Access 2013 database and I want to create a new calculated field that concatenates three lookup fields from this table.
I inserted a new field, defined it as "Calculated" and, using the expression builder, put in the following code:
[Field1] & " " & [Field2] & " " & [Field3]
What I got were numbers, like these:
23423 23 47
How can I get the values instead of the numbers?

See Create or delete a lookup field to create a lookup field in a table. I don't know if you can lookup values in a calculated field in a table. You could try to use the DLookUp function, but event if it works, the performance will be bad. You are better off with creating a query for this purpose.
Something like this
SELECT d.Description, w1.Description & ' ' & w2.Description & ' ' & w3.Description AS sentence
FROM
((dictionary d
LEFT JOIN words w1 ON d.Id = w1.Id)
LEFT JOIN words w2 ON d.Id = w2.Id)
LEFT JOIN words w3 ON d.Id = w3.Id
But it is very difficult to give you a more detailed answer with the very few pieces of information you gave us.

In short, the lookup hides the number that is actually stored in the table. to fix the query, add the look ups manually to your query.
the field with a look up is usually a foreign key to another table. the idea behind a lookup is to show a more intuitive description just like you would with a combobox but the number is still there in the table. the number is just visually wrapped in a combobox:
Hence the query:
gives the result:
To fix this just include the lookup in the query:
which gives the desired result:

Related

How to do a lookup or index/match in Access?

I'm new to access and I'm struggling on how to use lookups since vlookup is not available in Access. I want to get the values that correspond to a certain ID/tagging.
I have 2 tables.
Table A contains values let's say Product Number, Product Type, Price, Remaining Stock #, Product Type+Product Number Tag. Let's say product number is not unique but combining it with its product type, it will be unique so I created that tag.
Table B contains Seller's Name, # of items Sold, Product Number, Product Type, Product Type+Product Number Tag.
Now using Table A and Table B, how can I create a query/table that will allow me to use that "Product Number + Product Type" Tag when I try to get the price of that certain item so that I can get the total revenue of each seller.
I hope you understand what I'm trying to say. What I just want to do is I want to use this "Type+Number" Tag as a reference point in getting data of that respective item when I try to create queries/tables. It's just like an INDEX/MATCH in Excel. But how to do it in Access?
Please tell me if it's unclear.
Thank you!
You need to join your two tables based on the relationship between the Product Number & Product Type fields in both tables.
The two fields in Table A should be marked as the composite Primary Key (select both fields and in the Design ribbon click the Primary Key icon).
In Table B they will be Foreign Keys - a seller could sell those products more than once, so duplicates are allowed here.
You don't need the Product Type+Product Number Tag field.
The SQL for your query would then be:
SELECT *
FROM [Table A] LEFT JOIN [Table B] ON [Table A].[Product Number] = [Table B].[Product Number] AND
[Table A].[Product Type] = [Table B].[Product Type]
This will return all records from Table A and only those records from Table B that match the Primary Key.
Finally.... don't think of an Access table as an Excel spreadsheet. Access is all about the relationship between pieces of data - for a start queries can be expressed in plain English a lot easier.
E.g return all records from table B where seller name is "Dave" and date is between 1st Jan and 31st Jan would be written as:
SELECT *
FROM [Table B]
WHERE [Seller Name]='Dave' AND
[Sale Date] Between #01/01/2018# AND #01/31/2018#
(SQL only deals in US date format).
You can use DlookUp Function.
It can be used on queries as a calculated field, or in code in VBA:
For example, to get the price of a PRoduct, you could use something like:
DlookUp("[Price]";"Table A";"[Product Type+Product Number Tag]='" & Value & "'")
From my point of view, the complex part of DlookUp is the third argument, the WHERE clausule. If you have any experience with SQL, you will have no problem. If you don't, don't worry, just read some info and if you get stuck, come here to SO
You can use DlookUp to get any value of any field, based on a criteria (criteria applied to a unique field, ofc).
And yes, you can use it to get values from tables or from queries. In the link I provided before, it explains how the arguments works.
The most complex part is the criteria part. You must write as if you were typing a WHERE clausule on SQL more info here
About the criteria, always remember this:
If your criteria is a numeric value, then just type [field_criteria]=my_numeric_criteria
If your criteria is a text value, you must use single quotes. For example, [field_criteria]='my_text_criteria'
SQL requires single quotes around text values.
Try it!

Query Code Joining Issues

Final Query Table - What the final table should look like.
Pubs Tables - I did not include the author_id which needs to be changed to SSN.
I am very new to this - like three weeks into this class. I'm struggling to wrap my brain around the concepts but it looks like there is a wealth of information on stack overflow I need to keep reading....it would help if I were actually using a database on a regular basis in a real life situation right now. Here is my query I keep coming up with an error on - possible I am making beginner mistakes I'm not seeing or understanding. I want to understand and get this down before it gets harder.
USE pubs
SELECT DISTINCT authors_id as SSN
FROM authors
SELECT au_lname, au_fname as fullname
FROM authors
INNER JOIN author
WHERE royalsched = <50
I'm getting an error message - incorrect syntax near the word where, msg 156, level 15 state 1 line 7
The question is: Select the unique author id as ssn, the lastname and firstname separated by a comma (smith, joe) as Fullname from the authors table joined on the titleauthor table where the royaltyper is less than 50. Order the query by the fullname. I get that I need to append the values of the fields together (concatenate) by using the + symbol and I know that I need to join them - just not sure if I'm headed in the right direction or not. Is there a place you can check your queries like validator online (to show where you've missed stuff?)
Update 10/6/16:
Tried it again. This is my reasoning: join the tables (INNERJOIN?) bring together the first name and last name (field 1 + field 2) althoughthis is where you were saying I need to do something different. This is what I came up with but it is not working: (authors_id is what it is called in the database so it needs to be changed to SSN, au_lname and au_fname need to be combined into one column as FullName) then it needs to show the royaltyper is less than 50.
SELECT authors_id = SSN FROM authors AS INNER JOIN ON au_lname + au_fname WHERE royaltyper = <50
Your join is missing an ON clause to tell how authors and (title?)author are related. For instance, ON authors.id=titleauthor.author_id.
That's what it is expecting where it sees WHERE. (mysql also supports a USING () syntax instead of ON. But one or the other must be specified for non-CROSS joins.)
To concatenate in mysql, you use CONCAT, not +. E.g.:
concat(au_lname,', ',au_fname) as Fullname

Querying Two Tables & Using Record Data as Column Headers in Access?

This site has been super helpful with this database i am creating for work, i have a lot of VB knowledge but little SQL.
i am creating a database of cisco IP phones which records the MACs, serials and to what site id they are assigned to.
My managers have now decided to change the goal posts of my application once again and are now asking for me to create a form that displays data in a way i have no idea how to do.
what they now want and what i am hoping to achieve somehow is to create a sql statement that gets the count of the number handsets by site id and then to compare that number against the data from another table.
Here is the fields that we need to work with:
TABLE 1
Handset_Type
Handset_Site_Id
TABLE 2
Handset_Type
Handset_Site_Id
This is how i would like to display the data
SITE ID | Handset_Type1 | Handset_Type1
Handset_Site_id1 |COUNT TYPE1 FROM T1|COUNT TYPE1 FROM T2
Handset_Site_id2 |COUNT TYPE1 FROM T1|COUNT TYPE1 FROM T2
If we can use the data in the Handset_type field to create the column headings then that would be great as there are only a few models of phones but if they are static columns then that will be ok also.
I hope i have explained this ok as i can see this may be complex to understand.
if you have any suggestions please let me know
Thank you
Steve
If I understand the question correctly, you would like to present the count of records from both tables, with the handset type and source table as column headings, for each site ID.
The first step is to merge your tables into a single table, creating a column which expresses the handset type and source table:
SELECT Handset_Type & " in T1" as TypeFromT, Handset_Site_Id FROM [Table 1]
UNION ALL
SELECT Handset_Type & " in T2" as TypeFromT, Handset_Site_Id FROM [Table 2]
This will form an inner query for the crosstab. The syntax for crosstabs can be a bit tricky so I usually build mine in design view. We define the TypeFromT as our new column heading and we use Handset_Site_Id both as our row groupings and as our counting field:
TRANSFORM Count(SubQry.Handset_Site_Id) AS SiteIDCount
SELECT SubQry.Handset_Site_Id
FROM (
SELECT Handset_Type & " in T1" as TypeFromT, Handset_Site_Id FROM [Table 1]
UNION ALL
SELECT Handset_Type & " in T2" as TypeFromT, Handset_Site_Id FROM [Table 2]
) AS SubQry
GROUP BY SubQry.Handset_Site_Id
PIVOT SubQry.TypeFromT;
The only catch is that if there are zero entries for the particular Handset Type, Source Table, and Site ID, the resulting value will be null instead of a 0. If that is undesirable, replace Count(SubQry.Handset_Site_Id) with Nz(Count(SubQry.Handset_Site_Id),0)

Access 2010 DLookUp

Working with MS Access for the first time and coming across a few problems if someone could please point me in the right direction.
So I'm doing a mock database (so it looks silly) just to learn the ins and outs and need some help with DLookUp at the moment.
My database has two tables, with the following fields:
C_ID the PK in Courses and FK in Student
tblCourse: C_ID, Title, Subject
tblStudent: S_ID, C_ID, Name, EnrollDATE
As I said this is just for testing/learning. So what I want is to have a filter that gives me a list of C_ID's based on which EnrollDates are NULL.
so filter is:
Expr1: DLookUp("[tblStudent]![C_ID]","tblStudent","isNull([tblStudent]![EnrollDATE])")
I have also tried with the criteria being
[tblStudent]![EnrollDATE] = Null
Currently I get just blank fields returned. Any help is greatly appreciated, and please ask me to elaborate if my explanation is off.
Thank You!
The correct syntax looks like this:
DLookup("C_ID", "tblStudent", "EnrollDate is null")
You don't need to include the table name when you specify the columns
In Access, you check for Null by using xxx is null or xxx is not null
Note that DLookup only returns one value (if the criteria matches more than one row, the value is taken from any row), so you can't use it to get a list of C_IDs back.
EDIT:
What you actually want to do is select data from one table, and filter that based on data from the other table, correct?
Like, selecting all courses where at least one student has an empty EnrollDATE?
If yes, you don't need the DLookup at all, there are two different ways how to do that:
1) With a sub-select:
select *
from tblCourse
where C_ID in
(
select C_ID
from tblStudents
where EnrollDATE is null
)
2) By joining the tables:
select tblCourse.*
from tblCourse
inner join tblStudent on tblCourse.C_ID = tblStudent.C_ID
where tblStudent.EnrollDATE is null
This is SQL, so you need to switch to SQL View in your query in Access.

MS-Access Junction Table Inserts/Deletions Between Two Mutually Exclusive Lists (2 Listboxes)

With this kind of design, I would like to create a functionality with which to add and delete records from the junction table.
The case is from when editing an employee and selecting what exams they can take. In the left list box would be the exams that they aren't eligible for (yet, anyway) and the exams that they are eligible for on the right list box.
The table is something like this:
TABLE EmpExam
(
EmpID,
ExamID
)
The EmpID would be known at all times, but the ExamID would be selected from the left list box. Records in the right list box would probably have to have both the EmpID and the ExamID in order to be deleted.
Instant deletions/insertions aren't necessary once they're into their respective boxes are not necessary (they can wait until the form is closed).
Is there a good/standard way to accomplish this in Access?
Why use listboxes when you will have to add items to the table using code and then delete them using code?
For what you want to do, a subform is the usual solution. Furthermore, you can use the recordsetclone of your subform. Note that you should probably have a datetime field in that setup. Also, if ExamID is unique, and cascade delete is enabled, deleting from the main table will delete from the subtable.
I ended up using two listboxes with 1 add button and 1 remove button that triggers their VBA On Click methods that execute raw SQL.
The method looks something like this:
If IsNull(cboInEligible.Column(1))
Exit Sub
End If
CurrentDB.Execute ("INSERT INTO tblEmpExam (ExamID, EmpID) " & _
"VALUES (" & ExamID & ", " & lstInEligible.Column(1) & ")")
lstInEligible.Requery
lstEligible.Requery
The delete query is similarly done.
The two listboxes are mutually exclusive.
SELECT EmpID, EmpName
FROM Employee
WHERE EmpID NOT IN (SELECT EmpID FROM tblEmpExam WHERE ExamID = [txtExamID]);
txtExamID is a hidden (but obvious to the designer) control on the form since I can't refer to the form's ExamID except through a control or through absolute naming.
The other list box has EmpID IN instead of EmpID NOT IN to make it exclusive.