cascading combo boxes with bound form - ms-access

I am at my wits end here and have tried every code combination imaginable ( and what I can download) to get those elusive cascading combo boxes to work.
I have 6 of them and this is for a shipping database.
User has to select REGION then COUNTRY and then PORT.
That is both for origin and destination, so 6 in total.
I can get this to work only under the following conditions;
The Form is unbound, and have to use VBA/SQL to insert the selected values into the table.
Once the values are in the table, it is the ID's not the actual value (example "Asia").
I really need the form bound to the shipments table and combo boxes Control Source set to that particular Field.
I have a sinking feeling I am barking up the wrong tree and it cannot be done.
Any suggestions here?

cmbRegions.RowSource = SELECT ID, Region FROM tblRegions
cmbCountry.RowSource = SELECT ID, Country FROM tblCountries WHERE RegionID = frmForm!cmbRegion
cmbPort.RowSource = SELECT ID, Port FROM tblPorts WHERE PortID = frmForm!cmbPort
In the AfterUpdate event of `cmbRegions':
cmbCountry.Requery
In the AfterUpdate event of `cmbCountry':
cmbPort.Requery

Related

Pass parameters from one combo box to another using two forms in Microsoft Access

Just started working with Access 2016 for a project I am on.
How can I pass selected data from one form in access to another?
That Selection will be the determining factor on what data is presented in drop down menus. Basically, if I select a department, I want that departments data to be the "select from" data on the next.
I'm not really sure if this is a clear question, so I've added pictures.
The Goal is to not have to create a new form for every department.
Update:
So I am able to link the department combo box with the lineid box with the following Query
SELECT DISTINCT Line.lineName, Department.departmentName
FROM Line INNER JOIN Department ON Line.departmentID = Department.departmentID
WHERE (((Department.departmentName)=[Forms]![Production_Select_Data_Input_Destination].[OpenArgs]));
I need to get the parameter passed so this is not prompt and the combo box for lineId loads a select list of lineId's that are within the selected department.
I figured it out using
Private Sub btnEnterDataInput_Click()
DoCmd.OpenForm "frmDataInput", , , , , , OpenArgs:=cboDepartmentName
End Sub
In my first form,
and
[Forms]![frmSelectDataInput]![cboDepartmentName]
in my second form combobox row source query.
in my second form.

Query the MS Access table for the id entered in textbox

