'FileName 是数据库的文件名 'StrSQL 是SQL语句: 如 Select * From Table'**********************************方法 Public Function LoadData(ByVal FileName As String, ByVal strSql As String) Dim dB As Connection Dim Item1 As ListItem
Set dB = New Connection dB.CursorLocation = adUseClient dB.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & FileName & ";"
Set adoPrimaryRS = Nothing Set adoPrimaryRS = New Recordset adoPrimaryRS.Open strSql, dB, adOpenStatic, adLockOptimistic
lv.ColumnHeaders.Clear lv.ListItems.Clear If adoPrimaryRS.RecordCount <> 0 Then adoPrimaryRS.MoveFirst For j = 0 To adoPrimaryRS.Fields.Count - 1 lv.ColumnHeaders.Add , , adoPrimaryRS.Fields(j).Name Next
For j = 1 To adoPrimaryRS.RecordCount Set Item1 = lv.ListItems.Add(, , adoPrimaryRS.Fields(0).Value) For I = 1 To adoPrimaryRS.Fields.Count - 1 If Not IsNull(adoPrimaryRS.Fields(I)) Then Item1.SubItems(I) = adoPrimaryRS.Fields(I) End If Next adoPrimaryRS.MoveNext Next lv.View = lvwReport End Function
'FileName 是数据库的文件名
'StrSQL 是SQL语句: 如 Select * From Table'**********************************方法
Public Function LoadData(ByVal FileName As String, ByVal strSql As String)
Dim dB As Connection
Dim Item1 As ListItem
Set dB = New Connection
dB.CursorLocation = adUseClient
dB.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & FileName & ";"
Set adoPrimaryRS = Nothing
Set adoPrimaryRS = New Recordset
adoPrimaryRS.Open strSql, dB, adOpenStatic, adLockOptimistic
lv.ColumnHeaders.Clear
lv.ListItems.Clear
If adoPrimaryRS.RecordCount <> 0 Then adoPrimaryRS.MoveFirst
For j = 0 To adoPrimaryRS.Fields.Count - 1
lv.ColumnHeaders.Add , , adoPrimaryRS.Fields(j).Name
Next
For j = 1 To adoPrimaryRS.RecordCount
Set Item1 = lv.ListItems.Add(, , adoPrimaryRS.Fields(0).Value)
For I = 1 To adoPrimaryRS.Fields.Count - 1
If Not IsNull(adoPrimaryRS.Fields(I)) Then
Item1.SubItems(I) = adoPrimaryRS.Fields(I)
End If
Next
adoPrimaryRS.MoveNext
Next
lv.View = lvwReport
End Function