Tables
Company- CompanyID, CountryID , Company Name,
Country-CountryID, Country Name
I am creating a form to edit the company information. I am having problems updating Company's CountryID. I want a dropdown to displays every country which will modify the company's foreign key.
I followed the tutorial here. http://www.techonthenet.com/access/comboboxes/bind_index.php. It works with inner but it doesn't display all the countries. But when I switched SQL inner to left join, the form fails.
My sql-
SELECT DISTINCT table_country.Country, table_company.Country_id
FROM table_country LEFT JOIN table_company ON table_country.Country_id = table_company.Country_id
ORDER BY table_country.Country;
![enter image description here][2]
The the RowSource property of the combobox should only contain a lookup list. It should not be retrieving records from the company table.
The RowSource property should be:
SELECT Country_ID, Country FROM table_country ORDER BY Country
Set Column Count to 2
Set Bound Column to 1
Set Column Width to 0 as you don't want to display the country_id only country.
Related
I have two tables:
'tableStudent' - a list of students given an ID number by the table, with following columns:
student_ID
last_name
first_name
and 'tableProject' which gives each project an ID and will be used to store information about the students involved in the project. Students will work in pairs on the project. This table includes the following columns:
project_ID
project_title
student1_ID
student2_ID
The columns student1_ID and student2_ID are combo-boxes that link student_ID to the student names.
I want to create a form that can be used to record students involved in a project. I want to be able to select the student IDs and have the student names autofill on a form.
I can make a form that autofills, but only with one student with this SQL:
SELECT tableProject.project_ID, tableProject.project_title, tableProject.student1_ID, tableStudent.last_name, tableStudent.first_name
FROM tableStudent INNER JOIN tableProject ON (tableStudent.student_ID = tableProject.student2_ID) AND (tableStudent.student_ID = tableProject.student1_ID);
When I add Student 2, I get a duplicate error on the student names. How do I indicate that the two names belong to different students? This is the code I'm generating:
SELECT tableProject.project_ID, tableProject.project_title, tableProject.student1_ID, tableStudent.last_name, tableStudent.first_name, tableProject.student2_ID, tableStudent.last_name, tableStudent.first_name
FROM tableStudent INNER JOIN tableProject ON (tableStudent.student_ID = tableProject.student2_ID) AND (tableStudent.student_ID = tableProject.student1_ID);
Any help appreciated!
Options for displaying related info from lookup table:
Use Access query designer to build form RecordSource. Pull tableStudent into query designer. In your case pull in twice - second instance will get named like tableStudent_1. Join each to one of the student fields. Bind textboxes to both sets of lookup table fields and set them as Locked Yes to prevent edit.
Don't include lookup table in form RecordSource at all. Include all student info in combobox RowSource. Expressions in textboxes refer to combobox columns by index, index begins with 0: =[comboboxName].Column(1)
DLookup() domain aggregate function, however, since your tables have a relationship, this is an inefficient method.
For all options, set textboxes with TabStop No.
I have one Table of users in which their is LOGIN_ID, LOGIN_PASSWORD, and USER_ROLE.
I want to get Name of USERS From Different Table with single query.
e.g.
Login_ID: 1001 is from students so we have to get the name form students.
Login_ID: 235738932 is from Employees so we have to get name from Employees.
There is no Foreign Key Relation in users Tables because Login_id is from two different Tables.
Try:
SELECT
LOGIN_ID,
LOGIN_PASSWORD,
USER_ROLE,
CONCAT(COALESCE(students.name,''),COALESCE(employee.name,'')) as Name
FROM users
LEFT JOIN students ON students.Login_ID = users.Login_ID
LEFT JOIN employee ON employee.Login_ID = users.Login_ID
COALESCE(students.name,'') Will return the students name. If the name is not found, the values will be NULL, and COALESCE will change this to ''.
The same happend to the name of the employee. Both names are getting concatenated.
I have a form showing Customers. Now I would like to also display all Orders that the currently open customer has made.
The Order table has a foreign key to Customer.
I have tried using a list box with Multi Select set to Simple, but somehow it shows me all Orders instead of just the ones of the current customer.
More Details:
My list box has
Control Source: ID
Row Source: SELECT customer.id, order.info FROM customer
INNER JOIN order ON customer.ID = order.customer_id
If I set the Multi Select to None it always marks the first Order that matches the current customer, but not all matching orders.
Can someone point me in the right direction?
Thank you.
P.S. I don't necessarily want the list to be functional for the creation of a new Customer. If it would also work then that's a bonus.
Assuming your form is bound to Customers table, then you need to add code to the form's Current event:
lstOrders.RowSource = "SELECT id, info FROM order WHERE customer_id = " & Me.id
lstOrders.Requery
The list box (called lstOrders) should have its column count set to 2. If you don't want to see the order.id column then set the "Column widths" property to 0 (this will set the width of the first column to 0 and let the second column, order.info, fill the remaining width of the list). Set the "Bound column" to 1 - this means that the "value" of the list box will be the order.id
I am hoping someone will be able to point me in the right direction.
I have a COUNTRY table, containing a primary ID, and country name (e.g. France)
I have a second table PAYMETHODS_COUNTRIES, which contains a primary id, payment method id (e.g. 4) and country id
I am trying to output a list of checkboxes on each payment method, to control which countries (or users in which countries) have access to those local payment methods.
I have the checkbox list working okay, and the processing of the form is also working. The last remaining thing, is to output the list of country checkboxes with selected countries (e.g. this payment method is available in France and Germany) shown as CHECKED.
So what I am trying to do is to output a complete list of all countries
SELECT id, name FROM country ORDER BY id ASC
And then include the PAYMETHODS_COUNTRIES table something like this (not working of course)
SELECT country.id AS countryid,
country.name AS countryname,
paymethods_countries.id AS methodid
FROM country LEFT JOIN `paymethods_countries`
ON country.id = paymethods_countries.country
WHERE paymethods_countries.paymethod = 10
This example query only shows rows that actually have a record in PAYMETHODS_COUNTRIES, not what I am after.
What I was hoping for, is a complete list of country.id + country.name, with a final field (the ID of the PAYMETHODS_COUNTRIES table) - which should be null where there is no link. That way I should be able to check for null or otherwise, and output CHECKED for those rows.
Hopefully that makes sense - or if anyone knows a better way to achieve what I am doing, your thoughts are appreciated.
While the other answer here will produce the desired result, semantically it is more correct to move the condition from the WHERE clause to the ON clause:
SELECT country.id AS countryid,
country.name AS countryname,
paymethods_countries.id AS methodid
FROM country
LEFT JOIN `paymethods_countries`
ON country.id = paymethods_countries.country and paymethods_countries.paymethod = 10
This eliminates the need to add the extra OR condition in the WHERE clause.
It's only coming back with rows that have record in PAYMETHODS_COUNTRIES because of your WHERE clause since you say the paymethod must = 10. Add an OR to let it be null too:
SELECT country.id AS countryid,
country.name AS countryname,
paymethods_countries.id AS methodid
FROM country
LEFT JOIN `paymethods_countries`
ON country.id = paymethods_countries.country
WHERE paymethods_countries.paymethod = 10
OR paymethods_countries.paymethod IS NULL
I have 4 tables namely,
countries, states, cities, areas,
apart from countries table the rest three(states,cities,areas) contains country_id foreign key.
i wanted to return the total number of count of country_id combined in three tables for which i used jon_darstar's solution, here is the code i am using.
SELECT COUNT(DISTINCT(states.id)) + COUNT(DISTINCT(cities.id)) + COUNT(DISTINCT(areas.id))
FROM states
JOIN cities on cities.country_id = states.country_id
JOIN areas on areas.country_id = states.country_id
WHERE states.country_id IN (118);
the above code works perfectly fine although i am unable to understand the code properly, mainly the first line i.e
SELECT COUNT(DISTINCT(states.id)) + COUNT(DISTINCT(cities.id)) + COUNT(DISTINCT(areas.id))
Question 1 : doesn't that select the
primary id of the three tables
states,cities and areas and make the
count? i know this is not happening
from the result i am getting then what
is actually happening here?
However if i remove the DISTINCT from the query string it shows me a different result i.e a count of 120 whereas actually it should be 15(this is the count number of country_id in all three tables).
Question 2 : What is happening if i
use DISTINCT and remove DISTINCT?
isn't DISTINCT supposed to remove any
duplicate values. where is duplication
happening here?
thank you..
For an example, if in a country A(having primary id a.id=118),there is State B(having primary id b.id),inside that state there is City C(having primary id c.id), In city C there's Area D(having primary id d.id),E(having primary id e.id),F(f.id).lets visualize the query result in a database table.
C St Ct Ar
A->B->C->D
A->B->C->E
A->B->C->F
(Here C=Country,St=States,Ct=Cities,Ar=Areas)
Now just think what happens when you do count on above table to get total number of States within Country A without distinct.The result is 3,this way the Number of Cities is 3 and areas is 3,total 9.Because without distinct you're getting duplicate values in which you're not interested.
Now,if you use distinct count you'll get correct result cause here distinct states under
country A is 1,City is 1 and Areas is 3,total:5(excluding duplicate values)..
Hope this works!
!!Design Issue!!!
Like to add something:From your database design,i can see that you're using country id as a reference for countries from country table(to states,areas and cities) then joining states and cities then states and areas (by their country id)don't you think it's creating cross join?.Better design choice is at areas table keep foreign key of city,this way go bottom up like in city keep states and in states keep country.Or make a table for Areas where you are keeping Countries,States,Cities foreign key and areas primary key.