• Sort List theo Alphabe
  • Sort List theo Alphabe

    Noname > 27-08-10, 09:56 AM

    Mới lang thang tìm thấy 1 đoạn code cho phép Sort 1 Listbox theo Alphabe. Mời mọi người tham khảo, có gì thắc mắc cứ comment hỏi nhé!

    Mã:
    Function SortListBox(objListBox As ListBox)
        Dim intFirst As Integer
        Dim intLast As Integer
        Dim intNumItems As Integer
        Dim i As Integer
        Dim j As Integer
        Dim strTemp As String
        Dim MyArray() As Variant

        'Re-Dim the array
        ReDim MyArray(objListBox.ListCount - 1)

        'Get upper and lower boundary
        intFirst = LBound(MyArray)
        intLast = UBound(MyArray)

        'Set array values
        For i = LBound(MyArray) To UBound(MyArray)
        MyArray(i) = objListBox.ItemData(i)
        Next i

        'Loop through array values to determine sort
        For i = intFirst To intLast - 1
        For j = i + 1 To intLast
        If MyArray(i) > MyArray(j) Then
        strTemp = MyArray(j)
        MyArray(j) = MyArray(i)
        MyArray(i) = strTemp
        End If
        Next j
        Next i

        'Remove all items
        For i = intLast - 1 To intFirst Step -1
        objListBox.RemoveItem i
        Next i

        'Add all items in order
        For i = intFirst To intLast - 1
        objListBox.AddItem MyArray(i), i
        Next

        End Function
    Bạn tạo 1 module mới rồi copy đoạn code trên vào.
    tạo 1 form, tạo 1 listbox và 1 nút nhất. Để tên mặc định hết cho nhanh nha.
    Row source type của lis bạn chọn Value list
    Row Source bạn điền: "Rượu;Socola;Bánh;Bao Bì;Bột Ngọt;Cà Chua;Chanh;Đường;Gạo;Hành;Kem Đánh Răng;Khoai Tây;Mì Gói;Mức;Muối;Ớt;Sữa;Tiêu;Hộp quẹt"

    hoặc gì tùy bạn

    Trong event click nút nhấn, bạn gọi:

    Mã:
    Private Sub Command1_Click()
        Call SortListBox(List0)
        End Sub


    Mời xem demo:
    SortList
  • RE: Sort List theo Alphabe

    hoangthanhhai > 07-06-11, 03:03 PM

    (27-08-10, 09:56 AM)Noname Đã viết: Mới lang thang tìm thấy 1 đoạn code cho phép Sort 1 Listbox theo Alphabe. Mời mọi người tham khảo, có gì thắc mắc cứ comment hỏi nhé!



    Mời xem demo:
    SortList

    Bạn ơi mình có sort được theo ComboBox được không nhỉ?
    Mình có tạo form nhập liệu có 1 ComboBox
    - Row Source Type: Table/Query
    - Row Soure: Q_Chucdanh (Ma_CV; Chucdanh)

    Trong Query (Q_chucdanh) mình đã sort theo Ma_CV và đã ok; nhưng trong ComboBox của mình thì nó lại không sort theo Ma_CV mà nó lại sắp xếp theo thứ tự Ma_CV nào nhập vào trước thì nó có trước.

    Ví dụ:
    Trong Table mình nhập như sau:
    Ma_CV Chudanh
    0101 Giám đốc
    0201 Trưởng phòng Thương mại
    0102 Phó Giám đốc
    0201 Phó phòng Thương mại

    Trong Query và Table mình đã sort
    Ma_CV Chudanh
    0101 Giám đốc
    0102 Phó Giám đốc
    0201 Trưởng phòng Thương mại
    0201 Phó phòng Thương mại

    Nhưng trong ComboBox nó vẫn hiện là
    Ma_CV Chudanh
    0101 Giám đốc
    0201 Trưởng phòng Thương mại
    0102 Phó Giám đốc
    0201 Phó phòng Thương mại

    Cho mình hỏi là làm cách nào để Combo nó vẫn sort được nhỉ, vì hiện tại mình mới có 1 số chức danh công việc sau này sẽ phát sinh nhiều chức danh mới.

    Thanks
  • RE: Sort List theo Alphabe

    donxxiii > 21-10-11, 08:46 PM

    Row Soure: Q_Chucdanh (Ma_CV; Chucdanh)
    nhập thêm acsending vào cái collum ma_cv
  • RE: Sort List theo Alphabe

    kagetsu > 13-03-12, 12:53 PM

    (27-08-10, 09:56 AM)Noname Đã viết: 'Remove all items
    For i = intLast - 1 To intFirst Step -1
    objListBox.RemoveItem i
    Next i

    'Add all items in order
    For i = intFirst To intLast - 1
    objListBox.AddItem MyArray(i), i
    Next

    Remove all và add all thay intLast - 1 = intLast bạn ah.