2014-02-07 37 views
0

我在访问vba中为列表框项目上下移动写代码。需要在访问中使用.List属性。但它会引发错误,说没有找到方法或成员。任何使用.List替换方法?研究这4天以上。 私人小组cmdUP_Click()访问VBA有Listbox.List方法,因为excel VBA有

Dim i As Long 
Dim leaveAlone As Boolean 
Dim pos As Long 
Dim Temp As String 

pos = 0 

With Me.lbfNames 
For i = 0 To .ListCount - 1 
leaveAlone = False 

If .Selected(i) Then 

    If i = pos Then 
    leaveAlone = True 
    End If 

pos = pos + 1 

    If leaveAlone = False Then 
    Temp = .RowSource(i - 1) 
    .RowSource(i - 1) = .RowSource(i) ' before i used .List instead of rowsource 
    .RowSource(i) = Temp 
    .ListIndex = i - 1 
    .Selected(i) = False 
    .Selected(i - 1) = True 
    End If 

    End If 
    Next 

    End With 
+0

Access中的列表框有'rowSource'属性,也许你应该检查并操作它以获得你想要的。如果你的'rowSource'是一个SQL指令,那么你需要定义这些行在SQL中的排序方式。 – Barranka

+0

请看我上面的代码,当点击按钮时向上移动选定的项目。在这种情况下,rowource属性会抛出异常,导致错误的参数错误。 – vuyy1182

回答

1

我已经想通了这一点,如何做到这一点的访问。但是将列表框Multiselect属性设置为'None'。

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