如何在.hta代码中添加日期范围?

时间:2013-04-15 17:20:37

标签: sql-server vbscript hta

我写了一个带有一个搜索参数的hta应用程序。

使用此搜索参数,用户可以按名字或姓氏或两者搜索。

这非常有效。

今天,管理层决定在搜索中添加日期范围。

我尝试构建WHERE子句,用户可以使用姓氏,名字或两者或日期范围进行搜索,但不能同时搜索两者。

换句话说,一方面,用户可以使用名为txtsrch的表单变量按姓氏,名字或两者进行搜索。

或者他们只能使用fromMDY作为fromDate而使用fromMDY作为日期。

到目前为止,它运作得不好。

无论是在搜索名称搜索框中输入还是选择日期范围,都会出现类型不匹配错误。

当它只是一个搜索框时,我没有收到类型不匹配错误。

非常感谢任何帮助。

这是最小的,我认为相关的代码。

日期采用1/1/2013的形式

'*
Const cOPT = "<option value='?'>?</option>"
'*
Dim fromMDY(2)
Dim toMDY(2)
Dim optMDY(2)
optMDY(0) = "<option value='0'></option>"
optMDY(1) = "<option value='0'></option>"
optMDY(2) = "<option value='0'></option>"
Dim i
'*
For i = 1 To 12
  optMDY(0) = optMDY(0) & vbCrLf & Replace(cOPT,"?",i)
Next
For i = 1 To 31
  optMDY(1) = optMDY(1) & vbCrLf & Replace(cOPT,"?",i)
Next
For i = Year(Date)+1 To Year(Date)-4 Step -1
  optMDY(2) = optMDY(2) & vbCrLf & Replace(cOPT,"?",i)
Next

Sub Selected(What)
  Select Case What
  Case "FromMonth"
    fromMDY(0) = FromMonth.Value
  Case "FromDay"
    fromMDY(1) = FromDay.Value
  Case "FromYear"
    fromMDY(2) = FromYear.Value
  Case "ToMonth"
    toMDY(0) = ToMonth.Value
  Case "ToDay"
    toMDY(1) = ToDay.Value
  Case "ToYear"
    toMDY(2) = ToYear.Value
  End Select
End Sub

Sub DisplayDates()
  MsgBox "From:" & vbTab & Join(fromMDY,"/") & vbCrlf _
    & "To:" & vbTab & Join(toMDY,"/")
  End Sub

' first: Do we use AND or OR between clauses in the WHERE?
' AndOr = ANDOR.value
Sub radiocheck()
  for each b in ANDOR
    if b.checked Then AndOr = b.Value
  next
End Sub

' and now build up the WHERE:
where = ""

tsrch = txtsrch.Value

If tsrch <> "" Then
  where = where & " Name = '" & Replace(tsrch,"'","''") & "'"
End If

If fromMDY <> "" AND toMDY<> "" Then
    where = where & " convert(datetime, (left(dispdt,2)  + '/' + substring(dispdt,3,2) + '/' + case when cast(right(dispdt,2) as int) >= 70 then '19' else '20' end + right(dispdt,2)), 101) Between '"& fromMDY &"' AND '"& toMDY &"' "
End If


'Take care of sql injection tactics
SQL_query = "SELECT TOP 1000 Name, Rel, Estno, dtfild, pub, [TYPE OF DOCUMENT] typeofdocument, btyp, bkno, disp, dispdt, PGNO FROM PCS60418_MTHLY_XREF WHERE " _ 
  & where
msgBox sql_query

1 个答案:

答案 0 :(得分:0)

VBScript是不区分大小写的。 ANDORAndOr完全相同,所以如果您这样做:

Sub radiocheck()
    for each b in ANDOR
        if b.checked Then AndOr = b.Value
    next
End Sub

您要将基元分配给AndOr,该基元与ANDORArray类型的Iteratable Object冲突。只需重命名这两个变量中的一个并再试一次。

为了防止将来出现这种错误:使用Option Explicit,使用正确的(一致的)变量命名并使用正确的作用域:尽可能在本地进行,并将变量传递给子程序,而不是将它们用作全局变量。 / p>

相关问题