Share via

Open Form with Filter based on Combobox and Option Buttons

Anonymous
2013-09-12T16:05:55+00:00

Hello,

I have tried researching this but haven't had any luck understanding the results I have found.

I have a form ("LMS_MainMenu")  where users have a drop down box ("CourseCatalogCombo") and individual option buttons ("OptOpen" , "OptActive" , "OptClosed" , "OptComplete") in order to open a filtered form.  I originally had the following code to open the form "LMS_CourseCatalog" which worked perfectly.

If CourseCatalogCombo.Value = "<ALL>" Then

    DoCmd.OpenForm "LMS_CourseCatalog", acNormal

Else

    DoCmd.OpenForm "LMS_CourseCatalog", acNormal, "", "[Type]=" & "'" & CourseCatalogCombo & "'", , acNormal

End If

However, I was asked to provide more options based on the status of the course (Open, Active, Closed, or Complete).  They want to be able to choose any combination of the 4 options along with the combo box which is why I did not go with an Option Group.  I started to write the following code but it errors after the second If statement.  I also tried researching Case Statements but I didn't understand that either.  Please help.

Private Sub Search_Click()

If CourseCatalogCombo.Value = "<ALL>" Then

If OptOpen.Value = True And OptActive.Value = True And OptClosed.Value = True And OptComplete.Value = True Then

DoCmd.OpenForm "LMS_CourseCatalog", acNormal, "", "[Status]='Open' Or [Status]='Active' Or [Status]='Closed' Or [Status]='Complete'", , acNormal

Else

End If

If OptOpen.Value = True And OptActive.Value = True And OptClosed.Value = True And OptComplete.Value = False Then

DoCmd.OpenForm "LMS_CourseCatalog", acNormal, "", "[Status]='Open' Or [Status]='Active' Or [Status]='Closed'", , acNormal

Else

End If

If OptOpen.Value = True And OptActive.Value = True And OptClosed.Value = False And OptComplete.Value = False Then

DoCmd.OpenForm "LMS_CourseCatalog", acNormal, "", "[Status]='Open' Or [Status]='Active'", , acNormal

Else

End If

If OptOpen.Value = True And OptActive.Value = False And OptClosed.Value = False And OptComplete.Value = False Then

DoCmd.OpenForm "LMS_CourseCatalog", acNormal, "", "[Status]='Open'", , acNormal

Else

End If

Else

End If

DoCmd.Close acForm, "LMS_MainMenu"

End Sub

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2013-09-13T09:30:42+00:00

The approach you are attempting of calling the OpenForm method conditionally in different lines, with arguments determined by the value in the control is not one of the options I outlined.   In all of the solutions I described the OpenForm method is called in one line only.

Of the alternative solutions the one I would use is option 4 as this requires the button's code to simply open the form, as the restriction is done entirely by the second form's query.  This is relatively easy to build as the logic is quite straightforward.  The only difference would be that you would test for <ALL> rather than Null in the case of the combo box.  In the case of the option buttons be sure that these are all set to False by default to avoid having to cater for Nulls.

I'm assuming that you want to restrict the results to the selected value of Type or All types, further restricted by one or more values of Status as selected by the option buttons, for which the query for the second form's RecordSource would be like this:

SELECT *

FROM YourTable

WHERE ([Type] = Forms!LMS_MainMenu!CourseCatalogCombo

OR Forms!LMS_MainMenu!CourseCatalogCombo = "<All>")

AND (([Status] = "Open" AND  Forms!LMS_MainMenu!OptOpen = TRUE)

OR ([Status] = "Active" AND  Forms!LMS_MainMenu!OptActive = TRUE)

OR ([Status] = "Closed" AND  Forms!LMS_MainMenu!OptClosed = TRUE)

OR ([Status] = "Complete" AND  Forms!LMS_MainMenu!OptComplete = TRUE));

The query might draw upon more than one table of course and might include an ORDER BY clause to set the order in which the records are returned in the form.

Note the position of the parentheses in the above.  These are important to force the correct order of evaluation of the OR and AND operations.  Create the query, or at least add its WHERE clause, in SQL view not design view, and save it in SQL view without switching back to design view.  If you do, Access will move things around and at best the logic will be obscured, at worst the query might be too complex to open.

In the button's code you simply open the form with:

    DoCmd.OpenForm "LMS_CourseCatalog"

Do not close the "LMS_MainMenu" form in this procedure as it needs to be exposed to the second form's query, though you can hide it, in which case the code would be:

    DoCmd.OpenForm "LMS_CourseCatalog"

    Me.Visible = False

You can close it in the "LMS_CourseCatalog" form's Close event procedure with:

    DoCmd.Close acForm, "LMS_MainMenu"

