|
-
May 22nd, 2013, 09:44 PM
#1
Thread Starter
Lively Member
[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
-
May 23rd, 2013, 03:20 AM
#2
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
-
May 23rd, 2013, 10:11 PM
#3
Thread Starter
Lively Member
Re: Listview items/rows to access db
dee-u, I tried your suggestion but I have the same result.

Thanks!
-
May 23rd, 2013, 10:14 PM
#4
Thread Starter
Lively Member
Re: Listview items/rows to access db
I think I'm missing a loop here
-
May 23rd, 2013, 11:30 PM
#5
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
JG
... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...
-
May 24th, 2013, 12:22 AM
#6
Thread Starter
Lively Member
Re: Listview items/rows to access db
Thanks JG, I will try your code and will get back to this thread.
-
May 24th, 2013, 01:11 AM
#7
Thread Starter
Lively Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|