I have an ASP query form that among other things includes a text box which allows the user to enter some text which can be searched for in a database. Unfortunately, the search only returns a result when there is a match for the exact string that was entered. Is there a way to change this so as to return a result for partial matches as well, or if what's in the DB includes all or part of the search string?
The code is below and the Case in question is the one titled "Nam". Note that I have sort of gotten around this issue by adding an asterix wild card feature, but I'd really just prefer to avoid using the asterixes altogether. I chose to leave the extra code for the wildcards out so as not to complicate things.
Select Case strOption
Case "Nam"
strSelect = strSelect & " Nam='" & UCase(strNam) & "'"
Case "Location"
strSelect = strSelect & " Location='" & UCase(strLocation) & "'"
Case "Typ"
strSelect = strSelect & " Typ='" & UCase(strTyp) & "'"
Case "Season"
strSelect = strSelect & " Season='" & UCase(strSeason) & "'"
Case "Duration"
strSelect = strSelect & " Duration='" & UCase(strDuration) & "'"
Case "Yr"
strSelect = strSelect & " Yr='" & UCase(strYr) & "'"
End Select
It's more about SQL than VBScript. According the Operators docs, any of comparison and logical operators return a Boolean data type with a value of TRUE, FALSE, or UNKNOWN. More important could be reading about building a search condition (a combination of one or more predicates) using operators in question; in brief:
= (equal to) is the operator used to test the equality between two expressions;
LIKE indicates that the subsequent character string is to be used with pattern matching and returns TRUE if the operand matches a pattern (more on valid syntax, escaping etc.); a pattern can include the following valid wildcard characters:
% Any string of zero or more characters;
_ (underscore) Any single character;
[] Any single character within the specified range ([a-f]) or set ([abcdef]);
[^] Any single character not within the specified range ([^a-f]) or set ([^abcdef]).
Thus, you could formulate a predicate on Nam in your search condition (in terms of VBScript) as follows:
strSelect = strSelect & " Nam LIKE '%" & UCase(strNam) & "%'"
Related
I am concatenation hlinks obtained from word do
data = data & "," & Vbcr & hlnk.Address & ":" & hlnk.TextToDisplay
Here ',' is separator.
Now I get every time data starting with ',' (obviously)
I then use
data = Right(data,Len(data)-1)
But I doubted my method of string concatenation.
Am I using Right method of string concatenation in first place?
I have seen ASP classic - how do I join an array of strings / join / implode do not work but I don't think that is my case. I am not joining array but creating one.
The canonical way to avoid leading (or trailing) list separators is to collect the items you want to concatenate in an array, then join that array. That's probably why #Filburt considered your question a (borderline) duplicate. If you don't know the number of items beforehand you'd dynamically resize the array:
ReDim a(-1)
For Each hlnk In ...
ReDim Preserve a(UBound(a)+1)
a(UBound(a)) = hlnk.Address & ":" & hlnk.TextToDisplay
Next
Once the array is filled you simply join the elements:
data = Join(a, "," & vbCr)
Otherwise you need to either handle the first (or last) element differently from the rest:
If IsEmpty(data) Then
data = hlnk.Address & ":" & hlnk.TextToDisplay
Else
data = data & "," & vbCr & hlnk.Address & ":" & hlnk.TextToDisplay
End If
or remove the leading (trailing) separator after you finished constructing the string:
data = Mid(data, 3)
I'm trying to filter datagrid from a textbox it works but not if apostrophe or ' was typed on the textbox, I'm using ADODB and VB6
Public Sub pGetCustomer(Optional vSearch As String)
If vSearch = "'" Then
xRSTree.Filter = adFilterNone
xRSTree.Requery
Else
xRSTree.Filter = "description like '%" & vSearch & "%' or customercode like '%" & vSearch & "%'"
End If
Private Sub txtSearch_KeyPress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub
As it says in the ADO documentation (when did people fall into this weird habit of calling ADO "ADODB" anyway???):
Note To include single quotation marks (') in the filter Value, use two single quotation marks to represent one. For example, to filter on O'Malley, the criteria string should be "col1 = 'O''Malley'". To include single quotation marks at both the beginning and the end of the filter value, enclose the string with pound signs (#). For example, to filter on '1', the criteria string should be "col1 = #'1'#".
You must also consider wildcard rules here:
If Operator is LIKE, Value can use wildcards. Only the asterisk (*) and percent sign (%) wild cards are allowed, and they must be the last character in the string. Value cannot be null.
But a little confusingly:
In a LIKE clause, you can use a wildcard at the beginning and end of the pattern (for example, LastName Like '*mit*'), or only at the end of the pattern (for example, LastName Like 'Smit*').
You need to "escape" your qoutes or single qoutes. Simple way would be to replace in vSearch all ' with '' and all " with "".
Simply I'm trying to define the quotation mark (") as a constant in Visual Basic. However, VB automatically inserts another ", hence I can't make the char equals to an ".
You can also use the ASCII character (34) in Visual Basic like
TextBox1.Text = "She said, " & Chr(34) & "You deserve a treat!" & Chr(34)
You can also define a constant for the character, and use it where needed.
Const quote As String = """"
TextBox1.Text = "She said, " & quote & "You deserve a treat!" & quote
Taken from How to: Put Quotation Marks in a String
Two quote marks make an escaped quote mark.
variable = """"
I have asked this question before but there are no solution to it. I have created a multivalue filter with the following data-set.
SELECT PASS_M, ENTRY_DT, EXIT_DT, WORKED_HRS, ACCESS_LOCATION_X, IC_N, COMPANY_M, CONSECUTIVE_D
FROM TEMP_TARGET
WHERE (CONSECUTIVE_D >= #consecDays) AND (ENTRY_DT BETWEEN #startDate AND #endDate) AND
(ACCESS_LOCATION_X LIKE #accessVar) AND
(IC_N LIKE #icVAr)
It would be relatively easy if the value of my accessVar does not use wildcard but i needed that. So there will be 5 values possible in accessVar:
%(means all), 'At%', 'Bet%', 'Co%' and 'Dea%'
I am unable to use In operator with wildcard. Secondly, can i make this kind of dropdown filters optional? if nth is selected, just query all.
What other options do i have?
You want to use LIKE and multi-value parameters together, which isn't going to work. However, Reporting Services gives us the ability to do almost anything we want. The solution is to use custom code and expressions. First, we change your SQL statement into an expression, like so:
="SELECT PASS_M, ENTRY_DT, EXIT_DT, WORKED_HRS, ACCESS_LOCATION_X, IC_N, "
&"COMPANY_M, CONSECUTIVE_D "
&"FROM TEMP_TARGET "
&"WHERE (CONSECUTIVE_D >= #consecDays) "
&"AND (ENTRY_DT BETWEEN #startDate AND #endDate) "
&"AND ((#accessvar IS NULL) OR (ACCESS_LOCATION_X LIKE #accessVar)) "
&"AND ((#icVar IS NULL) OR (IC_N LIKE #icVAr)) "
So now the SQL statement is actually a string expression that will evaluate to a SQL expression which will execute.
Next we need to convert your multi-value parameter into a series of LIKE statements, which we can do with custom code. Add the following custom code to your report (right-click report, select Properties and click the Code tab):
Function AccessLocations (ByVal parameter As Parameter) AS String
Dim Result As String
If parameter.IsMultiValue then
Result = "AND ( "
For i as integer = 0 to parameter.Count-1
Result = Result + "(ACCESS_LOCATION LIKE '" + CStr(parameter.Value(i)) + "') OR "
Next
Result = Left(Result, Result.Length - 3) +") "
Else
Result = "AND (ACCESS_LOCATION LIKE '" + CStr(parameter.Value) + "') "
End If
Return Result
End Function
Then we call this function as part of the SQL statement:
Code.AccessLocations(Parameters!accessvar)
So your full SQL is:
="SELECT PASS_M, ENTRY_DT, EXIT_DT, WORKED_HRS, ACCESS_LOCATION_X, IC_N, "
&"COMPANY_M, CONSECUTIVE_D "
&"FROM TEMP_TARGET "
&"WHERE (CONSECUTIVE_D >= #consecDays) "
&"AND (ENTRY_DT BETWEEN #startDate AND #endDate) "
& Code.AccessLocations(Parameters!accessvar)
&"AND ((#icVar IS NULL) OR (IC_N LIKE #icVAr)) "
If your parameter has the % wildcard in it then this will work; otherwise add the wildcard to the function.
I have a table in mdb with field address, which will contain street address e.t.c as string.
I want to be able to search part of this record.
sCriteria = "address like " & "'" & streetAddr & "'"
Rs1.Filter = sCriteria
it searches for e.g "Mall" while there is a record "Mall Road" which should have been found but is not as Road is missing, what should i do to make part of field searchable
Try
sCriteria = "address like '*" & Quote(streetAddr) & "*'"
where Quote does a simple Replace(sText, "'", "''")
Put an '*' before the closing single-quote, after the filter value.