Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2011
    Posts
    56

    Unanswered: Setting Default Value on Subform (but subform changes)

    Access 2007

    I have a subform on my mainform that changes to the appropriate subform based on a combobox selection. On all the available subforms I have a field that needs to be set with a default value based on one of the mainform's field values. I planned to do it in the mainform's field afterupdate and use the code:

    Me.Subform.Form.Subformfield.DefaultValue = Mainformfield.Text

    But because the subform in question is based on a selection from the combobox, I don't how to tell Access that. I believe I will have to setup the code above for each subform available and then use "some code" that uses:

    Me.Subform1.Form.Subform1field.DefaultValue = Mainformfield.Text - if Subform1 is selected in combobox

    Me.Subform2.Form.Subform2field.DefaultValue = Mainformfield.Text - if Subform2 is selected in combobox

    etc.

    Just don't know what type of code that "some code" is.

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Whatever the name of the subform can be, the name of the control of type SubForm/SubReport that "shows" the subform in the main form does not change.

    From the Main form, you can then use:
    Code:
    Me.ControlSubForm.Form.SubFormControlName.DefaultValue = Me.ControlName.Value
    Where:
    ControlSubForm is the name of the control of type SubForm/SubReport (in the main form).
    SubFormControlName is the name of the control to which you want to assign a default value (in the subform).
    ControlName is the name of the control from which the default value is obtained (in the main form).
    Have a nice day!

  3. #3
    Join Date
    Aug 2011
    Posts
    56
    Seems my problem was using internal referencing instead of "universal" referencing. This is what I did to fix the problem.

    On the forms used to create the subforms, I put the following code into the form's afterupdate:
    Code:
    Private Sub Form_AfterUpdate()
    Forms![Barge_Crane]!Form![eqWMI].DefaultValue = Forms![frmeqWMI]!Form![eqWMI].Text
    End Sub
    It seemed to do the trick when I tried it.

  4. #4
    Join Date
    Aug 2011
    Posts
    56
    Quote Originally Posted by Sinndho View Post
    Whatever the name of the subform can be, the name of the control of type SubForm/SubReport that "shows" the subform in the main form does not change.

    From the Main form, you can then use:
    Code:
    Me.ControlSubForm.Form.SubFormControlName.DefaultValue = Me.ControlName.Value
    Where:
    ControlSubForm is the name of the control of type SubForm/SubReport (in the main form).
    SubFormControlName is the name of the control to which you want to assign a default value (in the subform).
    ControlName is the name of the control from which the default value is obtained (in the main form).
    I think the problem I was having with doing it that way was that the actual subform is an unbound "blank" subform until a choice is made in the combobox. Therefore the subform I was trying to reference was actually on the form or the field I was trying to default wasn't actually a part of the unbound subform "blank". I appologize for my lack of database language. Let me know if I am wrong in thinking that way.
    Last edited by sehenry; 11-23-11 at 13:37.

  5. #5
    Join Date
    Aug 2011
    Posts
    56
    Quote Originally Posted by sehenry View Post
    Seems my problem was using internal referencing instead of "universal" referencing. This is what I did to fix the problem.

    On the forms used to create the subforms, I put the following code into the form's afterupdate:
    Code:
    Private Sub Form_AfterUpdate()
    Forms![Barge_Crane]!Form![eqWMI].DefaultValue = Forms![frmeqWMI]!Form![eqWMI].Text
    End Sub
    It seemed to do the trick when I tried it.
    I am a bit confused because when the afterupdate even triggers I get:

    Run-time error '2450'
    Access can't find the form 'Barge Crane' referred to in a macro expression or Visual Basic code.

    But if I continue by ignoring the error and save and close the form. The default value was set to what it was suppose to be set to. Which leads me to believe it did find the forms?

    Turns out when the Form and subforms are linked by the fields that you are trying to default to one another, its already done for you.
    Last edited by sehenry; 11-23-11 at 14:36. Reason: Found answer

  6. #6
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    The reason of the error is because, when a form is open as a subform, it is not added to the Forms collection. Then, you cannot use this collection to reference it.
    Have a nice day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •