views:

276

answers:

1

I would like to set a field to a value selected from a grid in a dialog. I am using Access 2007.

In WinForms I would:

  • Create a child form
    • create a grid for the data
    • add a property for the selected item
  • In the parent form
    • add a button to open the form
    • on successful dialog result get the selected item from the property
    • update the object
    • persist on event
  • On parent edit
    • set selected value in child grid

Is something like this possible in Access 2007 forms? I have a Multiple Item form with the child records. Can I select one and return that to the parent? And on the other side, can I default the selected item on edit?

How do people approach this in Access?

+1  A: 

Here is a pattern that works assuming the child form is modal.

In your parent form

Private Sub cmdOpenChild_Click()
    DoCmd.OpenForm "ChildDialog", acNormal, , , , acDialog, "Info for child"

    'This line will block further code execution until child form is hidden or closed.  
    MsgBox Forms.Item("ChildDialog").Controls.Item("SomePropertyOrControl").Value

    DoCmd.Close acForm, "ChildDialog"
end sub

In the Child form have a close button that actually only hides the form.

Private Sub cmdClose_Click()
    'hide the form instead of closing it to return control to caller.
    Me.Visible = False
End sub
JohnFx
Thanks for this
blu
The "child form" doesn't have to be modal to pause the code if it has been opened with the acDialog parameter. In general, the modal property of Access forms is not usable, as it doesn't behave reliably (i.e., it doesn't force the form to be modal in all situations).
David-W-Fenton
Curious, how can the focus be taken away from a modal form? Even going to the Task Pane keeps the focus on the form (eg, cursor on field with focus keeps flashing.).
Jeff O
The definition of a modal form is one that can't lose focus (within a given context, like the app)
JohnFx
@David: I think its just a terminology thing. According to MSDN "You can use the Dialog setting of the Window Mode action argument of the OpenForm action to open a form with its Modal and PopUp properties set to Yes."
JohnFx