Results 1 to 12 of 12
  1. #1
    MrChips is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    20

    Using an option box to select which form to open in a sub-form

    I have a number of different forms that I would like to call into a sub-form.
    I would like to select which form to display using an option box on the main form.
    What would be the correct VBA code to use in the select case please?



    I thought it would be simple, but after an afternoon's trying, I have been forced to request help.
    There is plenty of advice on the net, but I couldn't get anything to work.

    Any advice that you can give me will be greatly appreciated.

    I am using Access 365 with windows 11
    Last edited by MrChips; 10-06-2022 at 10:51 AM. Reason: added a word forgot to put what access version

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,943
    I used a combo, that would hold the form name and a description, with form name hidden, then just opened what was selected. Same with reports.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Use an Option Group control instead of individual option boxes. Set the values of each option to the name of the form you want in your subform Then in the AfterUpdate event of the group simply have:
    Code:
    Me.sfrmYourSubform.SourceObject=Me.grpSelectForm
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  4. #4
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    If sticking with the option group, I'd probably make the option values the name of the subform to load. Then the AfterUpdate event of the frame would be
    Code:
    FormRefereceHere.SubformControlNameHere.SourceObject = frameReferenceHere
    That syntax is the best I can do without knowing the names of any of the objects involved.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    I understood that the OP has one subform control in which he wants to "load" various forms, there my suggestion to use the forms' names as the values.

  6. #6
    MrChips is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    20
    Welchgasman.
    Thank you for your reply.
    I currently have 10 buttons which each opens a form. When they have all been opened, it leaves the screen very untidy, which is why I would like them to be opened in one sub subform. I appreciate your reply. Thank You.

  7. #7
    MrChips is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    20
    I am sorry. Bit of a medical disaster here. Haven't been able to test your solutions. But will do, hopefully, shortly. Thank you all for your responses.
    Dave P

  8. #8
    MrChips is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    20
    Thanks Micron.
    When I tried to add the form names in the values field of the frame (option), it advised me the field was too long. (unless, I course, I should have put the form name in the label (column one of the option choices.!)
    Also, to be honest, I didn't quite understand which was FormReferenceHere.

  9. #9
    MrChips is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    20
    Gicu,
    Thanks.
    I have used select case to decided which piece of code to run, as in the attached screen dump.
    When I do so, I get the response as per the second screen dump.

    It is several years ago that I used Access, and with the aid of Google search, I have got a lot of it remembered, but this is eluding me, and I would like to clean up what I have at the moment.
    Many thanks for your time on this so far.
    Attached Thumbnails Attached Thumbnails VBA code tried.jpg   Forms.jpg  
    Last edited by MrChips; 10-07-2022 at 07:38 AM. Reason: forgot to add the screen dumps!!!

  10. #10
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    FormReferenceHere indicates to put a form reference there. Could be as simple as Me - all depends on your design.
    Select Case is over complicating this IMO but if you want to use it, put a break point at the Select line and make a choice on the form. The code should stop at your break point. Then F8 to step through code. You should be able to see what Frame2 value is. It won't be "Me.f_Info_subform_blank.SourceObject = Me.f_1_secs"

    Simpler:
    Private Sub Frame2_AfterUpdate()
    Me.f_Info_subform_blank.SourceObject = Me.Frame2 '<< where, as stated, option value is the name of the form to load
    End Sub
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,943
    So what cannot it not find?, that would normally be highlighted?
    What is f_c_scribes meant to be?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  12. #12
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    chips-davegri-v01.zip

    Like others have suggested - see attached barebones framework DB:

    Code:
    Option Compare Database
    Option Explicit
    
    
    Private Sub fmChoose_Click()
        Select Case fmChoose.Value
            Case 1
                Me.CTL_subfrm.SourceObject = "subfrm1"
            Case 2
                Me.CTL_subfrm.SourceObject = "subfrm2"
            Case 3
                Me.CTL_subfrm.SourceObject = "subfrm3"
            Case 4
                Me.CTL_subfrm.SourceObject = "subfrm4"
            Case 5
                Me.CTL_subfrm.SourceObject = "subfrm5"
        End Select
    End Sub
    Click image for larger version. 

Name:	subforms.png 
Views:	13 
Size:	20.0 KB 
ID:	48869
    Last edited by davegri; 10-08-2022 at 07:37 AM. Reason: added code display

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 05-08-2020, 08:57 PM
  2. Replies: 11
    Last Post: 07-05-2017, 09:00 AM
  3. Replies: 3
    Last Post: 01-20-2017, 02:25 PM
  4. Open form to set option
    By Voodeux2014 in forum Forms
    Replies: 5
    Last Post: 03-17-2015, 09:24 AM
  5. Replies: 1
    Last Post: 05-05-2011, 09:21 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums