MS Access: Add Const Value to Table from Different Table - ms-access

I am building a simple invoice form in MS Access and have two tables that I am trying to link data across. One is a product table, which includes the price field. However, because the price can change at any point, I want to store the price at the time of sale in my line_item table as a constant, unchanging value.
Furthermore, I want to be able to lock both of the values in the invoice form so neither is able to be changed. Because of this, I don't want my sale_price entry in the line_item table to be manually entered. I want it to pull from the product table's price entry.
However, I have yet to find any documentation to help me achieve this exact result.
Can anyone help me out?

This is a common requirement.
Set textbox properties Enabled No and Locked Yes and TabStop No.
To retrieve the price it can be included as a column of combobox where product is selected.
Save the price into record with code behind the combobox AfterUpdate event. It can be done with a macro but I use VBA, ex:
Me!sale_price = Me.cbxProduct.Column(x) - for x reference the column index with the price, index begins with 0.

Related

How to change a field on a sub-form depending on a value of a record

I have a product table with 2 different price for each product. In customer table I have a price category for each customer. When I open the order form, I choose the customer from a drop down list and the field of pricecategory show the type that the current customer has (1 or 2).
I tried to transfer the value of pricecategory to sub-form but I can't handle how to change the control source of the price field. Probably this approach doesn't work. How can I complete this task? I need the logic structure.
I tried with IIf statement in the control source with no success.
iif([forms]![Orders]![PriceCategory]=1,[Price1], iff([forms]![Orders]![PriceCategory]=2,[Price2]))
I try to find a way to change the control source from price1 to price2 in the field of a sub-form. Actually I want to automatically change the price field depending on the customer.
Thanks

MS-ACCESS - Using returned value from DLookup as part of calculation

Please don't laugh. I'm sure this is something simple for all of you but as a novice, I'm in need of help.
I have an invoicing form which contains a subform. The subform allows users to select products and quantities.
For each product selected, users enter a quantity and the subtotal is automatically calculated. This part is fine.
My problem is, when I want to provide an overall total in the footer of the subform, I am just getting £0.00.
Here are the details:
Field one is called "Unit Price". This unit price is found using DLookUp. The formula is "=DLookUp("[Price]","[tblCashPrices]","[ProductID]=" & [Product] & "AND [SalesTypeID]=" & [Forms]![tblInvoice1]![SalesTypeID])"
This auto-populates and seems to be fine.
Field 2 is called "quantity" and is just a number field where users can enter an integer.
In the subform, I want to generate the overall total. I have tried "=sum([quantity] * [unit price])"
This is just returning £0.00.
I don't know what is going on because my subtotals for the products work but the overall total doesn't?
Please help
Aggregate functions must reference fields from table or query used as the form RecordSource, not a calculated control.
Better options than DLookup() on form to retrieve related data.
include the lookup table in the form RecordSource, join type: "Include all records from tblInvoices and only those from tblCashPrices that match."
multi-column combobox for the products, the price can be in a hidden column, then a textbox can reference the combobox column by index
Option 1 will allow the Sum() function to reference the Price field. Optionally, do the DLookup() calculation in a query and use that as the form RecordSource. Then the calculated field can be referenced in Sum() calc. Just be aware domain aggregate can be slow performer in query or textbox.
Another option is to create a field in table to store the price. Then use code (macro or VBA) to execute DLookup and save the retrieved value into that field.
Also, your DLookup() has syntax error in the WHERE argument. Need space each side of AND.
Record must be committed to table before the Sum() calc will update. Record is committed when: 1. close table/query/form; or 2. move to another record; or 3. run code to save.

Cannot add record(s); join key of table not in recordset in MS Access

I'm new to access, and I have been able to find a solution for most of the hiccups I have experienced with MS Access, but I cannot get a combo box in a form to function properly.
I have tried unbounding it, but then it does not update the corresponding field in the query the form is based on. I have tried to bound it to the EmployeeID, but then cannot select the Employee in the combo box because EmployeeID is an autonumber field.
If anyone can offer me any advice on how to get this to function properly, it would be greatly appreciated. Also, the basic intent of the form is to allow an employee to select their name, enter a date, and check the appropriate boxes. All of the other values function properly, but I do not want the Employee table to change every time a maintenance log is created weekly...
I hope I explained everything well enough. Attached below is a copy of the database.
Maintenance Log Database
You have to set up the combo box with a row source that contains both the Employee ID and the Employee Name. Use Employee ID as the bound column. Then in the display properties set columns to 2 and column widths to 0;1 (assuming you set your row source so that Employee ID is column 1 and Name is column 2).
This way your combo box displays the name but it actually binds to and uses the ID.

Update single records in query with form based textbox

