I posted this in reply to a message in the general public.access newsgroup.
It's virtually the same question as yours. Someone wanted help with a
survey application where users could move items in a list box up or down to
rank them by preference.
=====================
Here's one way to do it. I'm sure there are other ways, some of which will
be easier and more efficient.
Start with a table to hold your option and its rank. Rank should be single
or decimal data type (you'll need fractions).
Put a list box on your form bound to the table. Your rowsource would be
something like "SELECT Option from tblOptions ORDER BY Rank".
Put two command buttons next to the list box, one to move an option up, the
other to move it down. When the user clicks the button, open a recordset,
move to the selected item in the listbox, and increase or decrease it's
rank. In the code below, I am decreasing the rank by 1.5. This would
effectively move the option upward in preference. You have to
increase/decrease the rank by enough to make the option lower/higher than
the next value.
Dim db as database
Dim rs as Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT * FROM MyTable", dbOpenDynaset)
rs.FindFirst "Option = '" & Me!MyListBox & "'" 'I'm assuming option
is text
rs.Edit
rs!Rank = rs!Rank - 1.5
rs.Update
rs.Close
The next step will be to resequence your options so they are again ranked 1
to 5. This will allow the user to change the rankings as many times as
he/she needs to.
Dim x as integer
Set rs = db.OpenRecordset("SELECT * FROM MyTable ORDER BY Rank",
dbOpenDynaset)
Do While Not rs.EOF
x = x + 1
rs.Edit
rs!Rank = x
rs.Update
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
Set db = Nothing
Finally, requery the listbox to redisplay the options in the correct order.
Hope this helps.
Jane said:
Hi all,
I am trying to create a survey with access, and I have a question in it
which states " Put these 5 options in order of importance" then I have 5
options, but how can I do this in access?
Any help would be greatly appreciated
Thanks
=============