Results 1 to 4 of 4
  1. #1
    Bkper087 is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    May 2014
    Posts
    81

    Combining " And " and " Or " operators in a Where clause of a VBA SQL statement

    I’ve been working through the creation of a budgeting database (version 4 or 5ish) using VBA code. I asked a related question in the forms previously, but realized after I solved that piece I needed to do it slightly different.



    I have 3 combo boxes at the top of my form (frmEntry) that filter a SQL statement via VBA. I also have four toggle switches that do the same. The trick is trying to get them to work together. The combo boxes all refer to separate fields (Project, Year, and Cost Center). The toggle switches allow the user to show or hide rows of the SQL statement in a subform based on a single field (Budget, Forecast, Actual, etc.).

    The problem I think I have is the toggle switches require a Where clause that uses the “ And “ operator and the toggle switches require the “ Or “ operator. How can I write the code so it recognized both? The code looks like this:

    WhereStatement = " Where " & strtglWhere & strcboWhere

    If I only write like this: WhereStatement = " Where " & strtglWhere
    Or this: WhereStatement = " Where " & strcboWhere

    the related filters (toggles or combo boxes) work on their own. However when i try to combine it I get errors or it just doesnt work at all. I've tried a few different methods, but I think I need some experienced advise here. Thanks in advance.



    Code:
    Option Compare Database
    Option Explicit
    Private SQL As String
    Private strtglWhere As String
    Private strcboWhere As String
    Private WhereStatement As String
    Private Sub EntrySQL()
     
    SQL = "SELECT tblEntry.EntryID, tblEntry.Entry_ProjectCodeIDfk, tblEntry.Entry_CostCenterIDfk, tblEntry.Entry_Description, " _
    & "tblEntry.Entry_ProductSKU, tblEntry.Entry_UnitPrice, tblEntry.Entry_QuantityofUnits, tblEntry.Entry_VendorIDfk, " _
    & "tblEntry.Entry_PurchaseOrderIDfk, tblEntry.Entry_EventYear, tblEntry.Entry_ReceiptDate, tblEntry.Entry_ReceiptNumber, " _
    & "tblEntry.Entry_BAFIDfk, qryEntry.Entry_TotalCost FROM qryEntry " _
    & WhereStatement
     
    Me.frmEntry_sub.Form.RecordSource = SQL
    Me.frmEntry_sub.Form.Requery
     
    End Sub
     
    Private Sub ComboBoxFilter()
    Dim Part1cbo As String
    Dim Part2cbo As String
    Dim Part3cbo As String
     
    If Nz(Me.[cboProjectCode], "") <> "" Then
    Part1cbo = "tblEntry.Entry_ProjectCodeIDfk=" & Me.[cboProjectCode] & " And "
    Else
    Part1cbo = ""
    End If
     
    If Nz(Me.[cboCostCenter], "") <> "" Then
    Part2cbo = "tblEntry.Entry_CostCenterIDfk=" & Me.[cboCostCenter] & " And "
    Else
    Part2cbo = ""
    End If
     
    If Nz(Me.[cboYear], "") <> "" Then
    Part3cbo = "tblEntry.Entry_EventYear=" & Me.[cboYear] & " And "
    Else
    Part3cbo = ""
    End If
     
    strcboWhere = Part1cbo & Part2cbo & Part3cbo
     
    If Nz(strcboWhere, "") <> "" Then
    strcboWhere = Left(strcboWhere, Len(strcboWhere) - 4)
    End If
     
    '''''''''''''''''''''
    Private Sub ToggleFilter()
    Dim Part1tgl As String
    Dim Part2tgl As String
    Dim Part3tgl As String
    Dim Part4tgl As String
     
     
    If tglBudget = True Then
    Part1tgl = "tblEntry.Entry_BAFIDfk=" & 1 & " Or "
    Else
    Part1tgl = ""
    End If
     
    If tglJatasa = True Then
    Part2tgl = "tblEntry.Entry_BAFIDfk=" & 2 & " Or "
    Else
    Part2tgl = ""
    End If
     
    If tglTracked = True Then
    Part3tgl = "tblEntry.Entry_BAFIDfk=" & 3 & " Or "
    Else
    Part3tgl = ""
    End If
     
    If tglForecast = True Then
    Part4tgl = "tblEntry.Entry_BAFIDfk=" & 4 & " Or "
    Else
    Part4tgl = ""
    End If
     
    strtglWhere = Part1tgl & Part2tgl & Part3tgl & Part4tgl
     
    If Nz(strtglWhere, "") <> "" Then
    strtglWhere = Left(strtglWhere, Len(strtglWhere) - 4)
    End If
     
    End Sub
    Private Sub WhereStatementsub()
     
    If strtglWhere <> "" And strcboWhere <> "" Then
    WhereStatement = " Where " & strtglWhere & strcboWhere
     
    End Sub
    ''''''''''''''''''''''''''''''''''''''''''''
    
    Private Sub cboCostCenter_AfterUpdate()
    ComboBoxFilter
    EntrySQL
    End Sub
     
    Private Sub cboProjectCode_AfterUpdate()
    ComboBoxFilter
    EntrySQL
    End Sub
     
    Private Sub cboYear_AfterUpdate()
    ComboBoxFilter
    EntrySQL
    End Sub
    '''''''''''''''''''''''''''''''''''''''''
    Private Sub tglBudget_AfterUpdate()
    ComboBoxFilter
    EntrySQL
    End Sub
     
    Private Sub tglForecast_AfterUpdate()
    ComboBoxFilter
    EntrySQL
    End Sub
     
    Private Sub tglJatasa_AfterUpdate()
    ComboBoxFilter
    EntrySQL
    End Sub
     
    Private Sub tglTracked_AfterUpdate()
    ComboBoxFilter
    EntrySQL
    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Shot in the dark:

    WhereStatement = " Where (" & strtglWhere & ")" & strcboWhere

    This will probably help:

    http://www.baldyweb.com/ImmediateWindow.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Bkper087 is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    May 2014
    Posts
    81
    Here is the code I finally got to work. The big problem is I left out calling a few of the sub procedures during the "on click" events that triggered the filter.
    Code:
    Option Compare Database
    Option Explicit
    Private SQL As String
    Private strtglWhere As String
    Private strcboWhere As String
    Private WhereStatement As String
    Private Sub EntrySQL()
    
    
    SQL = "SELECT tblEntry.EntryID, tblEntry.Entry_ProjectCodeIDfk, tblEntry.Entry_CostCenterIDfk, tblEntry.Entry_Description, " _
    & "tblEntry.Entry_ProductSKU, tblEntry.Entry_UnitPrice, tblEntry.Entry_QuantityofUnits, tblEntry.Entry_VendorIDfk, " _
    & "tblEntry.Entry_PurchaseOrderIDfk, tblEntry.Entry_EventYear, tblEntry.Entry_ReceiptDate, tblEntry.Entry_ReceiptNumber, " _
    & "tblEntry.Entry_BAFIDfk, qryEntry.Entry_TotalCost FROM qryEntry " _
    & WhereStatement
    
    
    Me.frmEntry_sub.Form.RecordSource = SQL
    Me.frmEntry_sub.Form.Requery
    
    
    End Sub
    
    
    Private Sub ComboBoxFilter()
    Dim Part1cbo As String
    Dim Part2cbo As String
    Dim Part3cbo As String
    
    
    If Nz(Me.[cboProjectCode], "") <> "" Then
    Part1cbo = "tblEntry.Entry_ProjectCodeIDfk=" & Me.[cboProjectCode] & " And "
    Else
    Part1cbo = ""
    End If
    
    
    If Nz(Me.[cboCostCenter], "") <> "" Then
    Part2cbo = "tblEntry.Entry_CostCenterIDfk=" & Me.[cboCostCenter] & " And "
    Else
    Part2cbo = ""
    End If
    
    
    If Nz(Me.[cboYear], "") <> "" Then
    Part3cbo = "tblEntry.Entry_EventYear=" & Me.[cboYear] & " And "
    Else
    Part3cbo = ""
    End If
    
    
    strcboWhere = Part1cbo & Part2cbo & Part3cbo
    
    
    If Nz(strcboWhere, "") <> "" Then
    strcboWhere = Left(strcboWhere, Len(strcboWhere) - 4)
    End If
    
    
    End Sub
    
    
    '''''''''''''''''''''
    Private Sub ToggleFilter()
    Dim Part1tgl As String
    Dim Part2tgl As String
    Dim Part3tgl As String
    Dim Part4tgl As String
    
    
    
    
    If tglBudget = True Then
    Part1tgl = "tblEntry.Entry_BAFIDfk=" & 1 & " Or "
    Else
    Part1tgl = ""
    End If
    
    
    If tglJatasa = True Then
    Part2tgl = "tblEntry.Entry_BAFIDfk=" & 2 & " Or "
    Else
    Part2tgl = ""
    End If
    
    
    If tglTracked = True Then
    Part3tgl = "tblEntry.Entry_BAFIDfk=" & 3 & " Or "
    Else
    Part3tgl = ""
    End If
    
    
    If tglForecast = True Then
    Part4tgl = "tblEntry.Entry_BAFIDfk=" & 4 & " Or "
    Else
    Part4tgl = ""
    End If
    
    
    strtglWhere = Part1tgl & Part2tgl & Part3tgl & Part4tgl
    
    
    If Nz(strtglWhere, "") <> "" Then
    strtglWhere = Left(strtglWhere, Len(strtglWhere) - 4)
    End If
    
    
    End Sub
    Private Sub WhereStatementsub()
    
    
    'If Nz(strcboWhere & strtglWhere, "") <> "" Then
    'WhereStatement = " Where " & strcboWhere & strtglWhere
    'Else
    'WhereStatement = ""
    'End If
    
    
    If Len(strcboWhere) >= 1 And Len(strtglWhere) >= 1 Then
    WhereStatement = " Where (" & strcboWhere & ") And (" & strtglWhere & ")"
    ElseIf Len(strcboWhere) >= 1 Then
    WhereStatement = " Where " & strcboWhere
    ElseIf Len(strtglWhere) >= 1 Then
    WhereStatement = " Where " & strtglWhere
    Else
    WhereStatement = ""
    End If
    
    
    End Sub
    ''''''''''''''''''''''''''''''''''''''''''''
    Private Sub Form_Load()
    EntrySQL
    End Sub
    
    
    Private Sub btnResetForm_Click()
    DoCmd.Close acForm, "frmEntry", acSaveYes
    DoCmd.OpenForm "frmEntry", acNormal
    End Sub
    '''''''''''''''''''''''''''''''''''''''''''''
    Private Sub cboCostCenter_AfterUpdate()
        Call ComboBoxFilter
        Call ToggleFilter
        Call WhereStatementsub
        Call EntrySQL
    End Sub
    
    
    Private Sub cboProjectCode_AfterUpdate()
        Call ComboBoxFilter
        Call ToggleFilter
        Call WhereStatementsub
        Call EntrySQL
    End Sub
    
    
    Private Sub cboYear_AfterUpdate()
        Call ComboBoxFilter
        Call ToggleFilter
        Call WhereStatementsub
        Call EntrySQL
    End Sub
    '''''''''''''''''''''''''''''''''''''''''
    Private Sub tglBudget_AfterUpdate()
        Call ComboBoxFilter
        Call ToggleFilter
        Call WhereStatementsub
        Call EntrySQL
    End Sub
    
    
    Private Sub tglForecast_AfterUpdate()
        Call ComboBoxFilter
        Call ToggleFilter
        Call WhereStatementsub
        Call EntrySQL
    End Sub
    
    
    Private Sub tglJatasa_AfterUpdate()
        Call ComboBoxFilter
        Call ToggleFilter
        Call WhereStatementsub
        Call EntrySQL
    End Sub
    
    
    Private Sub tglTracked_AfterUpdate()
        Call ComboBoxFilter
        Call ToggleFilter
        Call WhereStatementsub
        Call EntrySQL
    End Sub
    ''''''''''''''''''''''''''''''''''''''''''
    
    
    Private Sub txtMessage_Click()
    Dim TextMsg As String
    
    
    MsgBox WhereStatement
    
    
    End Sub

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Glad you got it sorted.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Simple table relationships ("faces" to "spaces" to "chairs")
    By skydivetom in forum Database Design
    Replies: 36
    Last Post: 07-20-2019, 01:49 PM
  2. Replies: 12
    Last Post: 10-01-2018, 02:40 PM
  3. Replies: 3
    Last Post: 06-06-2018, 08:26 PM
  4. Replies: 1
    Last Post: 09-07-2015, 08:00 AM
  5. Replies: 1
    Last Post: 09-03-2014, 03:27 AM

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