If you have to apply filters in HP ALM programmatically the following function allows you to escape characters in filter strings, which cause runtime errors if not handled properly.

Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
' @Author - Alexander Bolte
' @ChangeDate - 2015-01-07
' @Description - Adjusting a handed filter condition to comply with functional restrictions of ALM.
' A filter condition must not contain ...
' - spaces without embracing the whole condition with quotation marks.
' - not escaped quotation marks.
' - line breaks.
' This function is cleaning a handed filter condition of the above listed errors.
' @Param cond - A String providing a filter condition to be cleaned for usage in ALM.
' @Returns - a cleaned filter condition as String.
' In case of error an empty String is returned.
Public Function checkCondition(cond) ' As String
	Dim flt ' As String
 
	On Error Resume Next
 
	' Set the filter string which will be returned to temp variable in order to
	' avoid possible errors caused by handing a variable by reference and not
	' by value.
	flt = cond
	flt = Trim(flt)
	' Escape special character quotation mark.
	flt = Replace(flt, """", "\""")
	' Embrace condition with quotation marks in case a condition contains spaces.
	If InStr(1, flt, " ") > 0 Then
		flt = """" & flt & """"
	End If
	' Remove line breaks from filter condition.
	flt = Replace(flt, vbCr, "")
	flt = Replace(flt, vbLf, "")
 
	If Err.Number <> 0 Then
		Err.Clear
		flt = ""
  	End If
 
	checkCondition = flt
End Function