Make record control buttons on main form affect subform

gddrew

Registered User.
Local time
Today, 02:49
Joined
Mar 23, 2005
Messages
38
Most of the forms in my database are either unbound, or bound to a table or query. On all of these forms I use button controls to save a record, cancel, close the form, or add a new entry.

In one form, I have a main form and a subform (see illustration). The main form contains read-only information, while the subform contains assignment details, which can be updated. New assignments can also be added.

To be consistent with the other forms in the database, I want the user to be able to click on a button to save the changes before they are committed to the database, or to cancel the operation, etc. However, the problem I'm running into is the buttons are on the main form, but I need them to control, in most cases, the action on the subform. I have them on the main form for aesthetic reasons and to be consistent with the other forms in the database.

As an example, this is what my Save code looks like on a normal form:

Code:
Private Sub cmdSave_Click()
    'Move the focus to the Hidden field
    Me.txtHidden.SetFocus

    Select Case MsgBox("Do you want to save your changes to the current record?" & vbCrLf & vbLf & "  Yes:         Saves Changes" & vbCrLf & "  No:          Does NOT Save Changes" & vbCrLf & "  Cancel:    Reset (Undo) Changes" & vbCrLf, vbYesNoCancel + vbQuestion, "Save Current Record?")
        Case vbYes: 'Save the changes
            Me.txtProperSave.Value = "Yes"
            Forms!Assignment_subform!DoCmd.RunCommand acCmdSaveRecord

        Case vbNo: 'Do not save or undo
            'Do nothing

        Case vbCancel: 'Undo the changes
            DoCmd.RunCommand acCmdUndo
            Me.txtProperSave.Value = "No"

        Case Else: 'Default case to trap any errors
            'Do nothing

    End Select
    
    Label78.Visible = False

Exit_cmdSave_Click:
    Exit Sub

Err_cmdSave_Click:
    MsgBox Err.Description
    Resume Exit_cmdSave_Click
End Sub

In the above code, txtProperSave and Label78 are on the main form, but the records being affected are in the subform.

Any insight and help would be appreciated.
 

Attachments

  • FormSubform.gif
    FormSubform.gif
    62.9 KB · Views: 266
Last edited:
put the buttons in the subform and move your label and proper save with it
 
What problem are u having? it is not saving correctly?
 
One thing to note. If you have the save button for the subform on the main form, it isn't really going to work all that well, as the minute you click on the save button, it transfers focus to the main form and the subform is saved, regardless of the button. In fact you could click anywhere on the main form and the subform will have been saved, unless you are using an unbound subform.
 
Hi

I am having a similar problem except that I want to save/undo/cancel records both on the main form and a subform. Obviously I only want my users to have to press one button (per function). So how do I effect that please?... So far I've tried the syntax :-

"Forms!Assignment_subform!DoCmd.RunCommand acCmdUndo"

in the click event of my main form button but it is invalid syntax/reference. I have also tried having a non-visible save button on the subform but there does not seem to be a method of clicking it programmatically? There is the Cancel = True option but that's only really any good if you are closing or navigating to a new record.

What I'm really trying to do is implement an undo button for both main and subform (last) changes whilst leaving the user on the current record.

Thanks in anticipation...
 
This won't work:

"Forms!Assignment_subform!DoCmd.RunCommand acCmdUndo"

This should:

Me![Assignment_subform].Form.DoCmd.RunCommand acCmdUndo

In general, you use a bang (!) when addressing an object in a collection of objects. You use a dot (.) when addressing the events, methods, and properties of that object. In our example, "Me" is the container with the main form and subform (two separate objects), so you use a bang to tell it which object you want. You then have to tell it what sort of object it is -- the ".Form" -- so that it knows what events, methods, and properties are available. Then you program as normal.
 
Hi Moniker

Thanks for your prompt help... however I've tried the syntax you suggest plus also:-

Me.Work_Items_SubForm.Form.DoCmd.RunCommand acCmdUndo

and I still get "Application-defined or Object-defined error" code 2465.

Any idea why this might be?
 

Users who are viewing this thread

Back
Top Bottom