我从数据库中检索了所有记录,由于将所有记录推入下拉列表,因此有很多重复记录。
代码:
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>")
答案 0 :(得分:0)
您是否可以更改查询以返回不同的年份列表,还是还返回其他数据?
答案 1 :(得分: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这样的东西,它有更快的搜索。
答案 2 :(得分: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
答案 3 :(得分: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")