find a record based on value selected from a combo box

G

Guest

I have a song database. I have a form with a combo box to find a particular
song title. The code (based on the Access wizard) is as follows:

Private Sub cboSelectSong_AfterUpdate()
' Find the record that matches the control.
Dim rs As Object

Set rs = Me.Recordset.Clone
rs.FindFirst "[intSongID] = " & str(Nz(Me![cboSelectSong], 0))
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
End Sub

This works, but it produces an unsorted drop down list of song titles. My
question - is it possible to modify the code so the list is alphabetically
sorted?
(I tried creating a query and setting the Row Source to the field in the
query, but ended up with no values in my combo box)
 
J

Jeff Boyce

Peter

Have you tried setting the parameter of a query to the form's combo box
value? I.e., something like:

Forms!YourFormName!YourComboBoxName

Then, in your combo box, the AfterUpdate event would requery the form. This
assumes the form's source is based on the query.

Generically, what happens is:

* You open the form.
* Since its source is based on the query, based on the combo box, which
is empty
* No record shows in the form
* You select an item in the combo box
* The combo box's AfterUpdate event fires, triggering a requery of the
form
* The form's underlying query runs, using the (selected) combo box
item, and
* The form's source now points to the record/song selected.
 
J

John Spencer

If you mean you want the list in the combo box sorted.
-- Open the form in design view
-- Click on the combo box
-- on the properties sheet, click on the three dots button on the ROW SOURCE property
(If Access says - You invoked the Query builder on the table. Do you want to
create a query based on the table? Answer Yes)
-- when the query window opens, set the sorting on then title column
--Close and save

Leave the CONTROL source for the combobox blank.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top