I have two tables Order and Customer.
In the order form, if I enter the value for the CustId field, I need the value for CustDesc to come automatically with reference to 'Customer' table.
The remaining fields in the 'Orders' form, I have mapped to the fields in the order form directly. I am stuck only in mapping to the 'Customer.CustDesc' to Orders.CustDesc. Because I dont know how to query like below
select custdesc from Customer where [custId= the value of CustId in Orderform]
I am new to MS Access form. pls help.
You can use the lookup function:
= DLookup("[custdesc],"[Customer]", "[custId] =[Forms]![Orderform]![CustId]")
In VBA on the AfterUpdate event:
(in orderform)
CustId_AfterUpdate
[Forms]![Orderform]![CustDesc] = Nz(DLookup("[custdesc],"[Customer]", "[custId] =[Forms]![Orderform]![CustId]"))
- By setting the control source property of the desired field to the same statement:
= DLookup("[custdesc],"[Customer]", "[custId] =[Forms]![Orderform]![CustId]")
It might be easier to use a query that joins both tables as your recordsource for the form instead of your orders table. That way you don't need to come up with this kind of construct to find one other value specifically.
Query:
SELECT O.*, C.custdec as CustomerDesc
FROM Order O left join Customer C
ON O.custId = C.custId
If you also need to be able to edit/add data to those tables from this form, you'll need to set the RecordsetType property to 'dynaset'.
Another option would be the use of subforms (customer data in the main form, data from different orders in the subform), but that depends on what the form's intended purpose is.

How to filter specific records using a combobox and display in a subform

I am trying to filter records from a combobox, if I select Cape Town only Cape Town records must show in the subform but if I select Durban only Durban records must display in the subform. I dont get any errors but if I Select Cape Town only Cape Town records shows and as soon as I select Durban the the Cape Town records still display it doesn change to show the Durban records in the subform. Heres my code below any help will be much appreciated Thanks
If Me.Combo0.Value Then
strFilters = "Sites = 'Cape Town'"
DoCmd.OpenForm "Home"
Forms!Home![Plant Transaction subform].Form.Filter = strFilters
Forms!Home![Plant Transaction subform].Form.FilterOn = True
ElseIf Me.Combo0.Value Then
strFilters = "Sites = 'Durban'"
DoCmd.OpenForm "Home"
Forms!Home![Plant Transaction subform].Form.Filter = strFilters
Forms!Home![Plant Transaction subform].Form.FilterOn = True
End If
As long as your tables are set up right you should be able to do this with little / no coding at all.
Lets create a table called destinations and put 3 fields in: Unique ID, Townname and Area: example:
ID | TownName | Areas
CT | CapeTown | PlaceWithinCapetown
CT | CapeTown | AnotherPlace
D | Durban | PlaceHereAswell
so on and so on.
Lets assume you have 2 elements on your form. A Combobox and a Listbox.
ComboBox - List of Towns
ListBox - List of Areas Within Town.
Lets say the combobox is called cboTowns and the ListBox is called lstAreas
Click on the combobox and in the Row Source place your query, Something like:
SELECT DISTINCT destinations.ID, destinations.TownName FROM destinations;
Here i have asked for the Unique ID and Town name and set my bound column to 2 (So Only The Town Name Shows when user selected one), Now your combobox is populated with the towns, You will note the towns are listed more than once. You need to change the property's of your query to allow Unique values only Go to Row Source, Click on 3 dots this take's you to query builder select a blank area and on the right find the setting Unique Values set this to Yes and the combobox is complete.
Now i have the list of Towns and the Unique ID we can get the listbox to start working. Before you move on if you List all of your information all in 1 table as detailed above it is easier to lookup the data. If its over 2 tables you will need to create a relationship in Access. before moving on.
Once done go to the list box and again in your row source you want to put something like:
SELECT DISTINCT destinations.ID, destinations.Areas FROM destinations WHERE (((destinations.ID)=[Forms]![FormName]![cboTowns])) ORDER BY destinations.Areas;
So here i told it to look for Unique ID that's been set via combobox and then look for areas attached to that Unique ID.
Every time the user selects a an entry from the drop-down box the list box updates straight away. no other coding needed. If you have more information and need to display other things you can then use the DLookup function to populate any other fields you might have.
I will also say if you don't want to restructure what you already have looking at the code you supplied I noticed you are not requerying anything. You will need a Me.requery after end if in order to refresh the form. otherwise it will not update anything.

Query with data from subform and sum

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.

How to Reference Individual Form Elements in a MS Access Continuous Form

MS Access Scenario:
I am using a form (we'll call it the select_contract form) and a subform (we'll call it the employee_allocations_by_contract subform).
The select_contract form:
Is unbound.
Contains a combobox that allows the user to select a contract id.
The employee_allocations_by_contract subform:
Is a continuous form.
Is bound to a table (which I'll call the hours_allocation table) that contains the number of hours that each employee is allocated to each contract.
Binds only one field, the employee_id field, to the hours_allocation table. The row source for this field is a query that returns the ids of employees that have hours allocated to the contract that the user has selected in the select_contract form.
Contains twelve other, unbound fields, one for each month of the year. These fields are intended to display the number of hours allocated to the employee listed in the employee_id field for each month of the year.
The Problem: In order to display the number of hours by month that are allocated to each employee listed on the employee_allocations_by_contract subform, the queries for each month field need access to the employee_id field of the row on which they appear. I haven't been able to figure out how to reference this field. Because the employee_allocations_by_contract subform is a continuous form, a reference to the the employee_id field name appears to reference each row on the form.
I have searched several forums and have found related continuous form issues, but nothing that clearly addresses this problem. Any thoughts?
Well, you could build a sub-query for each of the month columns you need that total for.
Something like:
Select id, employee_ID, bla, bla, bla,
(select sum(hours) where month = 1 and year = 2010 and
employee_id = ehours.Employee_id from tblProjectHours)
as month1,
(select sum(hours) where month = 2 and year = 2010 and
employee_id = ehours.Employee_id from tblProjectHours)
as month2,
(select sum(hours) where month = 3 and year = 2010 and
employee_id = ehours.Employee_id from tblProjectHours)
as month3
etc. for each column needed
from ehours
Note I used month and year as columns and you likely have to use month([SomeDate]) = 2 and year([SomeDate]) = 2010, but you get the idea.
Also, while you can't address each row and stuff a value into that un-bound text box, you CAN in fact bind the text box to a function that returns the value/expression you need.
txtBoxMonth1 txtboxMonth2 etc.
=MyMonth(1,[employee_id]) =MyMonth(2,[Employee_id])
And, then in the form, you have a public function called
Public Function MyMonth(intMonth as interger, lngEMPID as long) as long
' code here to get total based on employee id and month
End Funciton
So, you can bind a function to those text boxes, and the current row emp ID can thus drive what each text box displays. Just remember that this function should have all of the data pre-processed, and you don't want to have to re-run and re-load the data for each function call as that will be FAR too slow. However, I have used this approach to much success. So, each row of the form only has one real column (empID), the rest of the columns are derived from text boxes bound to the public function as per above with the month being passed along with the emp id to return the value for that column.
So, above is two possible approaches I used with success.
An unbound control on a continuous form can only refer to the record that has the focus. There fore, if data is entered into an unbound control, you will see the same data for every record in the continuous form.
You cannot reference controls "individually" ("at the line level") in a MS Access continuous form.
Your only solution here is to bound these controls to an underlying recordset\recordsource where corresponding fields hold the values to be displayed.
For example, if you want to display a time period as the difference between a "date in" and a "date out", your recordset\recorsource could be something like:
select id_person,dateIn,dateOut,dateDiff(xx,dateOut, dateIn) as timeIn from ...
Then your time calculation control has to be bound to the 'timeIn' field of the recordset.
(*) please check the dateDiff arguments. I do not have any help available here ..