Access / validationrule - calculation - ms-access

I need to set up a small database as an assignment after 3 hours of an access introduction and I'm failing completely. However I youtubed several hours but still no change in the outcome. Do I try to implment too many excel functions into access?
I saw in one resolved question that it if calculation is possible in access?
I have three tables for a dvd rental business. Customers, Movies, Rentals.
My questions / problems:
I wanted to do an age check in regards to the customer's age and the age restriction of the movie. I have customer's date of birth and the age restriction for a movie. Provided fields DoB 15/03/1996, age restriction 18+. For example user is born 15/03/1996 and wants to rent a movie 18+. Access should show me the customer's current age like: Field name: Age; "17". This didn't work out at all. I failed in the first step to set up the field/calculation.
In excel it's usually =INT((TODAY()-DoB)/365.25) which tells you the age up to date.
How do I need to set it up?
Many thanks in advance
Nina

You need to convert the Excel formula to use VBA functions. Since Int exists in VBA, and the Excel UI, VBA and and Access database engine (DAO/ADE) all use the OLE date/time format, the conversion is pretty trivial - just use the Date function in place of TODAY.
So, say the tables are set up like this:
Customers - CustomerID (AutoNumber, primary key), Surname (Text, required), Forename (Text, required), DoB (Date/Time, required)
Movies - MovieID (AutoNumber, primary key), MovieTitle (Text, required), MinAge (Number/Byte)
Rentals - RentalID (AutoNumber, primary key), CustomerID (Number/Long Integer, required, foreign key to Customers.CustomerID), MovieID (Number/LongInteger, required, foreign key to Movies.MovieID)
You could then create a query called RentalsWithAge as so:
SELECT Rentals.*, Movies.MinAge, Int((Date() - Customers.DoB) / 365.25) AS CustomerAge
FROM (Rentals INNER JOIN Customers ON Rentals.CustomerID = Customers.CustomerID)
INNER JOIN Movies ON Rentals.MovieID = Movies.MovieID;
(Do so by going to create a new query in the UI, before cancelling the table prompt and heading straight to the SQL view.) Next, create a form to log a new rental, and base it on RentalsWithAge (not Rentals); ensure all the fields discussed have been added to the form, then handle the form's BeforeUpdate event as so:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.CustomerAge < Nz(Me.MinAge, 0) Then
Cancel = True
MsgBox "Customer is under the minumum age for the chosen movie!", _
vbCritical, "Customer Under Age"
End If
End Sub

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!

Access 2013 Add multiple records

I am creating a Training Records database.
Tables: EmployeeList, RolesPerformed, SkillsNeeded, Courses, EmployeeRoles (Junction of EmployeeList and RolesPerformed), RoleSkills (Junction of Roles and Skills), SkillCourses (Junction of Skills and Courses). I can query to find out what courses an employee needs based on their roles. But I need to make another table to track the courses per employee and their date completed. When creating an Employee the user sets their Roles. I would like to auto populate the EmployeeCourses table with the needed courses.
I tried to use an After Insert event on EmployeeRoles, but it claims a createRecord cannot be used in a foreachrecord.
Do you have a table that defines what courses go with what roles?
The real trick is figuring out what event to put code into. Perhaps AfterUpdate for a combobox to select Role. Will probably also need code that first commits the new employee/role record to table.
VBA could be like:
If Me.Dirty Then Me.Dirty = False
CurrentDb.Execute "INSERT INTO EmployeeCourses(EmpID, CourseID) SELECT " & Me.tbxEmpID & " AS EID, CourseID FROM RolesCourses WHERE RoleID = " & Me.cbxRoleID
To prevent duplicate employee/course entries, set compound index on the EmpID and CourseID fields in EmployeeCourses.

Filter out non existing items

