Trigger database update after control update on subform - ms-access

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

Related

Textbox calculation appears only after navigating through other records in access

I have a form that is linked to a table in Access. I have an additional field which displays the sum of a few fields in the table. This field on the form is not connected to the table. I have the sum displayed on the form but what I noticed is that the sum does not appear until I move away and navigate to another record and come back to the original record. I don't see the addition as soon as I enter values in the respective fields.
Can someone help with this issue?
It sounds like you need to add some code to the After Update events of the controls for the fields used in the sum. That code can call the .Refresh method of the control that performs the calculation and update the total.
Edit
Another possibility is that there could be ambiguity between control values and field values if they have the same name. In Design View for a report if you drag a field from the "Field List" and drop it into a report then Access creates a report control with the same name as the field. This can confuse matters later because if any expressions refer to =[SomeColumn] it's not clear whether that refers to the field or the control. Often simply renaming the controls to something like txtSomeColumn can help if a report is acting strangely.

How to add new record to subform

I recently got help on When select value from combo, highlight that record in Access subform datasheet and now I'm trying to do a related task.
How can I make the subform give me a new row for data entry from a button on the main form?
Tried the method found here with no luck: http://www.access-programmers.co.uk/forums/showthread.php?t=26537
EDIT: Not sure if it matters, but the table my subform is supposed to update is a linked table to SQL Server.
Please first make sure your recordsource is updateable by opening the table or query in which your subform recordsource is based, and ensure that you can edit and add new records. If not, then it's possible that your SQL Server linked table is missing a unique indentifier. To create one, simply delete the linked table, and relink it. Upon relinking, MS Access should prompt you to specify a unique index (may take more than one field to make it truly unique). Then try again.
If the subform datasheet is editable and with AllowAdditions turned on, it should already give you the * button to add a new row.
If it doesn't, then you need to check the datasheet's properties and change them to match what you're actually seeking.
it has been a while since I used Access. Doesn't the RecordSource property of the subform give you the underlying table or query. If the source is editable, you should be able to add a record then Requery or Refresh the subform.
For those who need just to add a new record to sub form: just set focus from main form to subform. Then use DoCmd to add new record:
Forms![MainFormName]![SubFormName].SetFocus
DoCmd.GoToRecord , , acNewRec

Detecting data changes with Form events

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.

MS-Access unbound ComboBox in DataSheet?

In ms-access 2007, i'm trying to make a form for a table. this table has foreign keys from 2 parent tables. so i thought i would make these fields a lookup. but i couldn't create a single lookup for each parent table because they are composite keys.
I decided to create a query in which for each of these parent tables and the child table with an extra field for each composite key. this works fine with a normal form using an unbound ComboBox... but the unbound ComboBox does not work in a DataSheet Subform. when i make changes to a ComboBox in the Subform code, they are applied to all the other ComboBoxes in the same column as well.
My questions:
is there a way to change the values of the individual unbound ComboBox?
is there a different control i should be using other than the ComboBox or the DataSheet Subform?
what is the normal work around for this situation?
I cannot bind the ComboBox's because the field from the query is calculated/an-expression as I said.
I ran across a form of this problem myself, so for the sake of posterity:
While generally, the advice "Don't use continuous forms/datasheets in this situtation" is the best advice...It is possible to work around this.
However, access will not let you update the value of a single control on a datasheet. What can be used instead in this case is a temporary table, which can, when used as a recordsource, become the values of those controls. You will need to repopulate the table, and requery the controls (requerying the entire form should work as well) every time the calculation needs to change, however. Furthermore, should you enable editing on the controls, you will have to write some VBA on each control to handle the update event (Before Update), and run your own query to update the source tables, not merely the temporary table. Annoying to do perhaps, but effective.
There is another possibillity, which may also work, but I haven't tried to do something quite like this myself. The rowsource of a combobox can be very complex, so it is probable that you do not need to update the comboboxes with VBA at all. The rowsource can depend on other controls (such as another combobox) using the syntax Me.Form!controlName or Forms!FormName!ControlName, which will allow you to form the composite key. Of course, you can also select from queries with a rowsource. What is more interesting is that queries can reference controls on your form, provided the form is open, and you should safely be able to modify that with VBA should you have to.
Between the two of these, you should be able to force access, kicking and screaming, to display any data you wish, even on a datasheet, and to allow the user to change that data (but only if you want it to), and using the BeforeUpdate event, drag modified data back to whatever table it came from.
Continuous forms and datasheets do not work well for editing in situations where combo boxes need to be changed conditionally. The problem is that if you use the OnCurrent event to set the Rowsource of the combo box, it will be OK for that row, but will then hide the stored values for other rows.
The solution is to never use continuous forms/datasheets for editing data when this is the case (I hardly ever use them for editing, in fact). You can create two subforms, a continuous/datasheet subform that functions as a list, and a detail subform, that displays one record. Make the list subform uneditable, and the detail subform editable. You can link the two of them by using the Link Child/Link Master property of the detail subform control, and set it to the PK of the list subform.
If your list subform is Me!List and your detail is Me!Form, and the PK field is MyID, your link properties for the detail subform would be:
Master: Me!List.Form!MyID
Child: MyID
When you move to a different record in the list form, it will be automatically loaded in the child form. Any edits to the previously displayed detail will be saved before record departure.

In MS Access is there a way to allow forms to update while maintaning Read Only

I have several forms linked tables via queries. The form pull data such as sales and ratios by selecting a product from the main's form's combo box.
I am however having to issues:
1- I would ultimately prefer the combo box to be a free entry; however by just entering in the box and hitting enter (not a button called “enter on a screen” which would initiate recalcs, just normal enter), while it does bring the new information in sub-forms it also changes the information in the original table. If I make the table read only that it just doesn't allow the form to work by saying that the table is read only.
2- The same Read only issue occurs when another user with read only rights tries to use the database.
I understand that ready only is functioning as intended, however I am wondering if there is way to make some functions work while disallowing the updating.
I am unfortunately learning on the go, so go easy plz.
Thank you
You need to make your combo box no linked to the underlying data, but once the data is changed, set/reset the value of the combo box. This way, if some types in a new value
A) it will not update the data whether it is updatable or not.
B) you can still have the subforms update based on this value
Private Sub Form_Current()
cmbMyDisplayOnlyComboBox = Me!WhatEverFieldHasTheValue
End Sub
Again, cmbMyDisplayOnlyComboBox Control Source is blank. Substitute the field that 'use' to be the Control Source as WhatEverFieldHasTheValue