Udvælg poster i database med fra og til bestemte datoer
Jeg har brug for en søgemaskine i asp, der kan udvælge poster i en access database, fra en bestemt dato til en anden dato, altså f.eks fra 02-03-2003 til 15-06-2003
'Variables Dim objCn 'ADO DB connection object Dim objRs 'ADO DB recordset object Dim blnWhere 'True/False for have WHERE in sql already Dim intRecord 'Current record for paging recordset Dim intPage 'Requested page Dim intPageSize 'Requested pagesize Dim sql 'Dynamic sql query string
'Create objects Set objCn = Server.CreateObject("ADODB.Connection") Set objRs = Server.CreateObject("ADODB.Recordset")
'-Get/set requested page intPage = MakeLong(Request("page")) If intPage < 1 Then intPage = 1
'-Get/set requested pagesize If IsEmpty(Request("pagesize")) Then 'Set to default intPageSize = DEF_PAGESIZE Else intPageSize = MakeLong(Request("pagesize")) 'Make sure it fits our min/max requirements If intPageSize < MIN_PAGESIZE Then intPageSize = MIN_PAGESIZE ElseIf intPageSize > MAX_PAGESIZE Then intPageSize = MAX_PAGESIZE End If End If
'-Build dynamic sql sql = "SELECT ID, Sku, Title, date FROM Products "
'--ID (exact search only) If Not IsEmpty(Request("id")) Then If IsNumeric(Request("id")) Then 'Add to query 'First item so we don't have to test for WHERE blnWhere = True 'Set where to true sql = sql & "WHERE " sql = sql & "(ID = " & CStr(CLng(Request("id"))) & ") " End If End If
'--Sku (partial and exact search) If Not IsEmpty(Request("sku")) Then Dim strSku strSku = Trim(Request("sku"))
If strSku <> "" Then 'Test for WHERE If blnWhere Then sql = sql & "AND " Else sql = sql & "WHERE " : blnWhere = True
If (Left(strSku, 1) = "*" And Len(strSku) > 1) Then 'Partial search sql = sql & "(Sku LIKE '%" & Replace(Mid(strSku, 2), "'", "''") & "') " ElseIf (Right(strSku, 1) = "*" And Len(strSku) > 1) Then 'Partial search sql = sql & "(Sku LIKE '" & Replace(Mid(strSku, 1, Len(strSku)-1), "'", "''") & "%') " Else 'Exact match sql = sql & "(Sku = '" & Replace(strSku, "'", "''") & "') " End If
End If End If
'--Title (parital search only) If Not IsEmpty(Request("title")) Then Dim strTitle strTitle = Trim(Request("title"))
If strTitle <> "" Then 'Test for WHERE If blnWhere Then sql = sql & "AND " Else sql = sql & "WHERE " : blnWhere = True sql = sql & "(Title LIKE '%" & Replace(strTitle, "'", "''") & "%') " End If End If
'--date (minimum) If Not IsEmpty(Request("mindate")) Then If IsNumeric(Request("mindate")) Then Dim dblMindate dblMindate = CDbl(Request("mindate"))
'Test for WHERE If blnWhere Then sql = sql & "AND " Else sql = sql & "WHERE " : blnWhere = True sql = sql & "(date >= " & CStr(dblMindate) & ") " End If End If
'--date (maximum) If Not IsEmpty(Request("maxdate")) Then If IsNumeric(Request("maxdate")) Then Dim dblMaxdate dblMaxdate = CDbl(Request("maxdate"))
'Test for WHERE If blnWhere Then sql = sql & "AND " Else sql = sql & "WHERE " : blnWhere = True sql = sql & "(date <= " & CStr(dblMaxdate) & ") " End If End If
'--Sort By Field sql = sql & "ORDER BY " Select Case Trim(LCase(Request("sortby"))) Case "sku": sql = sql & "Sku " Case "title": sql = sql & "Title " Case "date": sql = sql & "date " Case Else: sql = sql & "ID " End Select
'--Sort Order Select Case Trim(LCase(Request("sortorder"))) Case "desc": sql = sql & "DESC" Case Else: sql = sql & "ASC" End Select '--Dynamic sql finished
'Create and open connection object With objCn .CursorLocation = adUseClient .ConnectionTimeout = 15 .CommandTimeout = 30 .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & Server.MapPath("../fpdb/eva.mdb") & ";" .Open End With
'Create and open recordset object With objRs .ActiveConnection = objCn .CursorLocation = adUseClient .CursorType = adOpenForwardOnly .LockType = adLockReadOnly .Source = sql .PageSize = intPageSize .Open Set .ActiveConnection = Nothing 'Disconnect the recordset End With
'Creates a long value from a variant, invalid always set to zero Function MakeLong(ByVal varValue) If IsNumeric(varValue) Then MakeLong = CLng(varValue) Else MakeLong = 0 End If End Function
'Returns a neatly made paging string, automatically configuring for request 'variables, regardless of in querystring or from form, adjust output to your needs. Function Paging(ByVal intPage, ByVal intPageCount, ByVal intRecordCount) Dim strQueryString Dim strScript Dim intStart Dim intEnd Dim strRet Dim i
If intPage > intPageCount Then intPage = intPageCount ElseIf intPage < 1 Then intPage = 1 End If
If intRecordCount = 0 Then strRet = "No Records Found" ElseIf intPageCount = 1 Then strRet = "End Of Hits" Else For i = 1 To Request.QueryString.Count If LCase(Request.QueryString.Key(i)) <> "page" Then strQueryString = strQueryString & "&" strQueryString = strQueryString & Server.URLEncode(Request.QueryString.Key(i)) & "=" strQueryString = strQueryString & Server.URLEncode(Request.QueryString.Item(i)) End If Next
For i = 1 To Request.Form.Count If LCase(Request.Form.Key(i)) <> "page" Then strQueryString = strQueryString & "&" strQueryString = strQueryString & Server.URLEncode(Request.Form.Key(i)) & "=" strQueryString = strQueryString & Server.URLEncode(Request.Form.Item(i)) End If Next
If Len(strQueryString) <> 0 Then strQueryString = "?" & Mid(strQueryString, 2) & "&" Else strQueryString = "?" End If
If intPage <= 10 Then intStart = 1 Else If (intPage Mod 10) = 0 Then intStart = intPage - 9 Else intStart = intPage - (intPage Mod 10) + 1 End If End If
intEnd = intStart + 9 If intEnd > intPageCount Then intEnd = intPageCount
If intPage <> 1 Then strRet = strRet & "<a href=""" & strScript strRet = strRet & "page=" & intPage - 1 strRet = strRet & """><<Prev</a> " End If
For i = intStart To intEnd If i = intPage Then strRet = strRet & "<b>" & i & "</b> " Else strRet = strRet & "<a href=""" & strScript strRet = strRet & "page=" & i strRet = strRet & """>" & i & "</a>" If i <> intEnd Then strRet = strRet & " " End If Next
If intPage <> intPageCount Then strRet = strRet & " <a href=""" & strScript strRet = strRet & "page=" & intPage + 1 strRet = strRet & """>Next>></a> " End If End If
<% 'Object cleanup If IsObject(objRs) Then If Not objRs Is Nothing Then If objRs.State = adStateOpen Then objRs.Close Set objRs = Nothing End If End If
If IsObject(objCn) Then If Not objCn Is Nothing Then If objCn.State = adStateOpen Then objCn.Close Set objCn = Nothing End If End If %>
Prøv at rette til følgende. Du må heller ikke bruge dansk datoformat i en SQL-sætning. Brug enten mm-dd-yyyy eller endnu bedre yyyy-mm-dd.
'--date (minimum) If Not IsEmpty(Request("mindate")) Then If IsNumeric(Request("mindate")) Then Dim dblMindate dblMindate = CDbl(Request("mindate"))
'Test for WHERE If blnWhere Then sql = sql & "AND " Else sql = sql & "WHERE " : blnWhere = True sql = sql & "(date >= '" & CStr(dblMindate) & "') " End If End If
'--date (maximum) If Not IsEmpty(Request("maxdate")) Then If IsNumeric(Request("maxdate")) Then Dim dblMaxdate dblMaxdate = CDbl(Request("maxdate"))
'Test for WHERE If blnWhere Then sql = sql & "AND " Else sql = sql & "WHERE " : blnWhere = True sql = sql & "(date <= '" & CStr(dblMaxdate) & "') " End If End If
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.