I am working with Access2013 and I have a query called PaidOrderQ with columns "SalesRep", "Customer" and "PaidAmount". I need to calculate the SalesRep's commission which isn't always the same percentage for each record. After reading through similar questions here I still haven't figured it out yet.
The way I thought doing it is to have a form called PaidOrderF with soucre is PaidOrderQ, a textbox called "CommRate" and a calculated field "Commission". The "Commission" gets calculated by "AmountPaid"*"CommRate".
I'm not sure how the percentage for commission is calculated, but there are a few ways this can be done depending on how the percentage is calculated. The first would be if you can calculate the percentage that will be used automatically. Then you can add everything automatically (even if the percentage changes based on the item). If this is possible you can open the query up in DESIGN view. Then click onto a blank field and you can make a calculated field for example:
Commission: [AmountPaid]*[CommRate]
Now you can add the calculation for CommRate for example:
CommRate: IIf([AmountPaid]>200,.15,.2)
Which would make any the comm rate 15% over $200 and 20% under.
If not you can make Text boxes in a form (which would be unbound and add an after update vba code to multiply them together and change the value of a third text box, label etc.)
You may also want to add a column to the original table to show commission rate so the information would be stored for future reference.
Let me know if anything is unclear or you would like to see an example of the vba code
EDIT::
Storing information to a table:
This one will be very basic and you probably know most of this, but just in case I will include it because it is the most straight forward.
Add a column to the table you are working from by opening up the table in Design view (you can also make a temporary table by using a make table query if you are using a liked table or do not want to modify the structure, but it is better to put it in the main table if possible). Here you could make a query that includes all of the fields plus the additional calculated field that shows amount paid * commission. Then when you make a form based on the query you will enter in the commission rate and the form will both save the information and automatically update the field to give commission.
Linking another table by Primary ID -
If neither of those options work you could also create another table that shares a primary ID. This option will be more complicated, but is possible. (I would recommend not making it a true primary ID for the second table so it is easier to modify (Or have a true primary ID and the ID you will use to link ie. 3 total columns)). If this is the option you need I can go into more depth, but the disadvantage of this method is if there is no CommRate listed for a given record it will automatically hide the information (it is possible to avoid this, but again will be more complicated) so it will be easy to make a mistake.
VBA coding
VBA will be the most complicated solution, and has the largest disadvantages. First - You will have to loop all of the records at one time (or at least for one employee) and when you close the form you will lose everything. If there is any way to avoid this I would suggest doing so, but you could theoretically store the information in an array.
The Standard Way -
As I understand it your table looks like this:
SalesRep Customer PaidAmount
John Eric 2040
Stacy Brian 1020
Stacy Eric 2004
etc.
You will want to open up your table in Design view and add a column "CommRate"
so it will look like:
SalesRep Customer PaidAmount CommRate
John Eric 2040
Stacy Brian 1020
Stacy Eric 2004
etc.
The data type for CommRate should be Number - type Double - Now I would use Comm rate as a percentage - ie 10.2 = 10.2%, but you could also do .102 = 10.2% either one is fine (more on that later) save and close the table.
Now with the table selected use the create query wizard add all of the fields to the query, give the query a name and click modify the query at the end (or save it then open it up in design view). Now you will see each field in a column with the table listed below. Copy and paste the entry below to your query in one of the bank "field" locations:
for percentage ie 10.2=10.2%
Commission: [CommRate]*[PaidAmount]/100
for .102 = 10.2%
Commission: [CommRate]*[PaidAmount]
Now save and close the query.
Lastly you will select the query then click on create form. This will make a form based on the query. With this form you can add the CommRate individually and it will be saved for the future. You will see that when you add the CommRate the Commission field will automatically update and the table will get updated with the new value.
This is the standard way of doing this. Does this work for what you are doing?
Alright well that is good news! The primary key will help a lot:
Ok here is how you can solve this with two tables.
First create a new table with three columns:
"ID" - Primary Key "RelatedID" - Number Long Int "CommRate" - Number Double
This table will basically just be used to "add on" the CommRate without changing the linked table.
Next you will make a query that includes "SalesRep", "Customer" and "PaidAmount" from the linked table AND the "RelatedID" and "CommRate" from the new table.
When you view the query in design view you will see both tables above the list of the different fields and you will click on the primary key from the linked table and drag it to the "ReatedID" in the new table. From here it will open up a dialog asking about the relationship join. This part is the most important - You want to make the relationship a LEFT JOIN - You do this by saying you want to show ALL records from the Linked table and only the record from the New table that match. Basically this makes it so it will always show the records from the new table even if there is no entry that matches in the new table.
After you have that created you will just make a calculated field as before by copying:
Commission: [CommRate]*[PaidAmount]/100
to a new field on the query design
From here everything is set up and you can create a form based on the query you just made - I would delete the field "RelatedID" from the FORM (or the Query the relationship is what links it so its not necessary it is just a good double check) so you don't change it.
How it works - Now when you cycle through the entries you can add the commission rate directly to the form. - This will make a new entry in your new table that shows the primary ID of the transaction and the commission rate for that sale. This will update the calculated field and show you the commission. Then when you view it it will save the values individually without changing your original table.
Keep me posted if you need more help on how to do this. It can be a little tricky the first time you do it.

How do I select a specific column based on a variable in a MS Access query?

I have a large table with the following fields:
Date
Product_ID
AmountEUR_Field1
AmountEUR_Field2
AmountEUR_Field3
AmountEUR_Field4
AmountEUR_Field5
where each AmountEUR field represents the sales amount for a product.
The reason for having 5 different AmountEUR fields is that they are based on different Currency Rates (in example BeginingOfMonthRate, AverageMonthRate, EndOfMonthRate etc.).
I now want to copy a specific AmountEUR field to another table, but the AmountEUR field to be copied varies over time (sometimes it is AmountEUR_Field2, other times it is AmountEUR_Field5).
Therefore I need to select a specific column based on a variable from another table. (that variable should then have value between 1 and 5).
I have been thinking about making a new field called AmountEUR_ToBeUsed that is updated with the correct AmountEUR_Field, but that brings me back to the same problem of selecting the specific column I want copied.
Can a solution be made within the Access query designer, or do I need some VBA code?
You can just make this with the Access Query designer.
Specifically you will need the function IIF.
For instance, if you want to specify that before a date you wish to use AmountEUR_Field1, and otherwise AmountEUR_Field5 you can say:
IIF(somedate<#1/1/2011#,AmountEUR_Field1,AmountEUR_Field5)
Note, depending on the settings of your PC, you may have to say:
IIF(somedate<#1/1/2011#;AmountEUR_Field1;AmountEUR_Field5)