Detecting data changes with Form events - ms-access

If you've read my other recent questions, you've prob'ly picked up that I'm not really an Access developer.... Most of my background is in VB, where I assiduously avoided bound controls.
Now I'm working in Access 2007. I have form that's based on a table (well, a single-table query), and a subform upon it based on a one-to-many child table. I want to detect user-made changes in the data on either of these forms so that I can update a date stamp in the parent table. The actual date field is not being shown to the user (at least not here).
What would be the appropriate event to catch the fact that the change has been made? How should I make the actual change (direct to form.Recordset!dateField | with a hidden bound text box | some other way)?

You'll have to look for the afterupdate event, which is fired when the updated content of a bound control is updated in the underlying recordset.
In this event procedure, you should be able to write the needed instruction to update your date field. It is not necessary to have it bound to any control on the form, as long as the field is in the recordset!

I think you can make a trigger to update the stamp of the child table per line of item that the user have made changes. I normally use it on afterupdate event in relation to the index number of the active line (if you are in a datasheet form or continues form) being edited. But of course you can count on how many times each record was updated and you can update the stamp date of the last update.

Related

How do I get my Access form to re-open to the last unbound text box entries?

I have created a form to update criteria in various queries in my database. I have 4 unbound text boxes. The user enters the new dates into the text boxes and I've added a button to run the queries. The query results look great.
The problem is, I want to be able to close the form and upon re-open have the dates that were last entered re-appear. Instead, the text boxes are blank and the user has to re-enter the dates.
Basically, the same dates will be used for a month, then at month end, the dates will change. I would like to be able to set the default to the last entry.
Is this possible?
Rather than using unbound textboxes, consider creating a table containing a single record to store the values entered in each textbox, and then use the table fields as the Control Source for each textbox.
This way, when the user enters/changes the value in the textbox, the underlying table record is automatically modified and will be automatically reloaded when the form is reopened.
You can also set the Allow Additions & Allow Deletions properties both to No in the Form Data properties, so that the user cannot create/delete the record in the underlying table being used to save the textbox values. Make sure that Allow Edits is set to Yes to ensure that the user can change the values in the textboxes, and that the Recordset Type is Dynaset so that it can be edited.
Using this method should also not require any changes to the rest of your existing application, since the values held by the bound textboxes may be read in the same way as if they were unbound.
It's hard to know exactly what you're asking without seeing the code you have, but if you are using VBA, you could look into the difference between hiding the form and closing/unloading the form as described in this answer. If you hide the form, it won't be visible to the user, but it will still be loaded, so your text boxes should keep their values. When the user needs to interact with the form again, you can un-hide it.

What Event is fired when Access is leaving one record and moving to next?

I am developing a MS Access 2016 application with bound controls on Forms. Late in the process I decided I want a multi-select listbox.
From what I can see, you can't bind a multi-select listbox to a table. I'm okay with parsing the listbox when about to leave that record, and putting a comma separated list of values in a bound, non-visible text box. And then on arriving at the new record, setting the values in the listbox based on the values in that record's textbox.
The Current event lets me know when I arrive at a new record, but is there an event that lets me know I'm leaving a record? Before Update only works if the current record is changed, not just navigating to a new record. I suspect I'm missing something real basic.
If you want to parse a field and manually update it to a table, use the Before Insert and Before Update events. Don't do anything with navigation, because then you're likely too late.
Also, make sure to dirty the form when the value of the list box changes (Me.Dirty = True in the On Change event), so the update fires when navigating.
Or, like krish KM says, just make sure your text box changes every time your listbox changes.
That's more reliable than accounting for navigation, filtering, closing the form, manually saving, etc.

Updating a non-bound field in a form with a record source

I have a form with a record source of SELECT [AccountRunInProgress] from [AccountRunSummary]. The form contains a text box with a date which is used to apply a filter on the above record source, based on the month and year (defined as two separate fields in the AccountRunSummary table).
The other field in the form is a check box bound to the AccountRunInProgress field. When ticked, the OnClick event for this box updates a date field called RunProgressStartDate in the AccountRunSummary table with the current date and time.
I then get a Write Conflict error when closing the form - almost certainly because I am trying to update the table via the OnClick event while using the table as the record source at the same time, despite not binding the date fields. The date is updated in the table but not the boolean.
What is the best practice for working around this? I thought about adding RunProgressStartDate as a hidden bound text box in the form and adding it to the record source, but I'm fairly sure that will cause more problems rather than less.
You are on the right track.
You can't edit a record through the bound form and with an Update query (in VBA) at the same time.
IMO the best solution is indeed to add RunProgressStartDate to the record source and as hidden control to the form.
Then (important!) set the value of this control in the OnClick event, don't write the table with an Update query:
Me!RunProgressStartDate.Value = Date()
and both fields (checkbox and date) will be saved when the record is saved.

Access 2007 unbound control changed in one record impacts on all others

In recording Patient data, two of the elements required are Main Consultant and Other Consultant. A table called consultant_list stores ID, name, code and speciality. A combo allows the consultant name to be selected, with the control storing the ID for that consultant in a table as consultant_id. The selection automatically displays the consultant's code and speciality in the bound controls on the form, txtConsultantCode and txtSpeciality.
Problem comes with the other consultant. Additional bound controls for consultant code and speciality automatically complete with the data selected under consultant. So I used unbound controls txtOtherConsultantCode and txtOtherSpeciality and set the AfterUpdate event for cboOtherConsultant to do a DLookup() and fill in the values. Thought that worked fine until I discovered that although it fills in the values OK, the values entered for the current record are also filling in in any previous record on the form, leaving inconsistent data. Also if the other consultant name is cleared, the values in the unbound controls remain, so I wrote code on the LostFocus event to check if cboOtherConsultant is Null and if so to set the associated values to Null also, but of course that also impacts on the other records. Help!
You described what normally happens with unbound controls on a continuous form. Since they're unbound, the controls aren't tied to the current record. However, you can tie them to the current record by using your DLookup() expressions as the Control Source properties for those controls. With that approach, the controls would no longer be unbound.
However, I'm not convinced this is the best approach. Seems to me your form's record source could be a query which joins in consultant_list. Then you wouldn't need DLookUp() expressions to pull in the values for txtOtherConsultantCode and txtOtherSpeciality; they would already be present in the form's record source. And you could set the properties for those 2 text boxes to Enabled = No and Locked = Yes so they will be display-only ... the user would change them by making a different selection in cboOtherConsultant.

Trigger database update after control update on subform

I have an Access form with a subform that allows updating data in table X.
The subform contains a text box control named Y, linked to column Y in table X.
Now I thought that the table would be updated immediately after that text box control loses focus, but I noticed that this only happens later, most probably after the subform loses focus.
The point is that I need the table to be updated before (or during) calling the sub Y_AfterUpdate, because I wish to perform some standard subs/functions that rely on the table to contain the new data.
What is the best way of achieving this table update? I applied subForm.Requery and that worked, but there may be another, more intuitive method available. Note that Requery to me sounds more like "populate controls of the subForm, using the current table contents", and not like "update table and then populate the subForm again". Hence, intuitively, I would have expected that I should have used another method.
You want:
DoCmd.RunCommand acCmdSaveRecord