How to store sums into MS Access table? - vba

I know how it sounds, you shouldn't store calculated fields into an Access table. However, this is my case:
I have a form where a user inputs bale weights. At the bottom of the form are two fields that calculate the total weights of all the bales, and the total number of bales. These two fields need to be stored into our table so we can keep track of the totals from shift to shift.
I have the two calculated fields, and then I have two hidden fields that have control sources for the table. The two hidden fields are in the parent form of this bale weight subform. There is an OnClick event on the control when you enter a new bale weight that updates the parent hidden controls with the form's calculated controls.
The problem:
These fields are not updated immediately after entering a new bale weight, and if I put requery, recalc, refresh into the afterupdate event the cursor goes to the first record which is very user-UNfriendly.
Sometimes someone has to delete or change a bunch of records in the middle of this form. every click and update bringing you to the top (or to the bottom if I add DoCmd.GoToRecord,,AC NewRecord).
TLDR:
I need a way to get the sum of all the bales from a 12 hour shift to store into our table that doesn't involve refreshing, because that messes up the placement of the cursor if the records need to be updated.
I tried updating the table after the bale weight form loses focus, but this isn't consistent with updating both total bale weight and total bale count hidden controls immediately.

You can use the SetFocus command on your expression.
Then the cursor will go to that field.
Me.controlfield.Setfocus
Or
Me.controlfield.Text.Setfocus

Related

MS Access Subform adding multiple rows

I have a form that I use for memo generation which I just added a new subform to which handles contract info. It is a small table which only has the PurchaseID (linked to the main table) and then the contract fields shown as text boxes/combo boxes.
My Issue: Originally what was happening was when a user didn't click into the sub-form shown, no row would be created in the Contract table. If you look at the picture, when the form loads the "PurchaseID" field is populated because it's an autonum linked to the main table. However even though that field is populated, if a user doesn't physically click into the sub-form, no row gets created. I just want the blank row with the default options to be added to the table.
My attempt to fix it:
What I tried doing to fix this is on the event "On Current", I wrote 1 line of code that just sets the value of the combo box to a value. I think this sets the sub-form as a focus and it does end up creating the record. The problem is, now whenever the form loads, it initially tries to add a blank row with the ID of 0 to the Contract table, essentially making a duplicate blank row every time the form opens. I think the reason is because the "On Current" event is happening before the "PurchaseID" field is linking to the main table, resulting in a PurchaseID of "0" continuing to duplicate when the form is opened.
What I need:
Looking at the picture below. How do I make it so when a user does not click in that subform box when going through everything, a new record is added to the sub-form table with the default values selected (procurement/notrequired/etc.).
Picture of form. Sub-form that has issue is circled.

Oracle Apex List of values selection and button disable upon repetition of same selection

