Does access VBA has Listbox.List method as excel VBA has

svuyyuru

Registered User.
Local time
Today, 17:24
Joined
Feb 10, 2014
Messages
33
I'm writing code in access vba for the list box selected item to move up and down. Needs to use .List Property in access . But it throws an error says no method or member found. Any replace method with .List ?
Private Sub cmdUP_Click()
Dim i As Long
Dim leaveAlone As Boolean
Dim pos As Long
Dim Temp As Stringpos = 0
With Me.lbfNamesFor i = 0 To .ListCount - 1
leaveAlone = FalseIf .Selected(i) Then
If i = pos Then leaveAlone = True
End If
pos = pos + 1
If leaveAlone = False Then
Temp = .List (i - 1)
.List (i - 1) = .RowSource(i) ' before i used .List instead of rowsource in excel VBA
.List (i) = Temp
.ListIndex = i - 1
.Selected(i) = False
.Selected(i - 1) = True
End If
End If
Next
End With
 
Last edited:
Researching on this more than 4 days.

Where? Have you considered looking the listbox and its methods up in the documentation?
 
I've tried all vba listbox methods contains in access
 
Last edited:
I have figured that it can be done by Set listbox property Multiselect to 'None' and by using the below code to achive this :).

Moving down

Private Sub cmdDown_Click()
Dim sText As String
Dim iIndex As Integer
Dim bottomLimit As Integer
iIndex = lbfNames.ListIndex
bottomLimit = lbfNames.ListCount - 1
'check: only proceed if there is a selected item
If lbfNames.ListCount > 1 Then
If iIndex >= bottomLimit Then
MsgBox ("Can not move the item down any further.")
Exit Sub
End If
'save items text and items indexvalue
sText = lbfNames.Column(0, iIndex)
If iIndex < bottomLimit Then
lbfNames.RemoveItem iIndex
'place item back in new position
lbfNames.AddItem sText, iIndex + 1
End If
'if you keep that item selected
'you can keep moving it by pressing btnMoveDown
lbfNames.Selected(iIndex + 1) = True
iIndex = iIndex + 1
End If
End Sub

Moving up

Private Sub cmdUP_Click()
Dim sText As String
Dim iIndex As Integer
iIndex = lbfNames.ListIndex
' ReDim iIndex(0 To 10)
'check: only proceed if there is a selected item
If lbfNames.ListCount > 1 Then
'index 0 is top item which can't be moved up!
If iIndex <= 0 Then
MsgBox ("Can not move the item up any higher.")
Exit Sub
End If
' If iIndex = -1 Or lbfNames.ListCount > 1 Then
'save items text and items indexvalue
sText = lbfNames.Column(0, iIndex)
lbfNames.RemoveItem iIndex
'place item back on new position
lbfNames.AddItem sText, iIndex - 1
'if you keep that item selected
'you can keep moving it by pressing cmdUp
lbfNames.Selected(iIndex - 1) = True
iIndex = iIndex - 1
End If
End Sub
 
I seriously could not let this pass on by.. Please use CODE tags next time !

Moving down
Code:
Private Sub cmdDown_Click()
    Dim sText As String
    Dim iIndex As Integer
    Dim bottomLimit As Integer
    
    iIndex = lbfNames.ListIndex
    bottomLimit = lbfNames.ListCount - 1
    
    [COLOR=Green]'check: only proceed if there is a selected item[/COLOR]
    If lbfNames.ListCount > 1 Then
        If iIndex >= bottomLimit Then
            MsgBox ("Can not move the item down any further.")
            Exit Sub
        End If
        
        [COLOR=Green]'save items text and items indexvalue[/COLOR]
        sText = lbfNames.Column(0, iIndex)
        
        If iIndex < bottomLimit Then
            lbfNames.RemoveItem iIndex
            [COLOR=Green]'place item back in new position[/COLOR]
            lbfNames.AddItem sText, iIndex + 1
        End If
        
        [COLOR=Green]'if you keep that item selected[/COLOR]
        [COLOR=Green]'you can keep moving it by pressing btnMoveDown[/COLOR]
        lbfNames.Selected(iIndex + 1) = True
        iIndex = iIndex + 1
    End If
End Sub
Moving up
Code:
Private Sub cmdUP_Click()
    Dim sText As String
    Dim iIndex As Integer
    
    iIndex = lbfNames.ListIndex
    
   [COLOR=Green] ' ReDim iIndex(0 To 10)
    'check: only proceed if there is a selected item[/COLOR]
    If lbfNames.ListCount > 1 Then
       [COLOR=Green] 'index 0 is top item which can't be moved up![/COLOR]
        If iIndex <= 0 Then
            MsgBox ("Can not move the item up any higher.")
            Exit Sub
        End If
        
       [COLOR=Green] ' If iIndex = -1 Or lbfNames.ListCount > 1 Then
        'save items text and items indexvalue[/COLOR]
        sText = lbfNames.Column(0, iIndex)
        lbfNames.RemoveItem iIndex
       [COLOR=Green] 'place item back on new position[/COLOR]
        lbfNames.AddItem sText, iIndex - 1
        [COLOR=Green]'if you keep that item selected
        'you can keep moving it by pressing cmdUp[/COLOR]
        lbfNames.Selected(iIndex - 1) = True
        iIndex = iIndex - 1
    End If
End Sub
 

Users who are viewing this thread

Back
Top Bottom