Access tables multiple join issue - database

Can I use two joins between two tables in Access database?
I have a customer database, and my customer names appear in two different fields and I want to convert customer names into the short names and return that in a query in one single field.
In attempt to solve that I have created a second table with all the customer names and their abbreviations then linked "CustName" field with the "Customer_Name" field in my main table, in my query I am returning the short names of my customers. The struggle is that some customer names e.g Toyota appear in "customer_Plant" field instead of "customer_Name" field (please see picture). I want to use different Toyota shortnames by each plant location. Another difficulty is that the "Customer_Plant" field in my original table is not always populated, except for Toyota.
Is there any way I can use multiple relations between two different tables? so that access query can return short names, not just by "customer_Name" but also by "Customer_Plant" at the same time.
Access does not allow me to join "Customer_Plant" with "custPlant" if one join is present between the tables. Is there any other way I can achieve this?
Tbl_claimdata & tbl_custShortName:
Join between the tables:
Current Output:

If Plant name is not provided in either or both tables, consider:
Query1: Claims_ADJ
SELECT tblCustClaimData.Customer_Name,
tblCustClaimData.Customer_Plant, Nz([Customer_Plant],[Customer_Name])
AS LinkNameClaim FROM tblCustClaimData;
Query2: Short_ADJ
SELECT tblShortCustName.CustName, tblShortCustName.PlantName,
Nz([PlantName],[CustName]) AS LinkNameShort,
tblShortCustName.ShortName FROM tblShortCustName;
Query3:
SELECT Customer_Name, Customer_Plant, ShortName FROM Short_ADJ RIGHT
JOIN Claims_ADJ ON Short_ADJ.LinkNameShort = Claims_ADJ.LinkNameClaim;
Query3 is not updatable so probably useful only for a report.
So alternative is DLookup in query (queries 2 and 3 not needed) or textbox:
DLookUp("ShortName","tblShortCustName","Nz([PlantName],[CustName])='"
& Nz([Customer_Plant],[Customer_Name]) & "'")

Related

Microsoft Access || Join Query to return only one record

Im trying to join two tables together using Microsoft Access design view (still a beginner) where in the first table I have only unique date but in the second I have multiple records with the same reference.
So, when I try to join the two tables together I get all the records in table 2 (duplicates) however, I only need only to return one record (highest value for example)

Dynamic Parameter showing too many values

I am currently creating a simple report in Crystal Reports with two tables:
{Table1.group_name_id} --> {Table2.technical_group_id}
Table 1 holds all of the groups; ID's, Names etc
Table 2 holds only the technical groups ID
With these two tables linked it means the only records that will return are those where technical groups are involved, perfect! But now I want to add a dynamic parameter to return the Technical Group Names for the End User to select.
Because {Table2} only holds one field (the ID) which links to {Table1}'s ID, I have to perform the Parameter selection on {Table1}'s name field.
But this is pulling back all of {Table1}'s names and is discounting the Join on {Table2} even with an enforced join present.
Is there a way to force it to only pull back {Table1}'s names as long as it matches the JOIN between {Table1} and {Table2}?
Thanks in advance!
Edit (additional information)
As I thought - the problem was Crystal Reports was not recognising the JOIN when displaying the parameter values.
After some reading, I found out that the JOINS are only recognised when the query is passed to the database (logically). So when selecting a Parameter, it doesn't recognise the JOINS.
I got around this by creating a custom SQL command, forcing it to only pull back the groups in the second table.
First, please confirm your join INNER JOIN.
For Addition,
You can pass the User Input as a parameter in to the crystal report and use this {#Parameter} to filter the result set using SelectionFormula in Crystal Report.
Or you can even set the selectionformula itself from the application.

Exporting data from two tables and combining into one file

Have a question. I have a database called scraped. It has two tables. One called profiles and the other called charges. There is an identifier for each person in the profiles table called ID and a corresponding identifier in the charges table called profile_id. I'd like to export the profiles table, but want to join up all the contents of the charges to make one big flatfile (or table) using the "use query" as an export method. I'm stumped as to how to do this.
One other issue. say john smith has ID of 101, he may have 10 rows In the charges table that correspond to his ID number. Will they all be listed in separate rows in final output or not? If not can they be somehow?
It sounds like you just need to write a simple join query.
SELECT [list of columns you need]
FROM Profiles
INNER JOIN Charges ON Profiles.ID = Charges.profile_id
If you need all profiles whether or not they have a charge then change INNER JOIN to LEFT JOIN.
And yes, you'll get a record for every match in the Charges table (so 10 for your john smith).

Is it possible to query two access tables where you want to know if a value/range in the first table is between two fields in the second table?

I'm trying to query two tables, ASSAYS, AND LITHO in a diamond drillhole database.
I was given values (SAMPLE_NO) to search for in the ASSAYS table, to return values such as HOLE-ID, FROM, and TO. So each sample that we take has a HOLE-ID, SAMPLE_NO, FROM AND TO. One hole-id can have multiple sample numbers, but each sample number is unique. The from and to will be unique in each hole-id. This I can find no problem.
My coworker also wanted to know what rock type was associated with each sample. This info is located in another table so I'll need to figure out how to query for this. The information that this table holds is HOLE-ID, FROM, TO, and ROCKTYPE.
You're looking for what is called a JOIN. This allows you to JOIN data of multiple tables based on matiching column values.
This could be your starting point:
SELECT a.*, l.*
FROM ASSAYS a LEFT JOIN LITHO l ON a.hole-id = l.hole-id
WHERE a.sample_no = 'XXXX'
Please google for JOIN and SQL to find out about the exact syntax.

Possible to search multiple tables with a single query? [MSAccess/SQL Server]

So my goal here is to have a single search field in an application that will be able to search multiple tables and return results.
For example, two of these tables are "performers" and "venues" and there are the following performers: "John Andrews","Andrew Smith","John Doe" and the following venues: "St. Andrew's Church","City Hall". Is there a way to somehow return the first two performers and the first venue for a search of "Andrew"?
My first thought was to somehow get all the tables aggregated into a single table with three columns; "SearchableText","ResultType","ResultID". The first column would contain whatever I want searched (e.g. Performer name), the second would say what is being shown (e.g. Performer) and the third would say the item's ID (note: all my tables have auto-incrementing primary keys for ease). The question for this idea is it possible to somehow do this dynamically or do I have to add code to have a table that automatically fills whenever a new row is updated/added/deleted from the performers and venues table (perhaps via trigger?).
My application is written in MSAccess (I know, I know, but I have no choice) on top of a SQL Server backend. I'd prefer this happen through MSAccess so I don't have to have a "searchme" table sitting on my SQL Server but any good result is acceptable :)
I think you are looking for the "union" sql keyword
I'd use full text indexing in SQL server, have a single table with your searchable text, and forign keys in your main tables that link to the search table. This way you can order your results by relevance.
I think you have a schema problem. Querying a UNION is almost always evidence of that (though not in all cases).
The question to me is:
What are you returning as your result?
If you find a person, are you displaying a list of people?
Or if you find a venue, a list of venues?
Or a mix of both?
I would say that if you want to return a list of both, then you'd want something like this:
SELECT tblPerson.PersonID, tblPerson.LastName & ", " & tblPerson.FirstName, "Person"
FROM tblPerson
WHERE tblPerson.LastName LIKE "Andrew*"
OR tblPerson.FirstName LIKE "Andrew*"
UNION
SELECT tblVenue.Venue, tblVenue.Venue, "Venue"
FROM tblVenue
WHERE tblVenue.Venue LIKE "Andrew*"
ORDER BY Venue
This will give a list of the matches indicating which is a person and which a venue, and allow you to then select one of those and open a detail view (by checking the value in the third field).
What you definitely don't want to do is this:
SELECT tblPerson.PersonID, tblPerson.LastName & ", " & tblPerson.FirstName, "Person"
FROM tblPerson
UNION
SELECT tblVenue.Venue, tblVenue.Venue, "Venue"
FROM tblVenue
then saving that and trying to query it on the 2nd column. That will be extremely inefficient. You want your WHERE clause to be on fields that can be searched via the index, and that means each subquery of your UNION needs to have an appropriate WHERE clause.

Resources