first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

VB Script and ASP Code: basCriteria(); Building Dynamic HTML Based on Variable Form Input

Function basCriteria(vSQL,vDBField,vComparison,vFormValue,vDataType) 'Based on http://15seconds.com/issue/010629.htm.

Dim vValue,vArrayVal,vArray,vClauseStyle,vConnector
Dim vWildCard,vStrChar
    
'ADO overrides the Microsoft Access MS-SQL wildcard ('*') and text delimiter ("):
vWildCard = "%"
vStrChar = Chr(39)

basCriteria = vSQL
vValue = vFormValue
If Len(vValue) = 0 then Exit Function 

vComparison = Trim(vComparison)
vDataType = Trim(vDataType)
vClauseStyle = Ucase(vComparison & ":" & vDataType)
      
Select Case vClauseStyle
Case "=:CHR"
    vValue = vStrChar & Replace(vValue,vStrChar,vStrChar & vStrChar) & vStrChar
          
Case "=:NUM", ">:NUM", "<:NUM"
    If (Not IsNumeric(vValue)) Then vValue = 0
          
Case "IN:CHR"
    vValue=Replace(vValue,", ",",")
    vValue=Replace(vValue,vStrChar,vStrChar & vStrChar)
    vValue="(" & vStrChar & Replace(vValue,",",vStrChar & "," & vStrChar) & vStrChar & ")"
      
Case "IN:NUM"
    vValue=""
    For Each vArrayVal In vArray
      If IsNumeric(vArrayVal) Then vValue = vValue & "," & Trim(vArrayVal)
    Next

    If Len(vValue) < 2 Then Exit Function 
    vValue = Mid(vValue,2) 'Remove leading comma.
    vValue = "(" & vValue & ")"
                  
Case "*:CHR" 'Containing:
    vComparison = " LIKE "
    vValue = vStrChar & vWildCard & Replace(vValue,vStrChar,vStrChar & vStrChar) & vWildCard & vStrChar

Case "=*:CHR" 'Starting with:
    vComparison = " LIKE "
    vValue = vStrChar & vWildCard & Replace(vValue,vStrChar,vStrChar & vStrChar) & vStrChar

Case "*=:CHR" 'Ending with:
    vComparison = " LIKE "
    vValue = vStrChar & Replace(vValue,vStrChar,vStrChar & vStrChar) & vWildCard & vStrChar

Case Else
    Err.Raise 1,"Function basCriteria", _
        ("Missing case for '" & vClauseStyle & "'")
End Select

vConnector=" WHERE " 'For first criterion only.
If 0 < Instr(1,vSQL,"WHERE") Then vConnector = " AND " 

basCriteria = vSQL & vConnector & vDBField & vComparison & vValue          

End Function

mod date: 2001-09-07T02:52:52.000Z