I have 2 tables (with different fields) :
tbl_Report (UIN, GID, RDate, RTS_a) > (UIN = unique nr.)
tbl_Employee (GID, Sname, Gname, Org_Cd) > (GID = Unique 8 char string)
the link is 1 to many based on GID
tbl_Report: every Employee (GID) generates monthly one or more reports (RTS_a = SR, IR or OR) of each type
I want to create 2 queries:
(1) for all Employees, so that I can see which employee delivered how many reports of specified type
in each month, even if Employee didn't enter a report at all..
RTS_a WHERE [ enter a Vald RTS_a]
Year([Rdate]) Where [enter Year]
(2) an overview for employees (selected by Report-type RTS_a) who didn't
deliver a report vor a given year (Rdate)
RTS_a WHERE [ enter a Vald RTS_a]
Year([Rdate]) Where [enter Year]
the problem is I don't know how to get also the employees in it, who didn't entered data at all...
I've tried in Query criteria under RTS_a : [enter a Valid RTS_a] ""or [enter a Valid RTS_a] Is Null
but that doesn't seem to work
(& somehow things went wrng when I tried using :
WHERE NOT IN an WHERE NOT EXISTS)
as a non native English speaking Access beginner... (and totally lost...)
I could use some help on this....
You got to use an outer join. In your query, in design mode, you should have both tables, and have a link between tbl_Employee.GID and tbl_Report.GID.
Double click the link (or right click and select Join Properties) and then click option 2 or 3 (the one saying you want ALL Employees and matching Reports.
Edit: for point(2), you have to make a query for those who DID enter data in [Enter Year], then make anther query, "outer joining" Employees and EmployeesWithoutDataInYearX WHERE EmployeesWithoutDataInYearX.employeeId is Null

Microsoft Access question

I have a combo box which has 3 pieces of information
COMBO BOX 210: Materials ID, Name of Product, Cost, Description
After I update the combo box the cost figure is stored in another field in the table, so far so good, so in selecting "Apples" the cost "$1" stores.
Now, what I want to is have a different field where the description of the apple (the text) is stored yet I only update the combo box once, in other words after updating the combo box Field 1 stores the price and Field 2 the description of the apple.
I will offer you a different example which I hope is similar to what you're asking.
My form includes a combo named cboNames. This is the query for its row source:
SELECT f.id, f.fname, f.date_added
FROM food_test AS f
ORDER BY f.fname, f.date_added;
In the combo's After Update event I can access the values of those 3 columns in the selected combo row by referring to the Column index. Notice the column index numbering starts with zero.
Private Sub cboNames_AfterUpdate()
Debug.Print Me.cboNames.Column(0)
Debug.Print Me.cboNames.Column(1)
Debug.Print Me.cboNames.Column(2)
End Sub
So if I wanted to put the date_added value into another data control, I could add this to the combo's After Update event.
Me.SomeOtherControl = Me.cboNames.Colummn(2)
However I wouldn't actually store both id and date_added in a row of another table. In my food table, each id is associated with a unique combination of fname and date_added. So I would store only the id, and use a SELECT with a JOIN to the food table to look up the associated fname and date_added values whenever I needed them.

Access - how to derive a field upon entry of a record

I have an Access database for a media rental company which includes the following tables, among others;
LOAN:
customer_id (pk),
loan_datetimeLeant (pk),
loan_dateReturned (pk)
LOAN_ITEMS:
customer_id (pk),
loan_datetimeLeant (pk),
item_id (pk),
loanItem_cost
ITEM:
item_id (pk),
product_id,
item_availability
PRODUCT:
product_id (pk),
product_name,
product_type
MEDIA_COST:
product_type (pk),
product_cost
So basically the 'product type' (DVD, VHS etc) determines the cost. The 'product id' determines what movie, platform etc each item is.
My question is:
When creating a form for the Loan_Items table, how can I populate the loanItem_cost field (so it is stored) whenever a new Loan_Item record is added? I need to store it as the cost of the item (product_cost) may change over time and I'd like to record what the customer paid at the time the loan was made.
Thanks in advance
Go to the properties for the button and find the OnClick event. Select Event Builder (or something like that, I'm just going from memory) and double-click. This should take you into the code editor. Here you can add something like loanItem_cost = product_cost.
Set the Default Value property of the loanItem_cost form control to reference the product_cost from another form or from the product table.
If you have a form with a button that opens another form, you can reference a control on the parent form in the Default Value property: Forms![ParentFormName]![product_cost].
Similarly, you can use a DLookup function:
DLookup("product_cost", "ProductTable", "product_id = " & Forms![ParentFormName]![product_id])
The source of the product_id value in the "where" parameter of the DLookup function can come from several places, but would likely be found on another form.
You can create an update query to update your loan item cost from the current product cost. Then to perform the update, just execute the query.