Results 1 to 12 of 12

Thread: Remove Selected item from database when removed from listview(Resolved -Thanks!!!!! )

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Location
    Home
    Posts
    85

    Remove Selected item from database when removed from listview(Resolved -Thanks!!!!! )

    Hi All

    Need a little help with this one, I am trying to write code to remove a selected item from the database when the user selects an item in the list view and here is what I have so far

    Dim adoRemove As Recordset

    Dim vacancyid As String
    Dim code As String
    vacancyid = Form7.txtFields(0).Text
    code = ListView1.List(ListView1.ListIndex)

    Adoremove.open = "select * from tblrequired requirements where lngvacancyid = " & vacancyid And bytcode = code ,db, , adOpenStatic, adLockOptimistic

    .Delete
    .Close

    I have two columns in the listview and I want to the sql statement to get the value from the 2nd column in the listview for the selected item

    Can you help me?

    Thanks in advance
    Last edited by altf4; Jul 3rd, 2004 at 04:29 AM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Location
    Home
    Posts
    85

    I am getting there, just need a little help

    Hi There
    I have got as far as being able to delete the record is if i do this -

    [Highlight=VB]
    Dim adoRemove As Recordset

    Dim db As Connection
    Set db = New Connection
    db.CursorLocation = adUseClient
    db.Open "PROVIDER=MSDASQL;dsn=XPHOME;uid=Paul ;pwd=;database=ContractManagement;"

    Set adoRemove = New Recordset

    Dim vacancyid As String
    Dim code As String
    vacancyid = Form7.txtFields(0).Text
    code = 6
    'Dim tblrequired As String
    tablerequired = tblexpertiserequirements


    adoRemove.Open "select * from tblexpertiserequirements where lngvacancyid = " & vacancyid, db, adOpenStatic, adLockOptimistic
    adoRemove.MoveFirst

    adoRemove.Find "bytexpertisecode = '" & code & "'"

    adoRemove.Delete

    [Highlight=VB]

    This will delete a the record where the code is 6 - I just need to know how to reference the column in the listview which
    contains the code

    Thanks

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Location
    Home
    Posts
    85

    Okay so i am still not there

    So here we are - I have two columns in the listview

    When removing an item from the database, i need to know the Vacancyid ( from the textbox on the form I dont have a problem with that ) and the bytexpertisecode( from the second column in the listview). I cant find out how to do it, i have searched and searched to no avail
    Please help

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Get the vacancy ID.

    Use the connection object's EXECUTE method to run a DELETE statement against your table, using the vacancy ID.

  5. #5
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697
    If you want the second column value use the below. HTH

    VB Code:
    1. ListView1.SelectedItem.SubItems(1)

  6. #6
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Right...this is dead easy. I didn't read this thread yesterday as I thought someone would have already answered it:
    VB Code:
    1. Private Sub cmdDelete_Click()
    2. Dim adoConn As Connection
    3. Dim lvwItem As ListItem
    4. Dim strSQL As String
    5.     Set lvwItem = ListView1.SelectedItem
    6.     If Not (lvwItem Is Nothing) Then
    7.         Set adoConn = New Connection
    8.         adoConn.Open CONN_STRING
    9.         strSQL = "DELETE * FROM TABLE WHERE ID = " & lvwItem.SubItems(1)
    10.         adoConn.Execute strSQL
    11.         ListView1.ListItems.Remove lvwItem.Index
    12.         Set lvwItem = Nothing
    13.         adoConn.Close
    14.         Set adoConn = Nothing
    15.     End If
    16. End Sub
    Woooof

  7. #7
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Originally posted by Wokawidget
    Right...this is dead easy. I didn't read this thread yesterday as I thought someone would have already answered it:
    VB Code:
    1. Private Sub cmdDelete_Click()
    2. Dim adoConn As Connection
    3. Dim lvwItem As ListItem
    4. Dim strSQL As String
    5.     Set lvwItem = ListView1.SelectedItem
    6.     If Not (lvwItem Is Nothing) Then
    7.         Set adoConn = New Connection
    8.         adoConn.Open CONN_STRING
    9.         strSQL = "DELETE * FROM TABLE WHERE ID = " & lvwItem.SubItems(1)
    10.         adoConn.Execute strSQL
    11.         ListView1.ListItems.Remove lvwItem.Index
    12.         Set lvwItem = Nothing
    13.         adoConn.Close
    14.         Set adoConn = Nothing
    15.     End If
    16. End Sub
    Woooof
    Use stored procedures, you shouldn't be executing SQL on the client side. Too much network traffic going in and out. Have the server do the grunt work.

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    Quick note - it's DELETE FROM, not DELETE * FROM - didn't want you to spend forever debugging a typo...

    Also, I agree with JHermiz - use a SPROC - pass it just the listview value as a parameter and let it do the delete with the parameter.

  9. #9

  10. #10
    Junior Member gotcha_crazy's Avatar
    Join Date
    Jun 2004
    Location
    Cambridge, UK
    Posts
    31
    alf, you reference the second column exactly as you would with the first.

    It is simply :

    strItem=List.List(List.ListIndex)

    this will give you the selected content of whatever column

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  11. #11

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Location
    Home
    Posts
    85

    Wink You people are legends !!!

    Thanks for helping me out with somethign that was driving me crazy - i will have à cold beer waiting for all!!!!
    This is what i am using and it works!

    [/Highlight]
    Dim adoRemove As Recordset

    Dim db As Connection
    Set db = New Connection
    db.CursorLocation = adUseClient
    db.Open "PROVIDER=MSDASQL;dsn=XPHOME;uid=Paul ;pwd=;database=ContractManagement;"

    Set adoRemove = New Recordset

    Dim vacancyid As String
    Dim code As String
    vacancyid = Form7.txtFields(0).Text



    code = ListView2.SelectedItem.SubItems(1)



    'Dim tblrequired As String
    'tablerequired = tblexpertiserequirements

    'adoRemove.Open "DELETE FROM tblexpertiserequirements WHERE lngvacancyid = " & ListView2.SelectedItem.SubItems(1), db, adOpenStatic, adLockOptimistic
    adoRemove.Open "select * from tblexpertiserequirements where lngvacancyid = " & vacancyid, db, adOpenStatic, adLockOptimistic


    adoRemove.Find "bytexpertisecode = '" & code & "'"

    adoRemove.Delete
    adoRemove.Close

    [/Highlight]
    Last edited by altf4; Jul 3rd, 2004 at 04:59 AM.

  12. #12
    at the last
    VB Code:
    1. try
    [/Highlight] instead [Highlight=VB]

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