MS ACCESS: Hide Specific Values from Combo box

Dale James
Dale James used Ask the Experts™
on
Hello Team

Can you please advise how to hide specific values from a combobox list?

For example:  I have a main form which is used to either display previously created records or can be used to create new records.  When the form is loaded to display a history record, the combobox cboErrorCat displays, Error1, Error2, Error3, Error4 when selected. What I require is for the cboErrorCat to display only Error1 and Error2 when the form is loaded to create a new record.

Main Form is called frmPaymentError, combobox is named, cboErrorCat and the row source for the cboErrorCat combobox is called qryErrorCategories

Query SQL
SELECT tblErrorCategories.ErrorCategoryID AS Expr1, tblErrorCategories.ErrorCategory AS Expr2
FROM tblErrorCategories
WHERE (((tblErrorCategories.DateExpired)>Date()))
ORDER BY tblErrorCategories.Order;

Open in new window



Any assistance would be much appreciated.

Thanks in advance.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jim Dettman (EE MVE)President / Owner
Most Valuable Expert 2017
Most Valuable Expert 2012

Commented:
There are two basic ways to achieve this:

1. Load the combo with the values you want, either with a call back function or constructing a value list.

2. Adding criteria to the query it's based on or switching the row source to a different query, and then requerying.

 Typically one does #2, but you didn't say what conditions make the various entries possible, so #2 might not be doable.

 Explain a little more how the values are chosen under what conditions.

Jim.
Dale JamesTherapist

Author

Commented:
Hello Jim

Thanks for getting back to me.

The values for the combo list are obtained from a rowsource value list via the query qryErrorCategories.  This query consists of all the values that are seen when viewing historical records but when creating a new record only Error1 and Error2 list values (items) are to be viewable.

There is currently no filtering or criteria set in the query to limit the view of values.

Please let me know if you need more detail as am not sure if the above is addressing your question.

Ta

Dale
President / Owner
Most Valuable Expert 2017
Most Valuable Expert 2012
Commented:
Ah. now I got it.

I would construct another query that restricts the Error's to 1 and 2.   Then either in the GotFocus of the combo or the OnCurrent event, just do:

 If Me.NewRecord = True then
  Me.cboErrorCat.RowSource = "qryErrorCategories_Limited"
 Else
  Me.cboErrorCat.RowSource = "qryErrorCategories"
 End If
 
 Me.cboErrorCat.Requery

Jim.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
You could set the count of records to be retrieved from your query:

Private Sub Form_Current()

    Const Sql       As String = "Select Top {0} * From qryErrorCategories"
    
    Dim RowSource   As String
    
    If Me.NewRecord Then
        ' Select two rows
        RowSource = Replace(Sql, "{0}", 2)
    Else
        RowSource = Replace(Sql, "{0}", 4)
    End If
    
    ' Only requery the combobox if needed.
    If Me!cboTest.RowSource <> RowSource Then
        Me!cboTest.RowSource = RowSource
    End If

End Sub

Open in new window

The combobox will update automatically when the rowsource is set.

Given the output, I'm not sure why your query is sorted by "Order". It should probably read:

SELECT tblErrorCategories.ErrorCategoryID AS Expr1, tblErrorCategories.ErrorCategory AS Expr2
FROM tblErrorCategories
WHERE (((tblErrorCategories.DateExpired)>Date()))
ORDER BY tblErrorCategories.ErrorCategory ;

Open in new window

Dale JamesTherapist

Author

Commented:
Thanks Jim and Gustav for your assistance.

Just working through the detail at the moment.  

Will feedback soon.

Ta

Dale
Dale JamesTherapist
Commented:
Hello Jim and Gustav...

Just want to say thank you for your assistance regarding the this particular topic. Both responses provided the answer.

Thanks again.

Dale
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
You are welcome!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial