Làm thế nào lấy dữ liệu từ Acces đưa vào listview.

Liên hệ QC

nplvn

Thành viên mới
Tham gia
9/5/08
Bài viết
32
Được thích
24
Mình biết diễn đàn khá lâu nhưng do trình độ VBA thấp kém nên chỉ biết đọc và tải về những ví dụ trên diễn đàn, mong mấy bạn thông cảm.
Mấy bạn chỉ giúp làm thế nào để đưa dữ liệu từ access vào listview . Cụ thể mình có gởi file vi du đính kèm. Mong được sự hướng dẫn của các bạn.
Chân thành cảm ơn trước.
 
Lần chỉnh sửa cuối:
Bạn tham khảo hàm này.
Mã:
'==================================================================
'  Purpose:   to fill a ListView control with data from a table or
'             query
'  Returns:   1 to indicate if the function was successful
'             0to indicate if the function was un-successful
'  From:      http://support.microsoft.com/kb/q155178/
'  Edit:      Date 14/08/2007 by levanduyet@yahoo.com
'==================================================================
'
Dim intTotCount As Integer
Dim intCount1 As Integer, intCount2 As Integer, intAutosize As Integer
Dim colNew As ColumnHeader, NewLine As ListItem

    On Error GoTo FillListviewFrRecordset_Error

    If TypeName(objListview) <> "ListView" Then
        FillListviewFrRecordset = 0
        Exit Function
    End If
    'Setup the ListView
    With objListview
        .View = lvwReport    '3  Set View property to Report
        .FullRowSelect = True
        .MultiSelect = False
        .Gridlines = True
        .LabelEdit = lvwManual
        .HideColumnHeaders = False
    End With

    ' Clear the ListView control.
    objListview.ListItems.Clear
    objListview.ColumnHeaders.Clear

    ' Set Column Headers.
    For intCount1 = 0 To rs.Fields.Count - 1
        Set colNew = objListview.ColumnHeaders.ADD(, , rs(intCount1).Name)
    Next intCount1

    ' Set Total Records Counter.
    rs.MoveLast
    intTotCount = rs.RecordCount
    rs.MoveFirst

    ' Loop through recordset and add Items to the control.
    For intCount1 = 1 To intTotCount
        Set NewLine = objListview.ListItems.ADD(, , rs(0).Value)

        For intCount2 = 1 To rs.Fields.Count - 1
            NewLine.SubItems(intCount2) = rs(intCount2).Value
        Next intCount2
        rs.MoveNext
    Next intCount1
    If bAutoSize = True Then
        intAutosize = LvAutosizeCols(objListview)
    End If
    FillListviewFrRecordset = 1
    Exit Function

FillListviewFrRecordset_Error:
    FillListviewFrRecordset = 0
    ' Ignore Error 94 which indicates you passed a NULL value.
    If Err = 94 Then
        Resume Next
    Else
        ' Otherwise display the error message.
        MsgBox "Error: " & Err.Number & Chr(13) & _
               Chr(10) & Err.Description
    End If

End Function

Lê Văn Duyệt
 
Upvote 0
Gởi anh Duyệt.

Trình độ của em rất thấp, nên khi chép đoạn code về đọc tới đọc lui nhưng không hiểu phải áp dụng như thế nào. Anh có thể giúp em trên ví dụ em gởi lên không.
Cảm ơn anh trước.
 
Upvote 0
Web KT
Back
Top Bottom