Skip to main content
Corrección en código y explicación
Origen Enlace
Private Function filteredQuery() As String

    Dim sql_ As String
    Dim divisionSelected_ As Boolean = False
    Dim areaSelected_ As Boolean = False
    Dim paisSelected_ As Boolean = False
    Dim versionSelected_ As Boolean = False
    Dim mesSelected_ As Boolean = False
    Dim anioFiscalSelected_ As Boolean = False
    Dim counter_ As Integer = 0

    If Cb_Divisiones.Text <> "" Then
        divisionSelected_ = True
        counter_ += 1
    End If

    If Cb_Area.Text <> "" Then
        areaSelected_ = True
        counter_ += 1
    End If

    If Cb_Paises.Text <> "" Then
        paisSelected_ = True
        counter_ += 1
    End If

    If Cb_Versiones.Text <> "" Then
        versionSelected_ = True
        counter_ += 1
    End If

    If Cb_Mes.Text <> "" Then
        mesSelected_ = True
        counter_ += 1
    End If

    If Cb_AnioFiscal.Text <> "" Then
        anioFiscalSelected_ = True
        counter_ += 1
    End If

    sql_ = filteredQuery2(divisionSelected_, areaSelected_, paisSelected_, versionSelected_, mesSelected_, anioFiscalSelected_, counter_)

    Return sql_

End Function

Private Function filteredQuery2filteredQuery(divisionSelected_ As Boolean, areaSelected_ As Boolean, paisSelected_ As Boolean, _
                                versionSelected_ As Boolean, mesSelected_ As Boolean, anioFiscalSelected_ As Boolean, counter_ As Integer)

    Dim sql_ As String = "SELECT * FROM Registros WHERE "

    If Cb_Divisiones.Text <> "" Then
        sql_ += "Division = '" & Cb_Divisiones.Text & "' AND "
    End If
    If Cb_Area.Text <> "" Then
        sql_ += "Area = '" & Cb_Area.Text & "' AND "
    End If
    If Cb_Paises.Text <> "" Then
        sql_ += "Pais = '" & Cb_Paises.Text & "' AND "
    End If
    If Cb_Versiones.Text <> "" Then
        sql_ += "Version = '" & Cb_Versiones.Text & "' AND "
    End If
    If Cb_Mes.Text <> "" Then
        sql_ += "Mes = '" & Cb_Mes.Text & "' AND "
    End If
    If Cb_AnioFiscal.Text <> "" Then
        sql_ += "AnioFiscal = '" & Cb_AnioFiscal.Text & "' AND "
    End If
    'End If

    sql_ += "Enabled = '" & True & "'"

    Return sql_

End Function

Con esto puedo comenzar la query pidiendo que me traiga todos los registros que coincidan con las condiciones que pongo en el where. Luego por cada uno de los combos, si tiene texto (no importa el tipo de texto, simplemente un string de caracteres), lo incluyo en la query con un AND al final para poder seguir concatenando. Como en mi tabla tengo una columna llamada Enabled para controlar si el registro ha sido o no eliminado de la aplicación en algún momento, al final de la query incluyo siempre la condición

sql_ += "Enabled = '" & True & "'"

y devuelvo la query ya montada.

Private Function filteredQuery() As String

    Dim sql_ As String
    Dim divisionSelected_ As Boolean = False
    Dim areaSelected_ As Boolean = False
    Dim paisSelected_ As Boolean = False
    Dim versionSelected_ As Boolean = False
    Dim mesSelected_ As Boolean = False
    Dim anioFiscalSelected_ As Boolean = False
    Dim counter_ As Integer = 0

    If Cb_Divisiones.Text <> "" Then
        divisionSelected_ = True
        counter_ += 1
    End If

    If Cb_Area.Text <> "" Then
        areaSelected_ = True
        counter_ += 1
    End If

    If Cb_Paises.Text <> "" Then
        paisSelected_ = True
        counter_ += 1
    End If

    If Cb_Versiones.Text <> "" Then
        versionSelected_ = True
        counter_ += 1
    End If

    If Cb_Mes.Text <> "" Then
        mesSelected_ = True
        counter_ += 1
    End If

    If Cb_AnioFiscal.Text <> "" Then
        anioFiscalSelected_ = True
        counter_ += 1
    End If

    sql_ = filteredQuery2(divisionSelected_, areaSelected_, paisSelected_, versionSelected_, mesSelected_, anioFiscalSelected_, counter_)

    Return sql_

End Function

