Listview接受vba中的Nill值

时间:2017-05-19 09:38:11

标签: excel-vba vba excel

在listview中上传数据时,如果任何项目没有valuew,则会提取下一行数据。我们可以允许在listview中显示nill值。

Dim Acon As New ADODB.Connection
Dim Rs As New ADODB.Recordset
Dim row                 As Long
With Acon
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .ConnectionString = "Data Source=" + ThisWorkbook.Sheets(1).Range("V27").Value
    .Open
End With

With Rs
    .CursorLocation = adUseClient
    .CursorType = adOpenDynamic
    .LockType = adLockOptimistic
    .ActiveConnection = Acon
    .Source = "Select * from OJT where [Created by]='" & username1 & "'"
    .Open
End With

With Rs
    Rs.MoveFirst
    While Not Rs.EOF

        If Rs.Fields("Created by").Value = username1 Then
            On Error Resume Next
            ListView14.ListItems.Add , , Rs.Fields("Emp #").Value
            ListView14.ListItems(lv_item).ListSubItems.Add , , Rs.Fields("Name").Value
            ListView14.ListItems(lv_item).ListSubItems.Add , , Rs.Fields("Created by").Value
            lv_item = lv_item + 1
            On Error GoTo 0
        End If
        Rs.MoveNext
    Wend
End With

Rs.Close
Acon.Close

1 个答案:

答案 0 :(得分:0)

将空字符串附加到您所拥有的字符串似乎是最简单的解决方案

e.g。 ListView14.ListItems.Add , , Rs.Fields("Emp #").Value & ""