MS Access Identical Comboboxes for Autofill - ms-access

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.

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!

Dynamic or Static column?

I have some tables in databases. The table contains more than 150 columns for some custom field operations which may/may not be used by all. Instead of creating static 150 unused column fields, it can be created dynamically using add column.
Can somebody justify, which one is better? When to use dynamic, when static and why?
You can look for below normalization for maintaining custom fields, I have used in multiple web/window application successfully.
FormMasterTable
FormId, Name: this will be identifier and will be passed from Form to DB query to identify itself
datatypeMaster Table
datatypeId, datatypeName
The abobe will define all custom fields you are going to support i.e. customer can create to customize a form.
Table for Form-Customer-CustomFieldMapping say Custom Field Master Table for managing column definition and mapping with customer.
customerId, FormName/FormId, FieldName, customFieldId, datatypeId(FK:datatypeMaster), length ....
Next step is to create all tables to store all datatypes defined in datatypeMaster table i.e. one table for each datatype, some of the examples I can think of....
Custom field detail table for numeric say CustomNumberFields
customFieldId FieldValue(numeric(max))
Custom field detail table for string say CustomTextFields
customFieldId FieldValue(varchar(max))
Custom field detail table for Dates say CustomDateFields
customFieldId FieldValue(datetime)
and so on....
Now you can use all in one query with Inner Join to get all custom columns of a customer.
Sample Query to get customfields fields
select MP.FieldName, CFN.FieldValue from Form-Customer-CustomFieldMapping MP
JOIN CustomNumberFields CFN on MP.CustomFieldId and CFN.CustomFieldId where formId=<<paramformId>> and customerId=<<paramcustomerId>>
UNION
select MP.FieldName, CFN.FieldValue from Form-Customer-CustomFieldMapping MP
JOIN CustomTextFields CFN on MP.CustomFieldId and CFN.CustomFieldId where formId=<<paramformId>> and customerId=<<paramcustomerId>>
UNION
select MP.FieldName, CFN.FieldValue from Form-Customer-CustomFieldMapping MP
JOIN CustomDateFields CFN on MP.CustomFieldId and CFN.CustomFieldId where formId=<<paramformId>> and customerId=<<paramcustomerId>>

SQL have one column in two tables

I am trying to create a table that shows treatment information about patients (though I just wondered if would be better as a query) at a fictional hospital. The idea is that one row of this could be used to print an information sheet for the attending nurse(s).
I would like to make the attending_doctor column contain the name that corresponds with the employee_id.
|Patient_ID|Employee_ID|Attending_Doctor|Condition|Treatment|future_surgery|
Would appreciate any help. Thank you!
Just use a join in your query rather than have the employee name in 2 tables (which would mean updating in more than one location if they change name etc). For the sake of an example, this also gets the patients name from a 3rd table named patients.
eg
SELECT table1.*, employees.name, patients.name
FROM table1
LEFT JOIN employees ON employees.id = table1.employeeId
LEFT JOIN patients ON patients.id = table1.patientsId
Don't use directly this table, but build a view that contains the data you need. Then you can get the data from the view like it was a table.
Basically what you need is to have data in three tables. One table for patients, one table for for employees and one for the reports. Table with reports should contain only the employee_ID. Then you can either build a direct query over these three tables or build a view that will hide the complicated query.

MYSQL: should i use 2 tables for 2 types of user with common and different field?

i have 2 type of users
1. employee.
2. employees friend.
both users have common fields.
1.first name
2.middle name
3.surname
4.cellphone number
5.telephone number
6.city address
7.email (serve as login)
8.password
9.registration date
10. update account date
and the employee friend user don't have this field
1.employee id
2.company name.
3.company branch
4.position
and they will be going to use same login form.
edited version (thanks to nawfal and Patrick James McDougle)
1st table (user) should contain field 1-10(first list) and a new field fk_employees (foreign key to our 2nd table, null if it is an employees_friend)
2nd table (employees) should contain field 1-4(second list)
I think about this the same way I think about object oriented programming. If employee EXTENDS user then employee should be a user with extra information. I would have two tables.
Users (containing the common fields adding an id field perhaps)
&
Employees (with the 4 extra fields for employees and one field that references a unique identifier in the users table.)
More information about what I am proposing can be found here: http://www.agiledata.org/essays/mappingObjects.html#MappingInheritance
what i suggest create 2 separate tables as users and user_friends with the fields mentioned above.
Now create a view with following query:
select employeeID,email,password from users
union
select friendID,email,password from user_friends
Query this view to get the login info in your application.

Access 2007 - Building unmatched data query, but need two field categories

Is it possible to build an unmatched data query using two tables with a tracking a unique value field and also another value in the row as well (that value will not be unique).
For example I want to track a unique customer code from an invoice on a new table, compared to last month's invoice. The non unique value would be a "product code" of what they purchased.
A customer code could appear multiple times depending on if they have purchased multiple product codes.
Any help is much appreciated.
This should do what you want:
SELECT Invoice.CustomerID
FROM Invoice LEFT JOIN PreviousInvoices
ON (Invoice.Product = PreviousInvoices.Product)
AND (Invoice.CustomerID = PreviousInvoices.CustomerID)
WHERE PreviousInvoices.Product Is Null