0

我从数据库中检索了所有记录,由于将所有记录推入下拉列表,因此有很多重复项。

代码:

SQL = "SELECT * FROM insite.holiday where calendar_Type= '" &sCalendarType& "' order by date_holiday"

不使用 distinct 的原因是此查询需要其他字段。

前:

2013,2013,2013,2013,..... 2014,2014... 2015,2015,2015...

我想过滤下拉列表中的记录成为:

2013,2014,2015

我知道它在循环之间需要一个条件,但我无法弄清楚。

请帮忙。谢谢

下面是我的代码:

response.write(" <select name=""year1"">")
Do While not rs.eof
    nDateHoliday = rs("date_holiday")
    Ndate= year(nDateHoliday)
    response.write("<option value=" & Ndate & ">" & Ndate & "</option>" & vbCrLf)
    rs.MoveNext()
Loop
response.write("</select>")
4

4 回答 4

0

您能否更改查询以返回不同的年份列表,或者它是否还返回其他数据?

于 2013-06-17T09:28:20.270 回答
0

我更喜欢将不同的值简单地存储在动态数组中,在每次迭代中检查当前项尚不存在:

Dim arrDates(), x, blnExists
ReDim arrDates(-1)
Do While not rs.eof
    nDateHoliday = rs("date_holiday")
    Ndate= year(nDateHoliday)
    blnExists = False
    For x=0 To UBound(arrDates)
        If arrDates(x)=Ndate Then
            blnExists = True
            Exit For
        End If
    Next
    If Not(blnExists) Then
        ReDim Preserve arrDates(UBound(arrDates) + 1)
        arrDates(UBound(arrDates)) = Ndate
    Next
    rs.MoveNext()
Loop
rs.Close
response.write(" <select name=""year1"">")
For x=0 To UBound(arrDates)
    response.write("<option value=""" & arrDates(x) & """>" & arrDates(x) & "</option>" & vbCrLf)
Next
response.write("</select>")
Erase arrDates

注意:如果你有很多记录rs,例如超过 100 条,你最好使用 Dictionary 之类的东西,它的搜索速度更快。

于 2013-06-17T10:54:32.073 回答
0

正如影子向导所建议的那样,您可以使用字符串来代替使用数组来收集年份:

Dim sYears, iTmp
sYears = ""
Do while not rs.Eof
    iTmp = CStr(Year(rs("date_holiday")))
    If (InStr(sYear, "," & iTmp) = -1) Then
        sYears = sYears & "," & iTmp
    End If
    rs.MoveNext
Loop
If (Len(sYears) >= 1) Then
    sYears = Mid(sYears, 2)
End If
Dim arrYears
arrYears = Split(sYears, ",")
' print array
于 2013-06-17T13:18:18.690 回答
0

谢谢你们的辛勤工作,我终于解决了我的问题,再次感谢..

干杯

Dim arrDates(), x, blnExists



Dim arrYear


arrYear = rs.GetRows()



Set objDictionary = CreateObject("Scripting.Dictionary")



For Each strItem in arrYear

    If Not objDictionary.Exists(strItem) Then
        objDictionary.Add strItem, strItem


    End If

Next

intItems = objDictionary.Count - 1

ReDim arrItems(intItems)

i = 0



For Each strKey in objDictionary.Keys
    arrItems(i) = strKey
    i = i + 1
Next


'display out'
    response.write(" <select name=""year4"">")

For Each strItem in arrItems


   response.write("<option value=" & strItem & ">" & strItem & "</option>" & vbCrLf)
Next

            response.write("</select>")
yearReq4=Request.Form("year4")
于 2013-06-19T08:28:38.697 回答