Results 1 to 7 of 7

Thread: [RESOLVED] Listview items/rows to access db

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    74

    Resolved [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

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    74

    Re: Listview items/rows to access db

    dee-u, I tried your suggestion but I have the same result.

    Name:  prtsc.JPG
Views: 144
Size:  15.8 KB

    Thanks!

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    74

    Re: Listview items/rows to access db

    I think I'm missing a loop here

  5. #5
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    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 ...

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    74

    Re: Listview items/rows to access db

    Thanks JG, I will try your code and will get back to this thread.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    74

    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
  •  



Click Here to Expand Forum to Full Width