[RESOLVED] Listview items/rows to access db
I've been trying to import or save my listview items/rows/content to access db.. ive been playing around this code:
Code:
Set rs = New ADODB.Recordset
rs.Open "Select * from TblSearchMainAssets", cn, adOpenKeyset, adLockOptimistic
For Each itm In ListView1.ListItems
rs.AddNew
rs.Fields("Assettag") = ListView1.SelectedItem.Text
rs.Fields("AssetDescription") = ListView1.SelectedItem.SubItems(1)
Next
rs.Update
rs.Requery
MsgBox "done"
rs.Close
Set rs = Nothing
but it only save the item on the first row of my database. Any idea on what I should do with the code?
Thanks in advance
Re: Listview items/rows to access db
The rs.Update should be inside the loop:
Code:
rs.AddNew
rs.Fields("Assettag") = ListView1.SelectedItem.Text
rs.Fields("AssetDescription") = ListView1.SelectedItem.SubItems(1)
rs.Update
1 Attachment(s)
Re: Listview items/rows to access db
dee-u, I tried your suggestion but I have the same result.
Attachment 100481
Thanks!
Re: Listview items/rows to access db
I think I'm missing a loop here :confused:
Re: Listview items/rows to access db
IMO it would be better to use For-Next loop
Code:
Dim X As Integer
For X = 1 To ListView1.ListItems.Count
rs.AddNew
rs!Assettag = ListView1.ListItems(X).Text
rs!AssetDescription = ListView1.ListItems(X).SubItems(1)
rs.Update
Next X
Re: Listview items/rows to access db
Thanks JG, I will try your code and will get back to this thread.
Re: Listview items/rows to access db
Many thanks to you both! problem is fixed now.
Here's the final code.
Code:
Private Sub Command1_Click()
Dim x As Integer
Set rs = New ADODB.Recordset
rs.Open "Select * from TblSearchMainAssets", cn, adOpenKeyset, adLockOptimistic
For x = 1 To ListView1.ListItems.Count
rs.AddNew
rs.Fields("Assettag") = ListView1.ListItems(x).Text
rs.Fields("AssetDescription") = ListView1.ListItems(x).SubItems(1)
rs.Update
Next x
rs.Requery
MsgBox "Done"
rs.Close
Set rs = Nothing
End Sub