Avatar of boukaka
boukaka
Flag for Canada asked on

Populate combo box from code

I am attempting to populate a combo box based on values chosen in two other combo boxes. When I run the generated SQL from the code I get correct values. The combo box remains blank however, if I manually use the drop down arrow, the value is there and I can chose it but I need it to set the combobox value automatically. Requery doesn't seem to do it...

How can I modify it so that it doesn't just generate the value but also sets it as the combo box choice?

Private Sub Course_Name_AfterUpdate()
    Dim strSQL As String
    With Me![CourseID]
        If IsNull(Me![Course Name]) Then
            Me.[CourseID].RowSource = ""
        Else
            strSQL = "SELECT tblCourses.[CourseID] FROM tblCourses WHERE tblCourses.[Course Name] = '" & Me![Course Name] & "'" & " AND tblCourses.category = '" & Me![Category] & "'"
            Me.[CourseID].RowSource = strSQL
            Me.CourseID.Requery
         End If
    End With
End Sub
Microsoft Access

Avatar of undefined
Last Comment
boukaka

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
mbizup

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
PatHartman

Setting the RowSource doesn't populate the control.  Setting the RowSource will make a value "visible" if before the change, the value wasn't included in the RowSource.

For example, you have chosen Bridgeport as a city from a combo that is filtered by state.  If the state is NY, the combo will look empty since Bridgeport isn't in the RowSource but if you change the state to CT, then Bridgeport becomes visible.  Is this what you are talking about?

Combos have a bound ControlSource which specifies which column of the form's RecordSource the data is saved to.  They also have a bound RowSource which is a list of valid values.  The ControlSource may contain a value that is not in the RowSource and if it does, it will appear blank even though it isn't.
boukaka

ASKER
That's the ticket! Thanks :)
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy