Access 2007 need to Force a query update based on a field change in a form? - ms-access-2007

I have an access database with several calculated fields in text boxes on a form. Most of those update immediately after the data included in the calculation is updated. However I have a couple of those calculated field text boxes that include a data point from a Query, when I update a filed that would impact the query results, those calculated fields do not update correctly until I click through a few records or close and open the form.
How can I force that specific query to update after updating a specific filed on the form?

Try refreshing the form.
Me.Refresh
As an aside, you really should get out of the habit of using bound forms, because you have to end up kludging a bunch of stuff (like this) to get it to work properly, and the Undo functionality (cancelling the entry of a new record) is unstable at best. By unbinding a form, you can have more control over how it reacts to calculated fields and dumping a new data entry is a snap.

Related

Access 2013 Form Requery Issue

I have a form that has a parameter query for it's record source - the parameter query asks for an ID number, then the form displays that particular record.
Within the form are a number of buttons that perform various functions (change status etc...) and then requery the form.
My problem is that every requery obviously re-runs the parameter query and asks for the ID number - how can I make the requery use the current ID number and not ask for it?
The button functions are quite simple -
Me.SRStatus = 3
Me.Requery
Regards,
Garry.
Me.Requery will execute the query again, so it will ask for parameters always.
Consider using Me.Refresh instead of Me.Requery
It depends on what you need. You open your form filtered by ID, that's perfect. Then the user makes changes, and here comes the deal.
If those changes are just editing the record, then you don't need to use Me.Requery. Refreshin will update the record and show the changes without calling the query again.
But refreshing won't update if the record is deleted or if you add new records. To show new records/update deleted record/reorder records you are forced to Requery.
If that form is just for editing an existing record, then use Me.Requery so the existing data will be updated without executing the query again (and then asking for a parameter again).
You can read more about this
here
UPDATED ANSWER: OP said that Refresh works, but in event Form_Current got code that enables/disables the buttons according to a field value. So the right answer would be:
Me.Refresh
Call Form_Current

VBA access and forms

I created a database in Access and there are some type of records in some tables that requires a particular inserting, so I decided to use VBA to handle this.
The problem is that if i create a form with some controls which i want to refer and use their values as criteria for queries, the form is still a way to insert data. So the query works but the data i inserted are added directly from the form too, creating duplicates.
The question is, is there a way to create a form that has controls only for text input but does nothing to record , leaving inserting, deleting , updating all to queries in VBA?
I tried to put "no" on propriety "add records" in the form but it gets totally blank with no controls.
Your form must be unbound, i.e. its RecordSource must be empty.
Your form can be bound or not bound to a table / query.
This means that the controls on your form may be bound to a field of that table / query.
But you can also have controls in the same form which are not bound.
Example:
You can make a form in which the body has a list view of the records of the table. In this section the controls would be bound to a field.
In the header of the table instead you may have controls that are not bound and that could be used either to filter the records shown or to add new records. You may want to add records this way rather than let users insert data directly in order to add checks or do any other kind of processing before actually adding the data in a new record.

MSAccess: Change in subform recordsource causes loss of LinkChildFields binding

I have a problem that Im completely stumped by. Or rather, I have a work-around that is messy and I don't understand why what I want to do doesn't work:
I have a mainform / subform nest. The subform is linked by master/child fields (neither form is editable- so Im using the recordsource field directly rather than a separate control). The subform is a continuous form; so the subform displays the records related to the master in the mainform when the current record changes. The subform is based on a query- there are no parameters or filters being applied- the query presents the recordset exactly as needed. This all works perfectly (in that when the record of the mainform changes, so to do the related subform records).
Problem arises when I want the subform to be based on a different a different query (ie based on different tables)- but with the same unique-key (the 'ID') and fields as the original query. The queries all work fine, and displaying them in the form works fine.
The issue arises when I change the recordsource of the subform (so from QueryA to QueryB). The records the subform are correct (ie the query results are displayed as expected)- but the LinkChildFields is no longer functioning: the subform no longer shows the related records from the query- it displays them all. In other words, the form displays the entire recordset as if I had no LinkChild / LinkMaster set.
So, the code in the AfterUpdate event of the toggle control in the MainForm is ;
Private Sub optActRep_AfterUpdate()
Select Case Me.optActRep
Case 1
MainFormSubFormControl.Form.RecordSource = "QueryA"
Case 2
MainFormSubFormControl.Form.RecordSource = "QueryB"
End Select
Ive debugged and the property 'linkchldfields' is still set correctly ('ID'). Ive tried setting these to vbNullString before the recordsource update and resetting after the recordsource update (same issue). The odd thing is if I update the linkchildfield property in the mainform (in the Current Event)- then it works and seems to fire. So,
Debug.print Me!Subform.LinkChildFields
correctly returns 'ID'. The only way I can make it fire is by coding;
Me!Subform.LinkChildFields = "ID"
into the Current event of the Mainform (ie so that every time the MainForm record changes, the correct QueryB record is displayed)... even though Im only setting the property to what my debug is telling me it is already set to. Its as if it no longer fires.
Very confused.
Access continues to do the daftest things.
Nothing to do with the form setup at all. The underlying query (QueryB) was based on a Crosstab query. Whilst the query worked fine independently of the form (including when directly using the query as the forms record source), it obviously didnt like using the product of one of the fields as the Unique key (even though it was the same format type and Grouped / Row rather than being a cross-tab field).
I changed the crosstab part of the query to a Grouped query (using IIF & Sum to manually create the Crosstab-equivalent field values) and the form now works fine.
My lesson learnt- don't use Crosstab queries! Apologies for the red-herring.
Surferosa
Apologies for the late reply, I only found this thread when I encountered problems similar to yours. I'm not sure the Crosstab query is the cause of your problem.
I can set LinkMasterFields to be either a column name or a control name.
I can only set LinkChildFields to be a column name, setting it to a control name does NOT work. Ouch.
You can change LinkChildFields in the sub-form control either in Design View or via VBA, but in many cases Access continues to use the previous value even though it displays the new value. This is true for sub-forms whose record source is either a table or a simple two table query. I do not have the time or the patience to characterise this problem any further. Sorry !
My solution is to set LinkChildFields in the sub-form control to a value e.g 'ID' and then create an alias with that name in each and every record source used by my different sub-forms. Then you only need to change the Source Object when you switch sub-forms.
Hope this helps !
HB

Auto Inserting data into table row behind subform?

I've got a form that's got a subform and THAT subform/child has a subform/grandchild.
When I add a new client to the main form, the subform/child contains data like shipping address, etc etc. The subform/grandchild that one contains data like what we're shipping them.
When I make my initial new entry in the top form, there's data that seems to auto populate into the subform/child (and subsequently the table), however there are things that are always "default" items to ship (third subform/grandchild) that do not do that.
I've got some table constraints for the grandchild table like (ShipPackingSlip type bit) is set to 1, so that the checkbox should always be true. However, this does not occur on the 3rd layer of the form. Even setting the default option value to -1 will "autocheck" the box, but the data behind the forum does not reflect that.
Am I doing something wrong here?
Does that even make sense?
As I understand it (and I could be wrong), you can't use a subform in a subform on a main form without problems. It's better to organize your data and forms so that if you need to view more detailed information on data presented in a subform - it's better to call a new form from a button on the subform, passing a value to it so it pulls the correct record to view.
This would, of course, require you to store this subform of the subform data in a related table - then just relate it back to the main database with a unique identifier. This allows you a bit better means to organize your data, indexing isn't a pain, and you don't repeat information entry as much.

Form level event for when data is changed on a form

Well basically I am working on an MSAccess application and on the form where administrators will view and edit user data I want to be able to call an event with the old and new values of a field whenever it is changed. This function will add a record into the audit table to track changes.
I have no problem creating the query to add the entry to the audit table but I don't know where to place the function call. I've tried a few form level events so I don't have to go into each of the fifty fields and edit their onBlur events to check for a new value but have had no luck.
Any tips would be welcomed.
Microsoft has an article on this: How to create an audit trail of record changes in a form in Access 2000.
You can call your function in the After Insert,After Update and After Del Confirm events. These are kind of like triggers that fire after a record's been inserted, updated or deleted.
solution (1) would be to add an event handler in the "beforeUpdate" event. Remou's proposal is very interesting for that.
solution (2) would be to compare data in the recordset to data in the control in the afterUpdate event: for controls bound to fields in recordsets, there is allways a step where the value in the control differs from the one in the recordset. This is very easy when control sources are straight field names.
But I did not like the concept of auditing changes at the Form level: its results can be ambiguous as data changed at the form level might not be saved at the table level, either because form can be closed before the underlying recordset is updated, or because the SQL query is not sent to the server.
If your data update is made through SQL synatx, solution (3) would be then to store the "INSERT" or "UPDATE" strings sent from your Access app to your database server in a "transaction log".