Private Function filteredQuery2(divisionSelected_ As Boolean, areaSelected_ As Boolean, paisSelected_ As Boolean, _
                                versionSelected_ As Boolean, mesSelected_ As Boolean, anioFiscalSelected_ As Boolean, counter_ As Integer)

    Dim sql_ As String = "SELECT * FROM Registros WHERE "

    If Cb_Divisiones.Text <> "" Then
        sql_ += "Division = '" & Cb_Divisiones.Text & "' AND "
    End If
    If Cb_Area.Text <> "" Then
        sql_ += "Area = '" & Cb_Area.Text & "' AND "
    End If
    If Cb_Paises.Text <> "" Then
        sql_ += "Pais = '" & Cb_Paises.Text & "' AND "
    End If
    If Cb_Versiones.Text <> "" Then
        sql_ += "Version = '" & Cb_Versiones.Text & "' AND "
    End If
    If Cb_Mes.Text <> "" Then
        sql_ += "Mes = '" & Cb_Mes.Text & "' AND "
    End If
    If Cb_AnioFiscal.Text <> "" Then
        sql_ += "AnioFiscal = '" & Cb_AnioFiscal.Text & "' AND "
    End If
    'End If

    sql_ += "Enabled = '" & True & "'"

    Return sql_

End Function
    Private Function filteredQuery()

    Dim sql_ As String = "SELECT * FROM Registros WHERE "

    If Cb_Divisiones.Text <> "" Then
        sql_ += "Division = '" & Cb_Divisiones.Text & "' AND "
    End If
    If Cb_Area.Text <> "" Then
        sql_ += "Area = '" & Cb_Area.Text & "' AND "
    End If
    If Cb_Paises.Text <> "" Then
        sql_ += "Pais = '" & Cb_Paises.Text & "' AND "
    End If
    If Cb_Versiones.Text <> "" Then
        sql_ += "Version = '" & Cb_Versiones.Text & "' AND "
    End If
    If Cb_Mes.Text <> "" Then
        sql_ += "Mes = '" & Cb_Mes.Text & "' AND "
    End If
    If Cb_AnioFiscal.Text <> "" Then
        sql_ += "AnioFiscal = '" & Cb_AnioFiscal.Text & "' AND "
    End If
    'End If

    sql_ += "Enabled = '" & True & "'"

    Return sql_

End Function

Con esto puedo comenzar la query pidiendo que me traiga todos los registros que coincidan con las condiciones que pongo en el where. Luego por cada uno de los combos, si tiene texto (no importa el tipo de texto, simplemente un string de caracteres), lo incluyo en la query con un AND al final para poder seguir concatenando. Como en mi tabla tengo una columna llamada Enabled para controlar si el registro ha sido o no eliminado de la aplicación en algún momento, al final de la query incluyo siempre la condición

sql_ += "Enabled = '" & True & "'"

y devuelvo la query ya montada.

Origen Enlace

Solución final en base a la idea que me ha dado @gbianchi:

Private Function filteredQuery() As String

    Dim sql_ As String
    Dim divisionSelected_ As Boolean = False
    Dim areaSelected_ As Boolean = False
    Dim paisSelected_ As Boolean = False
    Dim versionSelected_ As Boolean = False
    Dim mesSelected_ As Boolean = False
    Dim anioFiscalSelected_ As Boolean = False
    Dim counter_ As Integer = 0

    If Cb_Divisiones.Text <> "" Then
        divisionSelected_ = True
        counter_ += 1
    End If

    If Cb_Area.Text <> "" Then
        areaSelected_ = True
        counter_ += 1
    End If

    If Cb_Paises.Text <> "" Then
        paisSelected_ = True
        counter_ += 1
    End If

    If Cb_Versiones.Text <> "" Then
        versionSelected_ = True
        counter_ += 1
    End If

    If Cb_Mes.Text <> "" Then
        mesSelected_ = True
        counter_ += 1
    End If

    If Cb_AnioFiscal.Text <> "" Then
        anioFiscalSelected_ = True
        counter_ += 1
    End If

    sql_ = filteredQuery2(divisionSelected_, areaSelected_, paisSelected_, versionSelected_, mesSelected_, anioFiscalSelected_, counter_)

    Return sql_

End Function

Private Function filteredQuery2(divisionSelected_ As Boolean, areaSelected_ As Boolean, paisSelected_ As Boolean, _
                                versionSelected_ As Boolean, mesSelected_ As Boolean, anioFiscalSelected_ As Boolean, counter_ As Integer)

    Dim sql_ As String = "SELECT * FROM Registros WHERE "

    If Cb_Divisiones.Text <> "" Then
        sql_ += "Division = '" & Cb_Divisiones.Text & "' AND "
    End If
    If Cb_Area.Text <> "" Then
        sql_ += "Area = '" & Cb_Area.Text & "' AND "
    End If
    If Cb_Paises.Text <> "" Then
        sql_ += "Pais = '" & Cb_Paises.Text & "' AND "
    End If
    If Cb_Versiones.Text <> "" Then
        sql_ += "Version = '" & Cb_Versiones.Text & "' AND "
    End If
    If Cb_Mes.Text <> "" Then
        sql_ += "Mes = '" & Cb_Mes.Text & "' AND "
    End If
    If Cb_AnioFiscal.Text <> "" Then
        sql_ += "AnioFiscal = '" & Cb_AnioFiscal.Text & "' AND "
    End If
    'End If

    sql_ += "Enabled = '" & True & "'"

    Return sql_

End Function

Gracias por la ayuda!!