One supplementary comment I would make, with regard to the use of option buttons, is that this ties you in to a set of four options.  Should at any time you introduce another Status option into the database, or delete or rename an existing one, then you'd have to amend the form design.  You might have noticed that my MultiSelect demo allows for multiple selections by means of a multi-select list box (hence the name of the file).  As this is 'data driven' any addition of deletion of an option from the referenced table in question, e.g. a Statuses table in your case, would automatically be reflected in the list box, so no amendment to the form's design is required.

Was this answer helpful?

0 comments No comments

11 additional answers

Sort by: Most helpful
  1. Anonymous
    2013-09-13T15:33:38+00:00

    Oh, I do have one last question...

    I was able to use your code, it works perfectly AND I understand the logic....which is the most important part. My only issue is going back to the Main Menu screen to perform another search because I have to keep the Main Menu form open.

    Once the user opens "LMS_CourseCatalog", they have the option to enroll in the course by going to another form or go back to "LMS_MainMenu" and searching again.  Which requires the "LMS_MainMenu" form to close in order to perform another search.  If the form does not close it keeps the initial criteria and I occassionally get the error message "You tried to lock table " while opening it, but the table cannot be locked because it is currently in use.  Wait a moment, and then try the operation again."

    If I put the close command for "LMS_MainMenu" in the On Close of "LMS_CourseCatalog", I am not able to open "LMS_MainMenu" from "LMS_CourseCatalog" anymore.  Is there a way to go around that?

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2013-09-13T14:49:24+00:00

    Thank you so much!!! This is exactly the explanation I needed.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2013-09-12T20:43:41+00:00

    Thank you Ken.  It has taken me quite a few hours to look over this and respond without sounding stupid but I have come to the conclusion it is inevitable.

    I was not able to get your link opened due to our security restrictions.  The example would probably help tremendously since I am a very visual person when it comes to learning code. 

    I don't know how to get started.  I am fairly new to VBA.  I keep trying to write out the code and I get stuck because of the multiple option the user can choose.  I have no problem when they only have the option of one or the other. 

    I could be completely off base but are you suggesting I do something like the following for option 1...

    Dim strOption as String

    strOption = " "

    If CourseCatalogCombo.Value = "<ALL>" Then

                DoCmd.OpenForm "LMS_CourseCatalog", acNormal, "", "strOption", , acNormal

    Else

                DoCmd.OpenForm "LMS_CourseCatalog", acNormal, "", "[Type]=" & "'" & CourseCatalogCombo & "'" & "strOption", , acNormal

    If that is the case, I am confused about how I write the string using my fields.  Do my Option Button names need to be the same as the Field I want to filter on?

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2013-09-12T16:35:14+00:00

    There are a number of ways you can do this:

    1.  Build a single string expression for use as the WhereCondition argument of the OpenForm method in which you test each control like this:

    "(NumericField = " & NumericControlName & " Or " & NumericControlName & " Is Null) " & _

        " And (TextField = """ & TextControlName & """ Or " & TextControlName & " Is Null) " & _

        " And (DateField = #" & Format(DateControlName,"yyyy-mm-dd") & _

        "# Or " & DateControlName & " Is Null) "

    Each parenthesized expression will evaluate to True for those rows where the value in the column equals the value in the control, or the control is Null, allowing controls to be left empty.

    2.  Examine each control, and progressively build an SQL statement as a string expression, including a condition in the expression only if the control is not Null.  Pass the string Expression to the form to be opened as its OpenArgs property, and in the second form's Open event procedure set the form's RecordSource property to the string expression.

    3.   Examine each control, and progressively build a string expression, including a condition in the expression only if the control is not Null.  Use the string expression as the WhereCondition argument of the OpenForm method.

    4.  Base the second form on a query which includes parameters referencing the controls on the first form, using the same logic as in 1 above, but in this case testing in each case for (Field = <parameter> OR <parameter> IS NULL).  One thing to note when doing this is that the parameters should not be declared, with the exception of parameters of date/time data type.  The referenced form must remain open of course, but can be hidden and subsequently closed in the second form's Close event procedure.

    You'll find examples of methods 2 and 4 in MultiSelect.zip in my public databases folder at:

    https://skydrive.live.com/?cid=44CC60D7FEA42912&id=44CC60D7FEA42912!169

    If you have difficulty opening the link copy its text (NB, not the link location) and paste it into your browser's address bar.

    This little demo file opens a report, but opening a form is exactly the same.  It includes two options for restricting the report on one or more fields using the two methods described above.

    Edited:  Method 1 string expression amended.

    Was this answer helpful?

    0 comments No comments