Hej! Nåt sånt här kanske?.... Alternativ lösning med en generell funktion: Suveränt, tack hjälpte mej också.Tabell till Listview
Jag försöker att läsa in allt i en tabell till listviewn, men lyckas endast att läsa in sista posten i tabellen, hur ska jag göra för att kunna läsa in allt som finns i tabellen till listview?
//Thanxx Sv: Tabell till Listview
ListView1.View = lvwReport
ListView1.ColumnHeaders.Add , , "FIELD1", 1000
ListView1.ColumnHeaders.Add , , "FIELD2", 3000
dim LI as listitem
'koppla dig mot databasen och hämta ett recordset
set rst= con.execute(select ......)
while not rst.eof
Set LI = ListView1.ListItems.Add(, B, CStr(rst!fältnamn1))
LI.SubItems(1) = CStr(rst!fältnamn2)
rst.movenext
wendSv: Tabell till Listview
Option Explicit
Private Const DBFileName = "C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb"
Private Sub InitializeListView()
With ListView1
.View = lvwReport
.ColumnHeaders.Add , , "Company Name"
.ColumnHeaders.Add , , "Contact Name"
.ColumnHeaders.Add , , "Contact Title"
.ColumnHeaders.Add , , "Address"
.ColumnHeaders.Add , , "City"
.ColumnHeaders.Add , , "Region"
.ColumnHeaders.Add , , "Postal Code"
.ColumnHeaders.Add , , "Country"
.ColumnHeaders.Add , , "Phone"
.ColumnHeaders.Add , , "Fax"
End With
End Sub
Private Sub Form_Load()
Dim conn As ADODB.Connection
Dim rsTemp As ADODB.Recordset
On Error GoTo Form_Load_Err
Set conn = New ADODB.Connection
Set rsTemp = New ADODB.Recordset
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Persist Security Info=False;" & _
"Data Source=" & DBFileName
rsTemp.Open "Customers", conn, adOpenForwardOnly, adLockReadOnly, adCmdTableDirect
InitializeListView
LoadListView ListView1, rsTemp, "C", rsTemp("CustomerID"), rsTemp("CustomerID"), rsTemp("CompanyName"), rsTemp("ContactName"), rsTemp("ContactTitle"), rsTemp("Address"), rsTemp("City"), rsTemp("Region"), rsTemp("PostalCode"), rsTemp("Country"), rsTemp("Phone"), rsTemp("Fax")
rsTemp.Close
conn.Close
Form_Load_Exit:
Set rsTemp = Nothing
Set conn = Nothing
Exit Sub
Form_Load_Err:
MsgBox Err.Description, vbCritical
Resume Form_Load_Exit
End Sub
Private Sub LoadListView(ListView As MSComctlLib.ListView, Recordset As ADODB.Recordset, KeyPrefix As String, KeyField As ADODB.Field, TextField As ADODB.Field, ParamArray Fields() As Variant)
Dim I As Long
Dim Count As Long
Dim fldField As ADODB.Field
Dim Item As MSComctlLib.ListItem
Dim ListItems As MSComctlLib.ListItems
Count = UBound(Fields)
Set ListItems = ListView.ListItems
Do Until Recordset.EOF
Set Item = ListItems.Add(, KeyPrefix & KeyField, "" & TextField)
For I = 1 To Count
Set fldField = Fields(I)
Item.SubItems(I) = "" & fldField
Next
Recordset.MoveNext
Loop
End Sub
Private Sub Form_Resize()
ListView1.Move ScaleLeft, ScaleTop, ScaleWidth, ScaleHeight
End SubSv: Tabell till Listview
Men en fråga, om man inte vet sökvägen utan vill bläddra efter databas filen hur skulle man kunna fixa det?
I min databas fil har jag 4st tabeller men alla heter olika, likaså alla "fields"
Har ändrat koden för att passa min databas fil men får följande fel meddelande:
Key is not unique in collection.
?????
Min kod:
Option Explicit
Private Const DBFileName = "C:\Karlssons Data och Webdesign\Dagbok.mdb"
Private Sub InitializeListView()
With ListView1
.View = lvwReport
.ColumnHeaders.Add , , "Datum"
.ColumnHeaders.Add , , "Kund"
.ColumnHeaders.Add , , "Arbete"
.ColumnHeaders.Add , , "Start"
.ColumnHeaders.Add , , "Slut"
.ColumnHeaders.Add , , "Timmar"
.ColumnHeaders.Add , , "Hardware"
.ColumnHeaders.Add , , "ProjNummer"
.ColumnHeaders.Add , , "Fakt"
.ColumnHeaders.Add , , "Other"
End With
End Sub
Private Sub Form_Load()
Dim conn As ADODB.Connection
Dim rsTemp As ADODB.Recordset
On Error GoTo Form_Load_Err
Set conn = New ADODB.Connection
Set rsTemp = New ADODB.Recordset
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Persist Security Info=False;" & _
"Data Source=" & DBFileName
rsTemp.Open "TBL_Dagbok", conn, adOpenForwardOnly, adLockReadOnly, adCmdTableDirect
InitializeListView
LoadListView ListView1, rsTemp, "C", rsTemp("Dagbok_ID"), rsTemp("Dagbok_ID"), rsTemp("Dagb_Datum"), rsTemp("Dagb_Kund"), rsTemp("Dagb_Arbete"), rsTemp("Start"), rsTemp("Slut"), rsTemp("Timmar"), rsTemp("Hardware"), rsTemp("ProjNummer"), rsTemp("Fakt"), rsTemp("Other")
rsTemp.Close
conn.Close
Form_Load_Exit:
Set rsTemp = Nothing
Set conn = Nothing
Exit Sub
Form_Load_Err:
MsgBox Err.Description, vbCritical
Resume Form_Load_Exit
End Sub
Private Sub LoadListView(ListView As MSComctlLib.ListView, Recordset As ADODB.Recordset, KeyPrefix As String, KeyField As ADODB.Field, TextField As ADODB.Field, ParamArray Fields() As Variant)
Dim I As Long
Dim Count As Long
Dim fldField As ADODB.Field
Dim Item As MSComctlLib.ListItem
Dim ListItems As MSComctlLib.ListItems
Count = UBound(Fields)
Set ListItems = ListView.ListItems
Do Until Recordset.EOF
Set Item = ListItems.Add(, KeyPrefix & KeyField, "" & TextField)
For I = 1 To Count
Set fldField = Fields(I)
Item.SubItems(I) = "" & fldField
Next
Recordset.MoveNext
Loop
End Sub
Private Sub Form_Resize()
ListView1.Move ScaleLeft, ScaleTop, ScaleWidth, ScaleHeight
End Sub
Jätte bussigt om du/nån kan hjälpa mej
/Tomas