dcsimg
Results 1 to 3 of 3

Thread: From listview to database

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2016
    Posts
    438

    From listview to database

    Hello experts
    I need to edit data in database according to listview checked or unchecked items.
    This is what I'm doing but without success.

    Code:
    Private Sub lvw_ItemCheck(ByVal Item As MSComctlLib.ListItem)
    For i = 1 To lvw.ListItems.Count
      sSQL = "Select passe from  tbl1 where ID = " & lvw.ListItems.Item(i).SubItems(2)
       RS.Open sSQL, DB, adOpenDynamic, adLockOptimistic
       
     If lvw.ListItems.Item(i).Checked = True Then
     RS!passe = "oui"
     Else
     RS!passe = "non"
     RS.Update
     End If
     Next
    End Sub
    Thank you very much
    Last edited by Mustaphi; Jan 12th, 2019 at 03:43 PM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,162

    Re: From listview to database

    This is what I'm doing but without success.
    what is happening? wrong result? error? nothing?
    as you do not close the recordset you should get an error on the next listview item when it tries to open the recordset

    try
    Code:
     sSQL = "Select passe from  tbl1 where ID = '" & lvw.ListItems.Item(i).SubItems(2) &"'"
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,835

    Re: From listview to database

    Code:
    If lvw.ListItems.Item(i).Checked = True Then
         RS!passe = "oui"
    Else
         RS!passe = "non"
         RS.Update
    End If
    Your update is in the wrong place, should be after the end if or if you prefer you could place it in both the If and the Else portion as is the update only executes in the case where it is not checked.
    Code:
    If lvw.ListItems.Item(i).Checked = True Then
         RS!passe = "oui"
    Else
         RS!passe = "non"     
    End If
    RS.Update

    Code:
    If lvw.ListItems.Item(i).Checked = True Then
         RS!passe = "oui"
         RS.Update
    Else
         RS!passe = "non"
         RS.Update
    End If
    You also need to close the recordset before trying to open it again.
    You should add a RS.Close just before the Next at the bottom of the loop

    Also if there is any chance that the item in the listview is not in the db already then you need to handle that by checking to make sure a record is returned and then using an addnew if it needs to be added.
    Last edited by DataMiser; Jan 15th, 2019 at 12:32 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width