mresann
Registered User.
- Local time
- Today, 12:59
- Joined
- Jan 11, 2005
- Messages
- 357
A common occurrence in working with Access interface forms is to be able to move a Listbox item up or down a list, sometimes to the top or bottom of the list. The programming for such a relatively benign function is involved. The Listbox must use a Row Source Type of Value/List with delimiters of either a comma ( , ) or semi-colon ( ; ) in the Row Source assignment. Using command buttons to move data up or down, or to the top or bottom, makes it easy for the user to position data within the listbox.
Here is a typical Listbox with navigation buttons for items:
Let's say you want to move the selected item down one position. Clicking the "Move Down" button produces the following result:
Clicking the "Move Top" button produces the following result:
Clicking the "Move Bottom" button produces the following result:
Clicking the "Move Up" button produces the following result:
The function is called by passing the listbox control and an increment amount. In addition, the navigation buttons for moving the list item are typically called so that their enabled properties can be properly assigned. The function calls are done through any control or programmatic process that alters the list order of the Listbox, most commonly through the navigation command buttons shown above. Here is how the function would be called from the "Move Up" command button, which moves the selected list item up one position:
Note that lngIncrement is passed with a constant. The four constants that are located on the public module "basMoveListItem" are assigned as follows:
Note that the Move Top and Move Bottom constants are very high long values, but the constants make the code easy to follow. If you choose not to use constants, then make sure that you use the exact numbers indicated.
Back to the function call: Note that the command buttons are passed as well, so that they are enabled or disabled depending on the resulting position of the selected item in the Listbox. You can see it in the examples above as well.
In addition, if you select a list item, the listbox AfterUpdate procedure passes the command buttons as well, using an increment value of zero (0) as no items were moved. Here is an example:
In the sample database, there are two listbox examples. The second example just uses two command buttons to move the list item up or down, but does not include command buttons that move an item to the top or bottom of a list directly. In this case, the function can be called with just the Move Up and Move Down values as shown:
The function is generously commented through each process, so you can follow the logic more easily. You can just port the entire bas file to your project, or just use the function in your own modules, making sure you also include the public constants.
The attachment contains a sample database in MDB format, as well as the
Here is a typical Listbox with navigation buttons for items:

Let's say you want to move the selected item down one position. Clicking the "Move Down" button produces the following result:

Clicking the "Move Top" button produces the following result:

Clicking the "Move Bottom" button produces the following result:

Clicking the "Move Up" button produces the following result:

The function is called by passing the listbox control and an increment amount. In addition, the navigation buttons for moving the list item are typically called so that their enabled properties can be properly assigned. The function calls are done through any control or programmatic process that alters the list order of the Listbox, most commonly through the navigation command buttons shown above. Here is how the function would be called from the "Move Up" command button, which moves the selected list item up one position:
Code:
Call fncMoveListItem( _
ctlListBox:=lstExample01, _
lngIncrement:=cglngMoveUp, _
cmdMoveUp:=cmdMoveUp01, _
cmdMoveDown:=cmdMoveDown01, _
cmdMoveTop:=cmdMoveTop01, _
cmdMoveBottom:=cmdMoveBottom01)
Note that lngIncrement is passed with a constant. The four constants that are located on the public module "basMoveListItem" are assigned as follows:
Code:
Public Const cglngMoveUp As Long = -1
Public Const cglngMoveDown As Long = 1
Public Const cglngMoveTop As Long = -99999999
Public Const cglngMoveBottom As Long = 99999999
Note that the Move Top and Move Bottom constants are very high long values, but the constants make the code easy to follow. If you choose not to use constants, then make sure that you use the exact numbers indicated.
Back to the function call: Note that the command buttons are passed as well, so that they are enabled or disabled depending on the resulting position of the selected item in the Listbox. You can see it in the examples above as well.
In addition, if you select a list item, the listbox AfterUpdate procedure passes the command buttons as well, using an increment value of zero (0) as no items were moved. Here is an example:
Code:
Call fncMoveListItem( _
ctlListBox:=lstExample01, _
lngIncrement:=0, _
cmdMoveUp:=cmdMoveUp01, _
cmdMoveDown:=cmdMoveDown01, _
cmdMoveTop:=cmdMoveTop01, _
cmdMoveBottom:=cmdMoveBottom01)
In the sample database, there are two listbox examples. The second example just uses two command buttons to move the list item up or down, but does not include command buttons that move an item to the top or bottom of a list directly. In this case, the function can be called with just the Move Up and Move Down values as shown:
Code:
Call fncMoveListItem( _
ctlListBox:=lstExample02, _
lngIncrement:=cglngMoveUp, _
cmdMoveUp:=cmdMoveUp02, _
cmdMoveDown:=cmdMoveDown02)
The function is generously commented through each process, so you can follow the logic more easily. You can just port the entire bas file to your project, or just use the function in your own modules, making sure you also include the public constants.
The attachment contains a sample database in MDB format, as well as the
Attachments
Last edited: