Results 1 to 13 of 13
  1. #1
    stalk is offline Competent Performer
    Windows 10 Office 365
    Join Date
    May 2014
    Posts
    143

    How to open different forms based on the drop down selected..

    I have a FORM that has one drop down field. Based on the drop down selected ( Me.cmd_selector) I would like to open the specific form? stDocName in the comment is the form name I would like to open. What changes should I make for the code below..





    Private Sub cmd_form_Open_Click()


    On Error GoTo Err_cmd_form_Open_Click


    Dim stDocName As String
    Dim stLinkCriteria As String

    'stDocName = "ASSAY_Main_frm"

    'DoCmd.OpenForm stDocName, , , stLinkCriteria

    Me.cmd_selector = "GT Unit Info" ' stDocName = "GT_unit_Main_Frm"
    Me.cmd_selector ="GT Assay Info" 'stDocName = "GT_Assay_Details_Survey_Responses_frm"
    Me.cmd_selector ="PR unit Info" 'stDocName = "PR_Unit_Main"
    Me.cmd_selector ="PR Assay Info" 'stDocName = "PR_Assay_details_Main"


    Exit_cmd_form_Open_Click:
    Exit Sub


    Err_cmd_form_Open_Click:
    MsgBox Err.Description
    Resume Exit_cmd_form_Open_Click
    End Sub


    Thank you,

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,972
    Bring the formname in as another column in the combo, then use that value.
    Columns start at 0.
    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
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,809
    That code doesn't make much sense. When you click the button, you're just assigning 4 different values to a control, one after the other. Are those supposed to be tests (i.e. IF the value is this, do this."?

    Please post your code within code tags (see # on forum posting tool bar/menu bar)
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    stalk is offline Competent Performer
    Windows 10 Office 365
    Join Date
    May 2014
    Posts
    143
    Hello Micron, I updated my post with code tags..
    If user select "GT Unit Info" value from the drop down and clicks the open button at the bottom, then I want to open "GT_unit_Main_Frm" form


    Me.cmd_selector = "GT Unit Info" ' stDocName = "GT_unit_Main_Frm"

  5. #5
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Hi stalk!

    At first, set those properties for cmd_selector:
    Property Name Property Value
    Row Source "GT_unit_Main_Frm";"GT Unit Info";"GT_Assay_Details_Survey_Responses_frm";"GT Assay Info";"PR_Unit_Main";"PR unit Info"
    Row Source Type Value List
    Column Count 2
    Column Widths 0

    Then, you can write code like this:
    Code:
    Private Sub cmd_form_Open_Click()
        Dim stLinkCriteria As String
        
        With Me.cmd_selector
            If IsNull(.Value) Then
                Beep
            Else
                stLinkCriteria = "1=1" 'Set here the Link Criteria
                DoCmd.OpenForm .Value, , , stLinkCriteria
            End If
        End With
    End Sub
    Hope it helps.

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,972
    PMFJI, but what is the purpose of the stLinkCriteria ?
    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

  7. #7
    stalk is offline Competent Performer
    Windows 10 Office 365
    Join Date
    May 2014
    Posts
    143
    Thank you accesstos.
    After making the changes to the row source. when I ran the above code I get an error:
    Run-time error'2101': The form name " Name of the form" is misspelled. Even thought I gave the correct name of the form..

  8. #8
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,809
    strLinkCriteria is usually what Access creates when you convert a macro to code. When I posted, I figured what the combo should have had is a column for the values the user sees and one for the actually object name. Since it doesn't, I took the code (hours ago) and modified it to this
    Code:
    Private Sub cmd_form_Open_Click()
    
    On Error GoTo errHandler
    
    Select Case Me.cmd_selector
      Case "GT Unit Info"
        DoCmd.Openform "GT_unit_Main_Frm"
      Case "GT Assay Info"
        DoCmd.Openform "GT_Assay_Details_Survey_Responses_frm"
      Case "PR unit Info"
        DoCmd.Openform "PR_Unit_Main"
      Case "PR Assay Info"
        DoCmd.Openform "PR_Assay_details_Main"
    End Select
    
    exitHere:
    Exit Sub
    
    errHandler:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume exitHere
    End Sub
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Is the Bound Column of cmd_selector set to 1?

    Choose Debug at error message box and hover over .Value in highlighted line to see the form name that tries to open.

  10. #10
    stalk is offline Competent Performer
    Windows 10 Office 365
    Join Date
    May 2014
    Posts
    143
    Thank you so much for the quick solution. It works perfectly as desired

  11. #11
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,809
    I'm going to guess that comment about a solution was directed to me since the code I posted was quite close to yours. In that case, you're welcome and glad I could help.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  12. #12
    stalk is offline Competent Performer
    Windows 10 Office 365
    Join Date
    May 2014
    Posts
    143
    Yes,Micron. I thought I added the comments to you. Not sure how the comments work if multiple users helps. I know my question is the basic one for VBA coders but I am not VBA person. Thank you again.

  13. #13
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,809
    @stalk, glad to help. Good luck with your project.

    P.S. I just gave you a clue to one way to reference a prior poster in this forum.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 40
    Last Post: 04-06-2020, 06:49 AM
  2. open form based on button selected
    By enginerdUNH in forum Programming
    Replies: 6
    Last Post: 02-27-2018, 12:56 PM
  3. Replies: 1
    Last Post: 07-06-2016, 03:35 PM
  4. Replies: 5
    Last Post: 02-20-2015, 01:21 AM
  5. Open 1 of 2 Forms based on Criteria
    By DCV0204 in forum Forms
    Replies: 28
    Last Post: 11-23-2011, 03:09 PM

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