Cannot refresh subform from other subform - ms-access

I have a form that has multiple subforms on it. When someone changes a value in a combobox of one of the subforms it needs to refresh another subform. I am using this code but it doesn't work:
Forms!frmDispatchPlanningSheet!subfrmExtraDriversForPlanning.Form.Requery
I have even tried to remove the Record Source and re-add it. However, that also did not work.
When I am saying it doesn't work for the direct requery, I don't get an error; it just doesn't requery.
For the remove and re-add the Record Source, it does remove and re-add the source; however, it still doesn't refresh the query.
however, when I click in the other form and then hit refresh all in the Ribbon, it will refresh (so I know the query works).
What am I doing wrong? how do I fix this?

If subfrmExtraDriversForPlanning refers to Driver via the table, you need to save the record first.
Add
Me.Dirty = False
before doing the .Requery.

Related

Disable Record Navigation SHIFT+TAB in Access Form

I have form that loads as Data Entry = Yes and the navigation buttons are disabled. I'm trying to figure out how to make it to where a user cannot press SHIFT+TAB and go back through previously entered records. You have to press tab at the very last field to move to a new record just like you would the table.
Should I just close and reopen the form after update? Or is there a different way?
Thanks in advance
Before I answer the question, are you sure preventing the user from going backwards is a good idea? Perhaps they tabbed forward to make a new record and then realized they made a mistake so they want to go back and correct it? Of course, this depends on the specific needs of your db and how it will be used.
The Cycle property in the Other tab defines how tabbing works. If you change this to Current Record, then the user cannot tab backwards or forwards to other records.
If the user needs to add multiple records at once, you can add a command button.
Edit
Regarding your idea on the submit button, here's how you can make that work. Leave the form's record source blank (so it isn't tied to the table). When the users click Submit, a msgbox will first pop up asking them to save and warning them they can't go back. If they choose Yes, a SQL query stored in the VBA will then append the form's values to the target table, then clear the form.
The only potential problem with this is if the user enters single or double quotes to any string fields as those can mess up your SQL query. A simple Replace() can fix that though.

Programmatically changing the RecordsetType in ms access or a better method

In ms access I have a form which also has a sub-form. I wanted to programmatically change the RecordsetType in the main-form to snapshot when the sub-table/sub-form has associated records in the one-to-many relationship. I wrote some vba code to do this but I was experiencing some very odd behaviour.
I then discovered that it was because when I change the RecordsetType, the form refreshes and navigates back to the first record. That then causes the On Current event and associated code to fire in both the sub-form and main-form twice. I was thinking of using DoCmd.SearchForRecord to navigate back to the original record but quickly realized that it wouldn't work because the the WHERE condition in the DoCmd.SearchForRecord gets overridden when the events fire the second time around. It all seemed so inefficient any way.
Are there any other methods to do what I am trying to do? I don't really want to set the controls in the form to 'Disabled'.
MS Access has a selection of form properties that should suit: AllowEdits, AllowDeletions and AllowAdditions. These can be set separately or together. In this case, it seems that you need AllowAddition set to Yes and AllowEdits set to No.

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

Handling #DELETED messages on records that have been deleted while a form is active

There may be a better way to do this, but currently, if the user enters i value that is not allowed (in this case, a project that has been closed), then it notifies them. However, because it's on a sub-form linked to the table, it automatically created a record anyway. This is how it's currently handled (if a bad input was entered):
Clear the fields of that record
Delete the record from the table
Requery / Refresh
Currently it works fine, except it leaves #DELETED in all the controls associated with the records. However, if I hit F5, it disappears. Is there a way to make it so the deleted record disappears (like how it does when F5 is hit) programmatically?
I think you have a couple options to handle this. The main thing is that you want to avoid deleting the record and try to handle it before it ever becomes a saved record. You should be able to do this using one of several different form events (not control events): Dirty, BeforeUpdate or BeforeInsert.
If you use the Dirty event, check to see if the project is closed and check to see if the record they are working on is a NewRecord. If both are true then try using Cancel = True and/or Me.Undo.
If you use the BeforeUpdate event instead, it works much the same way. You'll probably need to cancel the edits and undo the edits as I mentiond directly above, in which order I'm not sure.
I don't have a good way to test the BeforeInsert event at the moment but I think you can prevent new records from coming into existence inside this event by using Cancel = True.
If you cannot handle the record before it gets saved, and you must truly clear #DELETED from your form, you'll probably need to use a Me.Requery statement. Exactly how this will work depends on whether or not the form is bound and what it is bound to but it sounds like you are using a fairly standard Access form that is bound to a table or SQL statement so I believe the Requery statement should work.

Is there a way to override automatic record updating of Access 2007 forms created with Form Wizard?

I'm new to Access VBA, and have created a form using the Form Wizard that displays records in a table. That's a piece of cake.
The behavior that I get with the form, though, is that updates to the records occur automatically when I move around records.
What I'd like is to have the updates occur only when I click an "Update" button that I put in the form.
It seems like I can construct the form from scratch, update all of the (unbounded) controls programmatically, and then update the record from the controls programmatically, but this seems like too much work.
Is there a way to "turn off" the automatic updating behavior from within Access, or using VBA code?
Thanks!
As Robert suggested, you can avoid saving a changed record by canceling the before update event. The code would look something like this:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Me.Undo
Cancel = True
End Sub
However, that approach requires you discard any changes you've made to the record before you can move off it. Without Me.Undo, you can cancel the update, but Access won't allow you to move to a different record. You must either save or discard the changes to the current record before moving to another.
If you want to move to another record but not discard changes first, I think you need to try a disconnected recordset. It's an ADO recordset you create in memory, not bound to any data source. You can add a command button to save your changes on command. If it sounds useful, see this article by Danny Lesandrini at Database Journal: Create In-Memory ADO Recordsets
If users are accidentally changing data and moving record to record causing updates, maybe you should have an Edit button to only start editing when needed. You can use other suggested code to either undo the changes if they move to another record or prevent them from moving at all unless they save or cancel.
Is there a way to "turn off" the
automatic updating behavior from
within Access, or using VBA code?
You can try cancelling the Form.BeforeUpdate event, unless a flag is set. When the button is clicked, set the flag, set Form.Dirty to false (to save the data), and then clear the flag in the Form.BeforeUpdate event handler.
Why would you want to? Access works on a record by record basis. Not like Excel which only saves the spreadsheet when you choose to save or exit.
It sounds like you have a continuous form with multiple records on the screen. The only way to do this would be to use a "temporary" table and the save the contents of the "temporary" table to the permanent table once you're ready. However then you will lose the ability to determine if someone else has changed the records unless you do a lot more work.