I have three tables:
Products(ID,Name,Price) /
Customer(ID, Name, Surname)
Buys(ID, ID_customer, ID_product)
I have a form with subform. Subform gets populated with query from Buys table and then connected to Customer(ID) via ID_customer. ID and ID_customer gets hidden on subform.
Then I have two more fields/controls added on subform: Name and aPrice which gets populated via ProductsQuery:
SELECT Products.Name, Products.Price
FROM Products
WHERE (((Products.ID)=[Forms]![PregledKupcev-Form]![NAKUPI-Subform]![ID]));
//ID in this case is control on subform which holds ID of a product
using:
=DLookUp("[Name]";"[ProductsQuery]")
and
=DLookUp("[Price]";"[ProductsQuery]")
So far everything works but it gives me alot of troubles later when i try to sum one control (Price in this case).
Is there any way to do this better?
Then I try to sum up things in aPrice control into PriceSum control on subform's footer:
=Sum([Forms]![PregledKupcev-Form]![NAKUPI-subform]![aPrice])
and transfer it to form with:
=[Forms]![PregledKupcev-Form]![NAKUPI-subform]![PriceSum]
but I get error..
How do I sum up values in Price control on subform?
Pictures:
Let's say that your main form has a text box named txtInvoiceNo in which you display the Invoice Number (or whatever field links your parent table to your child table).
Let's also say that your main form has a text box named txtInvoiceTotal where you want to display the sum of the [Price] values for each child record.
Set the Control Source of the txtInvoiceTotal text box to do a DSum() on the child table (which I've called InvoiceLineItems):
=DSum("[Price]","InvoiceLineItems","InvoiceNo=" & [txtInvoiceNo])
In the After Update event of the subform, add a line to .Requery the parent form's txtInvoiceTotal text box:
Private Sub Form_AfterUpdate()
Me.Parent.txtInvoiceTotal.Requery
End Sub
See if that does the trick for you.
Related
I have created a simple inventory management Access DB which in its basic functionality works perfectly.I would like to start to make some polish here and there and I've found the next issue.
I do have a main form for inventory transactions. It has a Header section (with a table of transactions header as a recordsource) and a details section (datasheet type subform into the main form; which means a multiple records form where I can add or remove multiple lines for each header transaction and it is linked to the main form, that works OK).
In this detail/lines subform I have the fields: Category (combobox), Item (combobox) and Quantity (category and item fields are linked to "master categories" table and "master items" table).
I do want to add the following behavior:
When I select a category on category combobox field, the item field updates its selection to show only the items in the selected category.
I can do this easily on a single record form with this code:
Private Sub Category_AfterUpdate()
Me.Item.Requery
End Sub
On the Item field datasource I do have a simple query:
SELECT ID,DESC FROM ARTICLES WHERE ARTICLES.CATEGORY = [FORMS]![SUBFORM].[CATEGORY]
This works flawlessly with a single record form.
However as this is a multiple lines/records form when I select a category on a line, it updates the "Item" selection field for ALL the lines/records, even changing values depending on the selected category.
Is there a way to update controls/fields in an independent way for each record on a datasheet form? Let's say something like:
Private Sub Category_AfterUpdate()
Me.Item(current record).Requery
End Sub
I'm thinking on adding an index to the combobox article field and update only the field on that index but I don't know how to proceed or if it's possible to do that.
Any ideas or workaround to do this?
I have a auto number in a table that serves the purpose of a order id. I made a form to input line items for this order id into that table . I want a function that would allow me to click my "add to order button" and it would continue to use the same order id for every record until I complete the order. I tried the following in the on_click sub for my add record button. me.txt_Orderid = me.txt_OrderId - 1 but naturally that doesn't work. I am fairly new to access so the access vba is tripping me up. Ideas?
You should not be using AutoNumber on a field if you are wanting to multiple records of it. Instead create a new field and use that for your orderid. When you create new orders youll want to grab the max of that field, then add one. see below...
On Error Resume Next
DoCmd.GoToRecord , "", acNewRec
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
End If
'This is where you assign new order numbers.
Me.OrderIDFormControl = (DMax("OrderIDField", "OrderTable") + 1)
This will acheive the same result, but is better. And you can have duplicates of it.
Once you fix your ID, then every time you add a OrderLine Item you just grab the id of the order, and assign it to the new records.
There would be two tables tblOrder which will be master table for Orders. You can use OrderID as autonumber in this table. And second table would be tblOrderItems , this would be the detail table having duplicate values of OrderID for more than one Order Items.
Create a form frmOrder that binds to tblOrder, create another form that binds to tblOderItem. Now open the Order form in design mode, press F11 to see other forms in database, drag and drop frmOrderItem being create. Now this form will be a subform inside main Order form.
The sub-form property, set Master field, Child field property . You will be asked to set related fields, it would be OrderID from both the tables/forms.
You can use this form without need to code to get OrderID when inserting data to tblOrderItem. Access picks up value for you and adds.
Still if you want to have OrderID in your code for any reason or coding a unbound form/subform. You can get the value of OrderID from frmOrder form. when referring this tbOrderID text box control anywhere in the code of frmOrder, you can just use tbOrderID.value , but if you want to refer the text box in subform, remember to refer it with form name or get from Parent property of sub form for e.g. Forms![frmOrder].tbOrderID.value in case you want to use with Parent e.g. Me.Parent.form.tbOrderID.value
I have a form with multiple records, one per row, being pulled from a table. I have 2 ComboBox controls on each row. The first ComboBox's options are linked to a table of Categories. The second ComboBox's options need to be linked to the subcategories of the first ComboBox.
I have tried having the WHERE statement in the row source at design time, which produced all the subcategories.
I have tried having the WHERE statement, and a requery command, on the change event of the category ComboBox, which produced the subcategories for the last changed category. This was an issues because all the rows would have the same options, instead of the options being based on the category for that record/row.
What I need is for each row to query the subcategory/category mapping table and filter the results based on that row's category selection. Is this possible? If so, how can this be done? I am not afraid of using VBA to get the results I need.
In the On_Current event of the form, you can change the row source of the second combo box dynamically dependant on the value of the column of the first combo box. You probably also need to do it on the after_update event of the first combo box to.
Something like this
Select Case Me.cboCategory
Case "Category 1"
Me.cboSubCategory.RowSource = "qryCat1Subclass"
Case "Category 2"
Me.cboSubCategory.RowSource = "qryCat2Subclass"
Case Else
Me.cboSubCategory.RowSource = ""
End Select
I don't have two combo boxes, just one, but its contents have to be based on a value in another field. This is how I do it.
I have a form that contains a continuous subform that is linked with ID,Date and Shift fields.
ID is not changable in mainform. but date and shift fields can be changed.
The problem is when i change the value of Shift or Date in mainform the corresponding values are not being updated.
EDIT
I made a query to refresh the table of continuos form. It works. But i need to use this code in mainform.
UPDATE SubTable, MainTable SET SubTable.[Date] = [MainTable].[Date], SubTable.[Shift] = [MainTable].[Shift]
WHERE (((SubTable.ID)=[MainTable].[ID]));
I think you need to link the two forms only by a single field. In My scenario I created a field on the sub table called "MainLookup" (Integer). This links to field "ID" on the main form. Changes are made to Date and Shift in the main form. If you were to create a query based on the sub and main you would have only one record of Dates and shifts for each ID. Looking at forms, I created a Main form and a sub form linked ID:MainLookup. The sub form only shows data in the sub table, but the Date and Shift data are intact and as recorded on the main form.
I'm working on a small little project, at the moment just a test for a larger project, but I've hit a block that I can't find a solution too.
I have a list of items in a table, and they contain information such as the item name and the price. I have another table with sales. I have a form that will allow you to select a item name, however, then I would like the rest of the form to be auto-filled with information from the items table.
I can make a message box appear when the item name field has been changed, however, through my searching, I am unable to find a way to search the items table for the value of the field, then read the price value, then insert the price value into a field in this form.
First table "items":
itemname: text
price: currency
Second Table "sales":
itemname: text
price: currency
date: time/date
I hope I have explained myself well enough, if not, just ask and I will try to clarify.
Have a look at the Northwind datatabase (northwind.mdb) that ships with Access. They have several examples of how to update a form based upon a combobox changing values.
Also, you might want to look at the table design in the Northwind Database as well. Generally speaking, it is a bad idea to use the product name as the key field in both of your tables. What will happen (and it will eventually) is that the product name is going to change for one of the products and you'll have to update all of the tables that reference that product name.
The better design is to use a key field (I'd recommend an AutonumberField) in your products table and then reference the key field in the sales table. This way, if the product name changes you only have to make the change in one location, not many.
Here's a sample table layout to illustrate my point:
Table Items:
ItemID (Autonumber - Primary Key on the table)
ItemName (Text - Name of product)
Price (Currency)
Table: Sales
ItemID (Integer - Foreign Key to Items.ItemID)
Quantity (Integer - # of units ordered)
Price (Currency)
OrderDate (Date/Time)
Most answers here are posting way too much code and all kinds of SQL statements. As such, these answers are creating world poverty.
The most simple approach is to let the wizard build a combo box on your form to select a given item. This combo box will thus based on the items table. The first column of the combo box will be the PK (autonumber ID) of the items table. This combo box will thus be bound to the ItemID column in sales. The wizard will also “hide” the id, but you WANT to include the other columns in this combo box such as price etc. And you likely should have the 2nd column of this combo box the description of the item from the item table. As noted, you ALSO want to include the price column.
Thus, after you select a item, then to have auto matic fill out the price column, use this code:
This code goes in the AFTER update event of the item combo box:
Me.Price = me.ItemComboBox.column(2)
So when you select a item, the price field will to auto filled for you. And note how you only had to write one line of code.
So all of this can be done using built in wizards and one line of code.
Add an event procedure (code builder) to the dropdown box's onchange event. Right click the dropdown in design view and choose properties. then on the event tab in the properties window click in the on change line and click the '...' button.
In the code for this event you'll need to query the DB (using the dropdown box's index or ID field) to pull the items details into a recordset. The query would look somethign like
"SELECT * FROM Items WHERE ItemID = " & dropdownboxname.value
theres plenty of examples of how to do this on the web
Then you can update the various textboxes in the form with the required fields in the recordset. again theres plenty of examples of this on the web
EDIT:in response to comments below
you'll need to do somethign along the lines of...
Dim rsItems AS DAO.Recordset
Set rsItems = CurrentDB.OpenRecordset("SELECT * FROM Items WHERE ItemID = " & dropdownboxname.value)
If not rsItems.EOF Then
textbox1.text = rsItems![fieldname1]
textbox2.text = rsItems![fieldname2]
end if
Set rsItems = nothing
let me know if thats any help ;-)