How can edit the code to show result in query (1 Viewer)

zezo2021

Member
Local time
Today, 14:44
Joined
Mar 25, 2021
Messages
381
Code:
Private Sub Command2_Click()
On Error Resume Next
StrListItem_ = ""
Dim ctl
Set ctl = Me!List0
Dim i As Integer
                      
                      For Each varItem In ctl.ItemsSelected
                        StrListItem_ = StrListItem_ & "," & Me.List0.ItemData(varItem)
                      Next varItem
 
End Sub

Public StrListItem_ As String

Public Function SetListItem() As String
SetListItem = Right(StrListItem_, Len(StrListItem_) - 1)
End Function


Code:
SELECT *
FROM Table1
WHERE (((Table1.MName) In (SetListItem())));
 

cheekybuddha

AWF VIP
Local time
Today, 13:44
Joined
Jul 21, 2014
Messages
2,280
Code:
Private Sub Command2_Click()

  Dim selItems As String, varItem As Variant
  Dim strSQL As String

  With Me.List0
    For Each varItem In .ItemsSelected
      selItems = selItems & ",'" & Replace(.ItemData(varItem), "'", "''") & "'"
    Next varItem
  End With
  If Len(selItems) Then
    selItems = Mid(selItems, 2)
    strSQL = "SELECT * FROM Table1 WHERE MName IN (" & selItems & ");"
    Debug.Print strSQL
    With CurrentDb.OpenRecordset(strSQL)
      Do While Not .EOF
        ' Do something with the record eg
        Debug.Print rs.Fields(0), rs.Fields(1), rs.Fields(3), rs.Fields("MName")
        .MoveNext
      Loop
      .Close
    End With
  Else
    ' No items selected in List0
  End If

End Sub
 

zezo2021

Member
Local time
Today, 14:44
Joined
Mar 25, 2021
Messages
381
Code:
Private Sub Command2_Click()

  Dim selItems As String, varItem As Variant
  Dim strSQL As String

  With Me.List0
    For Each varItem In .ItemsSelected
      selItems = selItems & ",'" & Replace(.ItemData(varItem), "'", "''") & "'"
    Next varItem
  End With
  If Len(selItems) Then
    selItems = Mid(selItems, 2)
    strSQL = "SELECT * FROM Table1 WHERE MName IN (" & selItems & ");"
    Debug.Print strSQL
    With CurrentDb.OpenRecordset(strSQL)
      Do While Not .EOF
        ' Do something with the record eg
        Debug.Print rs.Fields(0), rs.Fields(1), rs.Fields(3), rs.Fields("MName")
        .MoveNext
      Loop
      .Close
    End With
  Else
    ' No items selected in List0
  End If

End Sub

Thanks


(y)(y)(y)(y)(y)(y)(y)(y)(y)(y)
 

Users who are viewing this thread

Top Bottom