Solved Loop Through List view Selected Items In Access Form (1 Viewer)

ahmedjamalaboelez

Ahmed J. Aboelez
Local time
Today, 15:23
Joined
Feb 25, 2015
Messages
79
I Appreciate Your help
i have list view with records , when I'm trying to loop through selected items return error
please help to loop through listview selected items

Code:
Private Sub btn_Hold_Click()
    ServerConOpen
    Dim cmd    As New ADODB.Command
    cmd.ActiveConnection = cnx
    cmd.CommandType = adCmdText
    Set lvwList = Me.list_search.Object
    Dim i
       For Each i In lvwList.SelectedItem
       With lvwList
            TempVars!ListText = .SelectedItem.Text
            TempVars!ListIndex = .SelectedItem.Index
            cmd.CommandText = "Update db_devices " & _
                              "SET device_status ='Hold' " & _
                              "WHERE device_lineid =" & TempVars!ListText & ""
                              cmd.Execute
           .ListItems.Item(CInt(TempVars!ListIndex)).ListSubItems(6).Text = "Hold"
           End With
        Next i
    ServerConClose
End Sub
1236.png
 

June7

AWF VIP
Local time
Today, 14:23
Joined
Mar 9, 2014
Messages
5,475
I think those are properties of a listbox, not listview. Consider:
Code:
Private Sub btnList_Click()
    Dim Item As Object
    For Each Item In Me.ListView1.ListItems
        If Item.Checked Then
            Debug.Print Item.Index
        End If
    Next
End Sub
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 23:23
Joined
Sep 21, 2011
Messages
14,317
Have you looked to see if that is even offered in intellisense?
 

June7

AWF VIP
Local time
Today, 14:23
Joined
Mar 9, 2014
Messages
5,475
Okay, new info.

Intellisense doesn't show ListItems property for listview control but it is valid.

Listview control has property InSelection

Declared and Set listview object variable has property SelectedItem

Can't get either to work.

Correction: listbox property is ItemsSelected
 
Last edited:

ahmedjamalaboelez

Ahmed J. Aboelez
Local time
Today, 15:23
Joined
Feb 25, 2015
Messages
79
Okay, new info.

Listview control has property InSelection

Declared and Set listview object variable has property SelectedItem

Can't get either to work.

Correction: listbox property is ItemsSelected
I think those are properties of a listbox, not listview. Consider:
Code:
Private Sub btnList_Click()
    Dim Item As Object
    For Each Item In Me.ListView1.ListItems
        If Item.Checked Then
            Debug.Print Item.Index
        End If
    Next
End Sub
Thank You Soooooooo Much
Your Code Run Like Magic
Here is your Code in sub

Code:
Private Sub btn_Hold_Click()
    ServerConOpen
    Dim cmd    As New ADODB.Command
    cmd.ActiveConnection = cnx
    cmd.CommandType = adCmdText
    Dim Item   As Object
    For Each Item In list_search.ListItems
        If Item.Checked Then
            TempVars!ListText = Item.Text
            TempVars!ListIndex = Item.Index
            cmd.CommandText = "Update db_devices " & _
                              "SET device_status ='Hold' " & _
                              "WHERE device_lineid =" & TempVars!ListText & ""
            cmd.Execute
            Item.ListSubItems(5).Text = "Hold"
        End If
    Next
    ServerConClose
End Sub

@June7 Thank You So Much 🌹🌹🌹🌹🌹

 

Gasman

Enthusiastic Amateur
Local time
Today, 23:23
Joined
Sep 21, 2011
Messages
14,317
You have to use the correct words, you just cannot make them up as you go along.:)
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:23
Joined
Sep 21, 2011
Messages
14,317
Oh, I am not a professional. There are a few here who are dabblers like myself. I do not even use Access much any more as I am retired, and when in work, doing pretty much the same as you, it was not my full time job, just something that would make life easier.

I still use Excel in the same way though.
In fact I thought my signature made that clear? :unsure:
 

June7

AWF VIP
Local time
Today, 14:23
Joined
Mar 9, 2014
Messages
5,475
Glad you have a solution. Here's how SelectedItem works - not with checks, click anywhere but checkbox:
Code:
Private Sub ListView1_ItemClick(ByVal Item As Object)
Debug.Print Item.Index
Debug.Print Me.ListView1.SelectedItem.ListSubItems(3)
End Sub
Turns out InSelection has nothing to do with selection items, it deals with determining if a control has been selected in design view. Why would I ever need to know that programmatically🙄?

My background is very similar to what Gasman described for theirs.
 

June7

AWF VIP
Local time
Today, 14:23
Joined
Mar 9, 2014
Messages
5,475
Definitely not a pro. About 2 years of my job was mostly dedicated to developing a major db for the laboratory I worked for then managing it for another 5 (and a few small ones as well). That's how I learned Access - on the job. Retired 7 years ago. I just like programming, logic exercise, solving puzzle.
 

ahmedjamalaboelez

Ahmed J. Aboelez
Local time
Today, 15:23
Joined
Feb 25, 2015
Messages
79
Glad you have a solution. Here's how SelectedItem works - not with checks, click anywhere but checkbox:
Code:
Private Sub ListView1_ItemClick(ByVal Item As Object)
Debug.Print Item.Index
Debug.Print Me.ListView1.SelectedItem.ListSubItems(3)
End Sub
Turns out InSelection has nothing to do with selection items, it deals with determining if a control has been selected in design view. Why would I ever need to know that programmatically🙄?

My background is very similar to what Gasman described for theirs.
yes i got it 🌹 🌹
Thanks
 

Users who are viewing this thread

Top Bottom