It seems like this is getting more complicated than it needs to be. I'm not sure why you would need to remove sections of a query. I certainly wouldn't do that. If there's a procedure that creates the query dynamically, I'd just create a new query when the criteria changes rather than try to remove a piece of text.
Can you give a little more detail on how this is going to work? I'm assuming there will be a form where the user will pick desired result fields for the Select clause, and some text boxes or drop downs with the filter criteria for the Where clause. Is that correct?
I've done a couple of programs that are similar to that, so made a quick form with 4 checkboxes to allow users to add up to 4 columns
Name: chkField_1, tag: dbField1, text: Field 1
Name: chkField_2, tag: dbField2, text: Field 2
Name: chkField_3, tag: dbField3, text: Field 3
Name: chkField_4, tag: dbField4, text: Field 4
2 MaskedTextBox controls for the year and order number. These are hardcoded so I didn't use tags.
txtYear
txtOrderNum
And 4 textboxes for additional filters
Name: txtWhere_1, tag: dbfilter1
Name: txtWhere_2, tag: dbfilter2
Name: txtWhere_3, tag: dbfilter3
Name: txtWhere_4, tag: dbfilter4
In an actual program, the tags would be set to the associated column name from the database. This allows you to pass the controls into generic processing functions instead of using a giant list of if/then statements.
Code:
Private Function MakeQuery() As String
' function to create a SQL query from user supplied criteria
' checkboxes and textboxes on the from must have the associated
' data table fieldname set as the tag
' result set column names will be taken from the checkbox text
Dim sb As New System.Text.StringBuilder
Dim strQuery As String = [String].Empty
Try
With sb
.AppendLine("SELECT")
.AppendLine("ordernr [Order number]")
.AppendLine(",offertnr [Quotation number]")
BuildSelectLine(sb, chkField_1)
BuildSelectLine(sb, chkField_2)
BuildSelectLine(sb, chkField_3)
BuildSelectLine(sb, chkField_4)
.AppendLine("FROM")
.AppendLine("tt")
.AppendLine("WHERE 0 = 0")
' if the year textbox has a value, add it to the query
If Not txtYear.Text = "" Then
.AppendLine("AND DATEPART(YEAR,tt.date) = " & txtYear.Text)
End If
BuildWhereLine(sb, txtOrderNum)
BuildWhereLine(sb, txtWhere_1)
BuildWhereLine(sb, txtWhere_2)
BuildWhereLine(sb, txtWhere_3)
BuildWhereLine(sb, txtWhere_4)
End With
strQuery = sb.ToString '.Replace(vbCrLf, " ")
Catch ex As Exception
End Try
Return strQuery
End Function
''' <summary>
''' Adds and element to the SELECT query if checked
''' </summary>
''' <param name="sb">stringbuilder passed in by reference</param>
''' <param name="cb">the checkbox to process</param>
''' <remarks></remarks>
Private Sub BuildSelectLine(ByRef sb As System.Text.StringBuilder, cb As CheckBox)
If cb.Checked Then
sb.AppendLine("," & cb.Tag & " [" & cb.Text & "]")
End If
End Sub
''' <summary>
''' Adds a condition to the WHERE clause if the textbox is not empty
''' </summary>
''' <param name="sb">stringbuilder passed in by reference</param>
''' <param name="txt">user entered filter criteria</param>
''' <remarks></remarks>
Private Sub BuildWhereLine(ByRef sb As System.Text.StringBuilder, txt As TextBox)
If txt.Text <> "" Then
sb.AppendLine("AND " & txt.Tag & " = '" & txt.Text & "'")
End If
End Sub
''' <summary>
''' Adds a condition to the WHERE clause if the maskedtextbox is not empty
''' </summary>
''' <param name="sb">stringbuilder passed in by reference</param>
''' <param name="txt">user entered filter criteria</param>
''' <remarks></remarks>
Private Sub BuildWhereLine(ByRef sb As System.Text.StringBuilder, txt As MaskedTextBox)
If txt.Text <> "" Then
sb.AppendLine("AND " & txt.Tag & " = '" & txt.Text & "'")
End If
End Sub