I am looking to make a form that will load data from a table into a list box.
Once the data is in the list box the user should be able to move a selected item up or down using to command buttons.
The result should then be written back to the table.
So far I have this:
Table:
'tblDataSortering' with the fields 'uid' (autonumbering), 'sort' (number) and 'data' (text)
Form:
'frmDataSortering' with a list box 'lstSorter'
The list box contains two columns 'sort' and 'data' into which the content of the table is loaded. The data is sorted using 'sort' ASC.
In addition I have two command buttons 'cmdMoveUp' and 'cmdMoveDown'.
The code that I have estalished so far is as follows:
Basically it works fine, but once I have moved one item around, I cannot move another item to the post in which previously moved items are placed.
Also I still need to write the end result back to the table.
I hope that you guys can help my take this one or two steps further
/Soren
Once the data is in the list box the user should be able to move a selected item up or down using to command buttons.
The result should then be written back to the table.
So far I have this:
Table:
'tblDataSortering' with the fields 'uid' (autonumbering), 'sort' (number) and 'data' (text)
Form:
'frmDataSortering' with a list box 'lstSorter'
The list box contains two columns 'sort' and 'data' into which the content of the table is loaded. The data is sorted using 'sort' ASC.
In addition I have two command buttons 'cmdMoveUp' and 'cmdMoveDown'.
The code that I have estalished so far is as follows:
Code:
Private Sub cmdMoveDown_Click()
Dim db As DAO.Database, rst As DAO.Recordset
Dim lngTemp As Long
Set db = CurrentDb
Set rst = db.OpenRecordset("tblDataSortering", dbOpenDynaset)
lngTemp = Me.lstSorter.Column(1) 'the column with the number field
With rst
.FindFirst "sort=" & lngTemp
.Edit
!sort = Me.lstSorter.Column(1, Me.lstSorter.ListIndex + 1)
.Update
.FindFirst "sort=" & Me.lstSorter.Column(1, Me.lstSorter.ListIndex + 1)
.Edit
!sort = lngTemp
.Update
End With
rst.Close
Set rst = Nothing
Set db = Nothing
Me.lstSorter.Requery
End Sub
Private Sub cmdMoveUp_Click()
Dim db As DAO.Database, rst As DAO.Recordset
Dim lngTemp As Long
Set db = CurrentDb
Set rst = db.OpenRecordset("tblDataSortering", dbOpenDynaset)
lngTemp = Me.lstSorter.Column(1) 'the column with the number field
With rst
.FindFirst "sort=" & lngTemp
.Edit
!sort = Me.lstSorter.Column(1, Me.lstSorter.ListIndex - 1)
.Update
.FindFirst "sort=" & Me.lstSorter.Column(1, Me.lstSorter.ListIndex - 1)
.Edit
!sort = lngTemp
.Update
End With
rst.Close
Set rst = Nothing
Set db = Nothing
Me.lstSorter.Requery
End Sub
Basically it works fine, but once I have moved one item around, I cannot move another item to the post in which previously moved items are placed.
Also I still need to write the end result back to the table.
I hope that you guys can help my take this one or two steps further

/Soren