MS Access Form: How to show all records that reference current record - ms-access

I have a form showing Customers. Now I would like to also display all Orders that the currently open customer has made.
The Order table has a foreign key to Customer.
I have tried using a list box with Multi Select set to Simple, but somehow it shows me all Orders instead of just the ones of the current customer.
More Details:
My list box has
Control Source: ID
Row Source: SELECT customer.id, order.info FROM customer
INNER JOIN order ON customer.ID = order.customer_id
If I set the Multi Select to None it always marks the first Order that matches the current customer, but not all matching orders.
Can someone point me in the right direction?
Thank you.
P.S. I don't necessarily want the list to be functional for the creation of a new Customer. If it would also work then that's a bonus.

Assuming your form is bound to Customers table, then you need to add code to the form's Current event:
lstOrders.RowSource = "SELECT id, info FROM order WHERE customer_id = " & Me.id
lstOrders.Requery
The list box (called lstOrders) should have its column count set to 2. If you don't want to see the order.id column then set the "Column widths" property to 0 (this will set the width of the first column to 0 and let the second column, order.info, fill the remaining width of the list). Set the "Bound column" to 1 - this means that the "value" of the list box will be the order.id

Related

SSRS Current Document Map Display

I have a report that has document map on two columns...
I am trying to display the currently selected data (CAF SA -> PP5566) on its own page (using page breaks between groups) with the report name and code above the table of data, an example show below with the arrows:
So if I click the next code it will change data accordingly:
Here is how I would probably do it based on what I know.
The following example uses the Microsoft supplied Northwind sample database in case you want to reproduce this.
First I created a dataset pointing to the Northwind database using the query
SELECT DISTINCT
r.RegionID, r.RegionDescription
, t.TerritoryID, t.TerritoryDescription
, e.EmployeeID, e.FirstName, e.LastName
, c.CustomerID, c.CompanyName
FROM Region r
join Territories t on r.RegionID = t.RegionID
join EmployeeTerritories et on t.TerritoryID = et.TerritoryID
join Employees e on et.EmployeeID = e.EmployeeID
join Orders o on e.EmployeeID = o.EmployeeID
join Customers c on o.CustomerID = c.CustomerID
This will give us Region (the top group level), Territory (second group level) and some employee names and customer names which we can use in the detail part.
Next, added a tablix (table) and dragged the first name, last name & company name onto it. This will be the details row.
Next I right-clicked the details group in the row group panel and chose 'Add Group => Parent Group', set the group by to 'TerritoryDescription' and checked the 'Add group header' option before clicking OK.
Next I right clicked on the new TerritoryDescription group and repeated the above, this time using 'RegionDescription'
On territory row I added some static text into the 3rd column (under FirstName) and the territory description field as the expression for the 4th column (under last name). Now right-click on Territory static text you just added and choose "Insert Row => Inside group Above". Add static "Region" text and region field into column 3 and 4 respectively
I added a page header and footer just to it looks clearer when viewing on screen.
Next delete the first two columns as we no longer need these, select 'delete column only' if prompted.
Now double-click the TerritoryDescrition group in the row groups panel, click 'Advanced' and set the document map to 'TerritoryDescription'.
Repeat this for the RegionDescription row group selecting 'RegionDescription' this time for the document map.
The final design looked like this...
I set the report 'InteractiveSize' height to 15cm for clarity.
When the report is run I get this..
If I drill down to a differnt Region and Territory, I get this..

Display New Column for Each Row Value

I'm trying to add a new column for each time an account number shows up in my query. My query below pulls the necessary information and then totals the amount of times a row occurred with the same account number but what I need to do next is have it create a row for each time a dose was given for a patient.
So if I have an account number that pulls up three rows in the table I would like for it to populate a new column displaying the individual value from each row that has that account number in it.
I just need to know what to add to the query below
SELECT LastName, FirstName, PatientNo, COUNT(*) AS 'Occourances Given'
FROM patient
JOIN medication ON patient.medication_id = medication.id
WHERE medication.id = 'variable'
GROUP BY PatientNo, LastName, FirstName
ORDER BY LastName
This is what I would like the output to look like. Take the total dose column and break it down and show what the value of each dose was.
Thanks!

How to return invoice numbers - where all associated detail records have a bit field set to 1

SQL novice here.
I'm trying to get a list of invoice numbers. There are two tables - invoices and invoicedetail. The invoicedetail table has a field called scanned (it's a bit field). If it's been scanned, then the bit field is 1.
For each invoice, there are associated records in the invoicedetail, but I only want a list of the invoice numbers where every associated record in invoicedetail's scanned field is 1.
I'd rather do this in a single query if possible.
The records scanned in InvoiceDetail is all records - for any invoice number returned.
Your help is appreciated.
SELECT
InvoiceNumber
FROM
Invoice
INNER JOIN
InvoiceDetail
ON
InvoiceDetail.ID = Invoice.ID
WHERE
InvoiceDetail.ScannedColumn=1
GROUP BY
Invoice.InvoiceNumber
Or...
SELECT
DISTINCT
InvoiceNumber
FROM
Invoice it
INNER JOIN
InvoiceDetail idt
ON idt.ID = it.ID
WHERE
idt.ScannedColumn=1
Just make sure you change the column names to fit your needs. Use your ID from each table for the joins and then use a WHERE to filter the records to just show those which are scanned. Being that you are using a bit field you will simply use WHERE ScannedColumn = 1.

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 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