I have an interactive report with two regions.
The first region is static region with a select list and a button to submit the process.
The second region is the report region which refreshes the region based on first region select list value and Process (calculate) button submitted.
However if I try to submit same item (List of values) subsequently the process button must be disabled. (i.e) Any repetition of same List of values, once calculation or process is performed, the calculate button must be disabled and these are all based on input selection from front end.
Please note there is not any direct relationship between the Select list button (Page item) and second region columns and I cannot achieve it by distinguishing at database level if it is processed or unprocessed, as there is not any column maintained in database tables (in this case second region about year which is processed).
How can process be disabled second time?
and only if select list for quarter is entered for first time the process button (Calculate) must be enabled based on.
The second region values are populated/refreshed by swapping the columns this_year_q1 to last_year_q1, last_year_q1 to year_before_last_year_q1 etc., if calculate button is enabled and process is called.
So in below case, the calculate button is enabled when select list is (Quarter is Q4:01/10/2017 to 31/12/2017), this is fine and must allow process for first time.
Subsequent reselection of same value must not allow process to calculate.
1:
Here's an idea:
create a (hidden) page text item
using a dynamic action on a button
compare current Select List item's value with hidden item's value (don't forget the NVL function!)
if they don't match, put current (selected) Select List item value into the hidden item so that you could check it when the button is pressed once again (i.e. whether user selected the same value or not) and submit
if they match, do nothing (don't submit)
If you don't want to run the report twice not only for subsequent executions but during the session, you'd have to store selected values somewhere (a table?). The rest would be the same - you'd only have to select from that table and compare Select List's values with values already used.

Prevent all Access Form ListBoxes from updating

I have a form that gets populated with rows of data from a database. I want to add a listbox to each row for the user to select a letter grade. When I run the form all the data looks correct, but if I update a single listbox all the listboxes in the form update to the same value. It seems like the listboxes aren't independent of each other.
An unbound control will always display the same on all records.
For individual display, you must bind it to a field of the record - or redesign your form in some way.

How to get min/max values on form that returns multiple records

I've got a form that returns multiple records from a table. Each of these records has a field indicating what the measurement was and the measurement's value. My form's record source pulls the appropriate ID's set of measurements and values, and then I want to write each of these measurements to different text boxes on the form.
The problem is that I'm also keeping track of the date this data was updated, and want to include only the data pulled based on the "date updated" selected at the top of the form.
When I go to pull the measurement value into a text box (with something like max(iif(Measurement="Acidity", [Value],[Null])) I get #Error all down my form. This had been working earlier today, and as I've been developing the form something changed and now it no longer works (everything but the date returned is saying #Error). I've set it to Requery when the form loads and whenever the date selected is changed.
EDIT:
This form is actually a subform of another main form. The main form has a control called MemberID which a user can use to select the Member they want information about. This is based on a query that pulls from a table the information related to that MemberID (one row per MemberID). When the form loads, the query behind the main form needs to be requeried to get the data for the selected MemberID.
Then there is this subform causing the problems. On this subform is a "Data Updated" drop down box, which lists the possible dates that the data was updated. When one chooses a particular "Data updated" in the drop down box, this chooses the latest data for all the data measures up to that date selected. This means the form's record source pulls about 10 records in my case.
Then, on this subform I try to layout these 10 records' values in different text boxes. So, in my acidity case above, I would say iif(Measurement="Acidity", [Value], Null). Because there are 10 rows returned, I will end up with 9 Nulls and 1 value, and to get that value I use the max function, so the text box's control source reads: max(iif(Measurement="Acidity",[Value],Null)).
The strange thing is this code was working this morning, until later this afternoon when I was filling in the remaining text boxes with similar code, and then it stopped working. I had also renamed the boxes from Text71 to more relevant names, but none of these are referenced in the code.

MS Access sum of related records

I have a purchase form that has a continuous subform which shows line items for that purchase. Each line item needs to include a text box that shows the sum for a number of related records in a table for that line item.
(Each line item is allocated for a specific purpose(s). For instance, a line item says that 100 widgets are ordered; the allocation table says that 20 widgets are for this purpose and 40 are for this purpose. The text box needs to say that 60 / 100 are allocated.)
I have written this in VBA for the OnCurrent event of the form, but this only occurs when the first "copy" of the continuous form receives focus. Then all the other "copies" of the form show the same values in the text box.
I then realized that I could perhaps do this as part of the query for the form. Whenever I add ... Sum(Quantity) AS TotalAllocations to the SQL query, I get the following error:
"You tried to execute a query that
does not include the specified
expression 'VendorPartNumber' as part
of an aggregate function."
(VendorPartNumber is a field for the records that show up on the continuous form.)
Can someone explain how to do this successfully in a query designer / SQL view or with VBA?
Complete aside:
Ideally, it would be nice to have this as a continuous subform on a continuous subform. However, Access does not allow continous subforms on continuous subforms.
If you're going to SUM in SQL, you need to include a GROUP BY clause for the non-aggregated columns, such as:
SELECT VendorPartNumber, Sum(Quantity) as TotalAllocations
FROM YourTable
GROUP BY VendorPartNumber