Query criteria from multi-select list box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kstevens
    New Member
    • Mar 2009
    • 74

    Query criteria from multi-select list box

    I am trying to pull time records for employees. I have a list box (simple multi select) with there names in the box. I want people to be able to select the employees that they want, and click the view report button. Its not working.

    my code is
    Code:
    Dim VarItem As Variant
    Dim strEmployee As String
    
    strEmployee = """"
    
    For Each VarItem In Me.EmployeeTime.ItemsSelected
        strEmployee = strEmployee & Me.EmployeeTime.ItemData(VarItem)
        strEmployee = strEmployee & """ Or """
    Next VarItem
    
    If Len(strEmployee) = 0 Then
        strEmployee = "Like *"
    Else
        strEmployee = Left(strEmployee, Len(strEmployee) - 5)
    End If
    
    Me.SQLBox = strEmployee
    
    End Sub
    my sqlbox is what i am referring to in the query (i only use the qry wizard, i cant seem to do better, lol)

    My question is... if i manually type
    "Name1" OR "Name2"
    in the crieteria box (in the query wizard, then the query returns the results. The code above produces the same result for the query
    "Name1" OR "Name2"
    but it wont pull results. I did notice that if i put Name1
    (without quotes) it works fine, so i know somehow it is turning the txtbox into a string... how do i stop it from doing that... I tried
    name1" or "name2
    just to see if it would try to turn it into a string, and then separate the OR from the string, but that didnt work. I have tried """ and all kinds of crazy stuff, but no good results yet

    I did read http://www.fontstuff.c om/access/acctut18.htm but it wasnt much help for the query I am trying to build. To be honest, i am certain that the info was there, but i am grouping data, and again, I am not really good at SQL so maybe the answer is there...but i figured the method i was trying was a bit more streamlined, and would work.

    Thanks in advance for any help.
  • kstevens
    New Member
    • Mar 2009
    • 74

    #2
    I got it. I changed the code, and wrote the SQL in code. Here is the code that worked.

    Code:
    Private Sub Command15_Click()
    Dim VarItem As Variant
    Dim strEmployee As String
    Dim sqlWhere As String
    strEmployee = "HAVING (("
    sqlWhere = "(tblAttendanceDateFiltered.EmplName)="
    
    For Each VarItem In Me.EmployeeTime.ItemsSelected
        strEmployee = strEmployee & sqlWhere & "'" & Me.EmployeeTime.ItemData(VarItem)
        strEmployee = strEmployee & "'" & " Or "
    Next VarItem
    
    strEmployee = Left(strEmployee, Len(strEmployee) - 3) & "));"
    
    
    Me.SQLBox = "SELECT tblAttendanceDateFiltered.EmplName, tblAttendanceDateFiltered.EmplCode, Sum(tblAttendanceDateFiltered.Hours) AS SumOfHours, tblAttendanceDateFiltered.StartDate, tblAttendanceDateFiltered.EndDate " & _
    "FROM tblAttendanceDateFiltered " & _
    "GROUP BY tblAttendanceDateFiltered.EmplName, tblAttendanceDateFiltered.EmplCode, tblAttendanceDateFiltered.StartDate, tblAttendanceDateFiltered.EndDate " & _
    strEmployee
    MsgBox SQLBox
    End Sub

    Comment

    Working...