L1.ListItems.Clear
Dim li1 As ListItem
Set li1 = LV1.ListItems.Add()
While Not (rs.EOF)
li1.Text = rs(0)
rs.MoveNext
Wend
rs.Close
L1.ListItems.Clear
Dim li1 As ListItem
Set li1 = LV1.ListItems.Add()
While Not (rs.EOF)
li1.Text = rs(0)
rs.MoveNext
Wend
rs.Close
Several useful snippets are already posted by , , and . Those show the basic idea: add columns, loop the recordset, and push each row into the ListView. The gap in the thread is a robust, reusable pattern that (1) does not hard‑code every field name, (2) is Null‑safe, and (3) formats common types. The short example below automates header creation from the Recordset.Fields collection and fills each row by iterating fields; it also shows simple Null handling and basic type-aware formatting so the ListView stays stable when the schema or data change.
' dynamic populate: builds columns from rs.Fields and fills rows without hard-coded names
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim lv As ListView
Dim itm As ListItem
Dim f As ADODB.Field
Dim i As Integer, fCount As Integer
Set lv = ListView1
' initialize control (view/gridlines/clear)
With lv
.View = lvwReport
.Gridlines = True
.ColumnHeaders.Clear
.ListItems.Clear
End With
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\YourDb.mdb;"
Set rs = New ADODB.Recordset
rs.Open "SELECT * FROM YourTable", cn, adOpenForwardOnly, adLockReadOnly
fCount = rs.Fields.Count
For i = 0 To fCount - 1
lv.ColumnHeaders.Add , , rs.Fields(i).Name, lv.Width \ fCount, lvwColumnLeft
Next
Do While Not rs.EOF
Set itm = lv.ListItems.Add(, , CStr(Nz$(rs.Fields(0).Value))) ' primary column text
For i = 1 To fCount - 1
Dim v: v = rs.Fields(i).Value
If IsNull(v) Then
itm.SubItems(i) = ""
ElseIf rs.Fields(i).Type = adDate Then
itm.SubItems(i) = Format$(v, "yyyy-mm-dd")
ElseIf rs.Fields(i).Type = adCurrency Or rs.Fields(i).Type = adDouble Then
itm.SubItems(i) = Format$(v, "#,##0.00")
Else
itm.SubItems(i) = CStr(v)
End If
Next
rs.MoveNext
Loop
rs.Close: cn.Close
Set rs = Nothing: Set cn = Nothing Troubleshooting notes: a) Use a client cursor (rs.CursorLocation = adUseClient) if you need RecordCount. b) Always guard against Nulls (ListView expects strings). c) For very large result sets prefer paging or a virtual list / DataGridView in modern projects — ListView can become slow with thousands of rows. d) Close and set ADO objects to Nothing to avoid leaks. This approach builds on the header work shown by and the ADODB examples from and while avoiding brittle, field-by-field hard coding.
Jump to Post— alfadz 0Voici une sub qui te permettra d'initialiser ton listview
Private Sub Init_ListviewDv() Rem=====pour mettre à jour le listview ======= With LviewDv .ListItems.Clear .ColumnHeaders.Clear .ColumnHeaders.Add , , "Nop", .Width / 15, lvwColumnLeft 'Colonne(C0) .ColumnHeaders.Add , , "Champ1 de ta base ", .Width / 7, lvwColumnCenter 'C1 .ColumnHeaders.Add , …
Voici une sub qui te permettra d'initialiser ton listview
Private Sub Init_ListviewDv()
Rem=====pour mettre à jour le listview =======
With LviewDv
.ListItems.Clear
.ColumnHeaders.Clear
.ColumnHeaders.Add , , "Nop", .Width / 15, lvwColumnLeft 'Colonne(C0)
.ColumnHeaders.Add , , "Champ1 de ta base ", .Width / 7, lvwColumnCenter 'C1
.ColumnHeaders.Add , , "Champ2...", .Width / 3, lvwColumnLeft 'C2
.ColumnHeaders.Add , , "Champ3...", .Width / 6, lvwColumnRight 'C3
.ColumnHeaders.Add , , "Champ4...", .Width / 6, lvwColumnRight 'C4
.ColumnHeaders.Add , , "Champ5....", .Width / 7, lvwColumnRight 'C5
.ColumnHeaders.Add , , "Observation", .Width / 2, lvwColumnLeft 'C10
.View = lvwReport
.Gridlines = False
End With
End Sub
une seconde sub qui te permettre de charger les éléments de la base de donnée dans le listview pour affichage===
`Private Sub MajLviewDv()
'rem mise à jour du listview
`'Déclare une variable pour ajouter des objets ListItem.`
Dim itmx As ListItem
' Gestion erreur
On Error Resume Next
' Ajout des colonnes à la listview
Init_ListviewDv
' Préparation de la requêtes
RsqlDv = "SELECT * FROM Devise " 'where [Type_Payement]='" & LstEnc & "'"
'Execution requête avec paramètre recordset via CnxAdo
RDv.CursorLocation = adUseClient
RDv.Open RsqlDv, CnxDv, adOpenDynamic, adLockPessimistic
' on est toujours sous la gestion d'erreur
'Execute_Sql = (err.Number = 0)
'If err.Number Then err.Clear
' nbr enregistrement base
'Info_Nbr_record_base = RstAdo.RecordCount
' Vérifie si la base n'est pas vide
'If Info_Nbr_record_base = 0 Then
'MsgBox "La base de données est vide !", vbExclamation Or vbOKOnly, "Information utilisateur"
'Else
' Remplie la listview via la base
While Not RDv.EOF
Set itmx = LviewDv.ListItems.Add(, , CStr(RDv!NumOP))
itmx.SubItems(1) = RDv!DateOpera
itmx.SubItems(2) = RDv!LibeleOpera
itmx.SubItems(3) = Format(RDv!MEntree, "# ##0.00")
itmx.SubItems(4) = Format(RDv!MSortie, "# ##0.00")
itmx.SubItems(5) = Format(RDv!MSolde, "# ##0.00")
itmx.SubItems(6) = RDv!Commentaire
' Passe à l'enregistrement suivant.
RDv.MoveNext
Wend
'Calcul Somme Colonne Montant_Réglé de Listview
Dim k As Integer
Dim Total1 As Currency
Dim Total2 As Currency
With LviewDv
For k = 1 To .ListItems.Count
Total1 = Total1 + .ListItems(k).ListSubItems(3).Text
Total2 = Total2 + .ListItems(k).ListSubItems(4).Text
Next
Txbox(6).Value = Format(Total1 - Total2, "# ##0.00 DA")
End With
'libére l'object
Set RDv = Nothing
Set CnxDv = Nothing
'On sort
Exit Sub
Aff_Err:
'Type erreur
MsgBox Err.Description, vbExclamation Or vbOKOnly, "Information utilisateur"
Err.Clear
End Sub`
, please see our posting rules. We need to keep it in English. Thanx for your post though...
, the below part of alfadz's code will do the trick -
Private Sub Init_ListviewDv()
Rem=====pour mettre à jour le listview =======
With LviewDv
.ListItems.Clear
.ColumnHeaders.Clear
.ColumnHeaders.Add , , "Nop", .Width / 15, lvwColumnLeft 'Colonne(C0)
.ColumnHeaders.Add , , "Champ1 de ta base ", .Width / 7, lvwColumnCenter 'C1
.ColumnHeaders.Add , , "Champ2...", .Width / 3, lvwColumnLeft 'C2
.ColumnHeaders.Add , , "Champ3...", .Width / 6, lvwColumnRight 'C3
.ColumnHeaders.Add , , "Champ4...", .Width / 6, lvwColumnRight 'C4
.ColumnHeaders.Add , , "Champ5....", .Width / 7, lvwColumnRight 'C5
.ColumnHeaders.Add , , "Observation", .Width / 2, lvwColumnLeft 'C10
.View = lvwReport
.Gridlines = False
End With
End Sub
Andre shown how to add header and display it in report style.
Below is how to display data into listview using access and adodb.
Modified as you need and show to us how it work..
Private Sub ShowData()
Dim con As ADODB.Connection
Set con = New ADODB.Connection
Dim rsShow As ADODB.Recordset
Set rsShow = New ADODB.Recordset
con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\project.MDB;Persist Security Info=False"
rsShow.Open "SELECT * FROM Student ", con, adOpenStatic, adLockOptimistic
ListView1.ListItems.Clear
While Not rsShow.EOF
Set View = ListView1.ListItems.Add
View.Text = rsShow!ID
View.SubItems(1) = rsShow!LastName
View.SubItems(2) = rsShow!FirstName
View.SubItems(3) = rsShow!MiddleName
View.SubItems(4) = rsShow!Age
View.SubItems(5) = rsShow!Sex
'and soon
rsShow.MoveNext
Wend
rsShow.Close
con.Close
End Sub
You can try this code this is tested and working from me.
Dim DB As New ADODB.Connection
Dim RS As New ADODB.Recordset
Dim path as String
path = "\Database.mdb"
DB.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " & path
Dim x As Integer
ListView1.ListItems.Clear
RS.Open "Table", DB, 3, 3
Do Until RS.EOF
Set List = Listview1.ListItems.Add(, , RS(0), , 1)
For x = 1 To 3 'if you have 4 fields in the Access like (ID,Fname,Mname,Lname) if you have more than 4 fields like 5 fields just add 1 to 4
List.SubItems(x) = RS(x)
Next x
RS.MoveNext
Loop
Set RS = Nothing
DB.Close: Set DB = Nothing
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.