• Adding a WHERE clause to query SQL in VBA (2002)

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Adding a WHERE clause to query SQL in VBA (2002)

    Author
    Topic
    #420857

    Ok..I have an existing crosstab query, from which I would like to be able to filter for one or more of the records that it is grouped by. I would like to apply this filter from selections in a listbox. Below is the SQL for my query, and below that is the code I’ve got started but dont know where to go next from my listbox selections. I want to filter my crosstab for the location(s) selected in the list box. If I can do this through the query’s design view, that would probably be much easier.

    The query’s SQL:
    SELECT qryLocationCompany.LOCATION, qryLocationCompany.COMPANY, Val(NZ([QRYLOCBYCO].[MO],0)) AS MO, Val(NZ([QRYLOCBYCO].[ME],0)) AS ME, Val(NZ([QRYLOCBYCO].[NO],0)) AS [NO], Val(NZ([QRYLOCBYCO].[NE],0)) AS NE, Val(NZ([QRYLOCBYCO].[AO],0)) AS AO, Val(NZ([QRYLOCBYCO].[AE],0)) AS AE, Val(NZ([QRYLOCBYCO].[Total Of SSN],0)) AS [Total Of SSN]
    FROM qryLocationCompany LEFT JOIN qryLocByCo ON (qryLocationCompany.LOCATION = qryLocByCo.Location) AND (qryLocationCompany.COMPANY = qryLocByCo.Company)
    GROUP BY qryLocationCompany.LOCATION, qryLocationCompany.COMPANY, Val(NZ([QRYLOCBYCO].[MO],0)), Val(NZ([QRYLOCBYCO].[ME],0)), Val(NZ([QRYLOCBYCO].[NO],0)), Val(NZ([QRYLOCBYCO].[NE],0)), Val(NZ([QRYLOCBYCO].[AO],0)), Val(NZ([QRYLOCBYCO].[AE],0)), Val(NZ([QRYLOCBYCO].[Total Of SSN],0));

    The code I have started:
    Private Sub cmdViewReport_Click()
    Dim varItem As Variant
    Dim strSQL As String

    If Me.lstLocations.ItemsSelected.Count = 0 Then
    MsgBox “Please select one or more fields.”, vbExclamation
    Me.lstLocations.SetFocus
    Exit Sub
    End If
    For Each varItem In Me.lstLocations.ItemsSelected
    ‘edit SQL to apply Location filter here
    ‘create SQL of the query

    CurrentDb.QueryDefs(“qryReportAll”).SQL = strSQL
    DoCmd.OpenQuery “qryReportAll”, acViewNormal, acReadOnly

    End Sub

    Viewing 0 reply threads
    Author
    Replies
    • #954247

      See for example post 465456 for a method to construct a WHERE part from a multi-select list box in code.

    Viewing 0 reply threads
    Reply To: Adding a WHERE clause to query SQL in VBA (2002)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: