Results 1 to 12 of 12
  1. #1
    gemadan96 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    110

    Use combobox to select form to open

    I have a table the list available forms with three feilds (ID, Title, FormName)

    I have created a form with a combobox (Combo13) that lists the titles of the available forms.

    I have created a button that when clicked it should open the correct form.



    I've entered the following code, but it's not working.

    Dim strForm As String
    strForm = DLookup("FormName", "RegTallyForms", "Title = '" & Forms!RegTallyForms!Combo13 & "'")
    DoCmd.Close
    DoCmd.OpenForm strForm

    Any suggestions?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    What is the row source and bound column properties of the combo? If I had to guess, the bound column is the ID field, not the Title. I'd also use the column of the combo rather than the DLookup. This isn't exactly what you want, but shows how you refer to the column:

    http://www.baldyweb.com/Autofill.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    gemadan96 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    110
    Changed the bound column and that did the trick. I had tried it with the DIM datatype as integer and used the ID field for the criteria and got nothing but errors.

  4. #4
    gemadan96 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    110
    I'm trying to refine this a bit more to reduce the number of forms I have. I've added a field to the reports table called WCond to serve as the where clause for opening the form selected. I added a second DIM statement to declare strWCond and perform a dlook up. I've added the strWCond to the OpenForm command in the correct location, However, when I select a form to open I get an error

    Runtime error '2102':

    The form name 'EventClass = 'Creative Forms" is mispelled or refers to a form that doesn't esist.

    If I simply enter the where condition in the code as "EventClass = 'Creative Forms'" it will work correctly. Right now I have about 20 versions of the form to call from the combobox. I would rather not have to code each one using a bunch of If Then's. If I can get it to call up the where condition from the table that will be much easier if more forms are added in the future.

    Here is the latest code

    Dim strForm As String
    strForm = DLookup("FormName", "RegTallyForms", "Title = '" & Forms![Division Tallies by Registrations]!Combo13 & "'")
    Dim strWCond As String
    strForm = DLookup("WCond", "RegTallyForms", "Title = '" & Forms![Division Tallies by Registrations]!Combo13 & "'")
    DoCmd.Close
    DoCmd.OpenForm strForm, , , strWCond

  5. #5
    gemadan96 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    110
    I've played around with it some more. The code is as follows:

    Dim strForm As String
    strForm = DLookup("FormName", "RegTallyForms", "Title = '" & Forms![Division Tallies by Registrations]!Combo13 & "'")
    If Me.Combo13 = "All Forms, Weapons, & Self Defense" Then
    DoCmd.Close
    DoCmd.OpenForm strForm
    ElseIf Me.Combo13 = " Please Select..." Then
    DoCmd.Close
    DoCmd.OpenForm PleaseSelect
    Else:
    DoCmd.Close
    DoCmd.OpenForm strForm, , , "[FWSDRegTally].[EventClass] = '" & strForm & "'"
    End If

    Using this I no longer receive an error, however if I select something other than "All Forms, Weapons, & Self Defense" or "Please select..." it opens the form, but it is filtered so that nothing is displayed. If I select the EventClass column in the datasheet it shows me all the available text filters, but they are unchecked. The one matching strForm should be checked.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Well, the Else clause opens it to a record matching the name of a form. Is that appropriate? I wouldn't think so.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    gemadan96 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    110
    That's not appropriate. What I should see a list of all the records matching that event clause.

    If I enter the last DoCmd.OpenForm statement as follows:

    DoCmd.OpenForm strForm, , , "[FWSDRegTally].[EventClass] = 'Creative Forms'"

    I will see the entire list of records under Creative Forms.

    But when entered as follows:

    DoCmd.OpenForm strForm, , , "[FWSDRegTally].[EventClass] = '" & strForm & "'"

    I get an empty list. There has to something in the where condition that's throwing it off.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    But strForm is the name of a form, is it not? Populated here:

    strForm = DLookup("FormName", "RegTallyForms", "Title = '" & Forms![Division Tallies by Registrations]!Combo13 & "'")

    I would guess you want something else in the wherecondition.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    gemadan96 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    110
    My oversight there. I've added the following:

    Dim strWCond As String
    strForm = DLookup("WCond", "FWSDRegTallyForms", "Title = '" & Forms![Division Tallies by Registrations]!Combo13 & "'")

    And changed the DoCmd.OpenForm to:

    DoCmd.OpenForm strForm, , , "[FWSDRegTally].[EventClass] = '" & strWCond & "'"

    That now produces the Runtime error 2102 I mentioned earlier.

  10. #10
    gemadan96 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    110
    Got it working. Found one more mistake. I had strForm in there twice instead of the second one being strWCond. Fixed and voila!

  11. #11
    gemadan96 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    110
    Here's the final code:

    Dim strForm, strWCond As String
    strForm = DLookup("FormName", "FWSDRegTallyForms", "Title = '" & Forms![Division Tallies by Registrations]!Combo13 & "'")
    If Me.Combo13 = "All Forms, Weapons, & Self Defense" Then
    DoCmd.Close
    DoCmd.OpenForm strForm
    ElseIf Me.Combo13 = "All Forms, Weapons Self Defense with >12 Competitors" Then
    DoCmd.Close
    DoCmd.OpenForm strForm, , , "[FWSDRegTally].[CountOfDivision] > " & 12
    Else:
    strWCond = DLookup("WCond", "FWSDRegTallyForms", "Title = '" & Forms![Division Tallies by Registrations]!Combo13 & "'")
    DoCmd.Close
    DoCmd.OpenForm strForm, , , "[FWSDRegTally].[EventClass] = '" & strWCond & "'"
    End If

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Glad you got it sorted out. I was stuck in a meeting.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Form ComboBox Filter - Select ALL records
    By jhrBanker in forum Forms
    Replies: 2
    Last Post: 08-02-2012, 08:57 AM
  2. Replies: 1
    Last Post: 05-31-2012, 01:01 PM
  3. open form based on combobox
    By bigmac in forum Forms
    Replies: 3
    Last Post: 04-09-2012, 11:25 AM
  4. Replies: 1
    Last Post: 11-26-2010, 05:22 AM
  5. Select / DoCmd to open another form
    By chrisjack001 in forum Access
    Replies: 1
    Last Post: 08-24-2010, 12:31 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