?
Solved

Populate combo box from code

Posted on 2014-01-08
3
Medium Priority
?
602 Views
Last Modified: 2014-01-08
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
0
Comment
Question by:boukaka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 2000 total points
ID: 39765372
You don't need the requery at the end... setting the rowsource is adequate.

Try adding this before the End Sub:

Me.CourseID =Me.CourseID.Itemdata(0)

Open in new window

0
 
LVL 38

Expert Comment

by:PatHartman
ID: 39765412
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.
0
 

Author Closing Comment

by:boukaka
ID: 39765478
That's the ticket! Thanks :)
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question