Results 1 to 13 of 13
  1. #1
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123

    Multi Select changing all withing listbox instead of the selected items

    I have the following code attached to an edit button for a listbox
    Code:
    Dim varItm As Variant
     Dim sSQL As String
     sSQL = "UPDATE tblRelief_Allot SET tblRelief_Allot.GrRating = '" & Me.txtRating & "' " & _
    "WHERE tblRelief_Allot.EmpId='" & Me.cboSearchName.Column(0) & "' " & _
    "AND tblRelief_Allot.ReliefCode=" & Me.listFunctions.Column(4, varItm) & ";" 'to ammend remove , varItem
    I have tried multiselect simple and also extended


    instead of editing the items I have selected it is changing all the items that are in the listbox

    any ideas?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,622
    You want to run an UPDATE against the records selected in listbox? Need loop code that reads each item of the list box and if it is selected, do something with it.

    1. within the loop, run the UPDATE action on selected item

    2. within the loop, construct a WHERE clause variable then use the variable outside the loop in the UPDATE sql.

    Code to loop through listbox, review: http://allenbrowne.com/ser-50.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    My code now looks like "Below" but it is still changing everything

    Code:
    Dim varItm As Variant
     Dim sSQL As String
     sSQL = "UPDATE tblRelief_Allot SET tblRelief_Allot.GrRating = '" & Me.txtRating & "' " & _
    "WHERE tblRelief_Allot.EmpId='" & Me.cboSearchName.Column(0) & "' " & _
    "AND tblRelief_Allot.ReliefCode=" & Me.listFunctions.Column(4, varItm) & ";" 
     For Each varItm In Me.listFunctions.ItemsSelected
            CurrentDb.Execute (sSQL)
            Next

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,622
    Debug. Refer to link at bottom of my post for guidelines. Set breakpoint and Debug.Print. Is the sSQL string construct correct?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    I think the construction is correct as if I simply have "Me.listFunctions.Column(4)" instead of "Me.listFunctions.Column(4, varItm)"
    the code works but only for the last item that was selected

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,622
    Right, construct the sql string and run the Execute within the loop.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    Ok I Did this, I made a breakpoint at the sql statement.
    the funnything is I can see it looping the same amount of times as items selected
    ie. If I selected 2 items it would loop through the SQL Statement twice
    and if i selected 4 items it would loop through 4 times.
    but it is physically changing all?
    How is this even possible?

    here is my code now
    Code:
     For Each varItm In Me.listFunctions.ItemsSelected ' Selecteditem
                Stop
                sSQL = "UPDATE tblRelief_Allot SET tblRelief_Allot.GrRating = '" & Me.txtRating & "' " & _
                "WHERE tblRelief_Allot.EmpId='" & Me.cboSearchName.Column(0) & "' " & _
                "AND tblRelief_Allot.ReliefCode=" & Me.listFunctions.Column(4, varItem) & ";" 'to ammend remove , varItem
                    
                    CurrentDb.Execute (sSQL)
                    Next

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,622
    That is odd. Is the compiled sql statement for each loop correct?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    how would I Create a "Where Variable" in the same code as code above?

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,622
    You are already constructing the WHERE clause in the code. The sql should update only the records that match the criteria. I don't understand why it does otherwise.

    If you want to instead construct the multiple value string and use the IN operator, Allen Browne code shows that.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    sorry Im not sure how I would do that with this code. I am a complete novice.
    Could you please construct for My SQL so I can see in action?

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,622
    I am not sure it will solve the issue. Really need to understand why the existing code fails.

    If you want to provide db for analysis, follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  13. #13
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Looks like you are updating one record, the record that equals a combo. I will guess you combo value is not changing as you loop through the selections in your listbox

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 16
    Last Post: 03-31-2020, 10:19 AM
  2. Selecting items in multi-select list box
    By chronister in forum Access
    Replies: 3
    Last Post: 08-25-2013, 03:33 AM
  3. Replies: 1
    Last Post: 03-05-2013, 06:53 PM
  4. multi select listbox
    By crowegreg in forum Forms
    Replies: 3
    Last Post: 07-28-2012, 01:48 PM
  5. Selected items in listbox
    By tomodachi in forum Access
    Replies: 1
    Last Post: 09-09-2010, 01:14 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
  •  
Other Forums: Microsoft Office Forums