RSS
 

Posts Tagged ‘ado recordset’

Dynamic Parameterized Queries w/ Paging

16 Dec

I did this because it was said that it could not be done.

Here we have a method to utilize the ADO.Command object, ADO.GetRows(), and true recordset paging, all wrapped in one neat little class.

Usage.asp

  1. <!–#include virtual="/cls_db.asp"–>
  2. <%
  3. ‘*****************************************************************************  
  4. ‘*****************************************************************************  
  5. ‘  
  6. ‘ This code can be used anywhere you like, all I ask is that you keep this  
  7. ‘ notice here, so people know who actually made it! =D  Thanks!  
  8. ‘  
  9. ‘ This code was developed by Kevin Pirnie, c/o o7th Web Design  
  10. ‘ support@07th.com :: http://www.07th.com  
  11. ‘  
  12. ‘*****************************************************************************  
  13. ‘*****************************************************************************
  14. ‘——————————————————————————
  15. ‘——————————————————————————
  16. ‘Dimension our variables
  17. Dim objData, intPage
  18. Dim intCols, intRows, intTotPages, intTotRecs, strDisplayPaging, strConnString
  19. ‘——————————————————————————
  20. ‘——————————————————————————
  21. ‘Get the page we are on, if any
  22. intPage = Request.QueryString("p")
  23. ‘If we don’t have a page, set it to 1
  24. If Not(IsNumeric(intPage)) Then intPage = 1
  25. ‘——————————————————————————
  26. ‘——————————————————————————
  27. ‘Set our data object to the class
  28. Set objData = New DBv1
  29.         With objData ‘just because i don’t feel like typing out objData everytime I need it
  30.                 ‘Set our command type: 1 = Inline SQL Statement, 4 = Stored Procedure
  31.                 .intCommandType =               1
  32.                 ‘Pass our Connection String … if we have one, uncomment the line below, and comment the … section
  33.                 ‘.strConnString =               strConnString
  34.                 ‘Or we can simply build it here, just uncomment the lines in between …
  35.                 ‘ …
  36.                 .intDBType =                    3
  37.                 ‘Supported database types:
  38.                 ‘       1 = SQL 2000
  39.                 ‘       2 = SQL 2005
  40.                 ‘       3 = SQL 2005 Express
  41.                 ‘       4 = MS Access
  42.                 ‘       6 = MS Access 2007
  43.                 ‘       7 = MySQL
  44.                 ‘       8 = Borland Interbase ‘<- requires the proper driver installed on the server
  45.                 .strDBServer =                  "The Address to your Database Server"
  46.                 .strDBUser =                    "The Database Username"
  47.                 .strDBPassword =                "The Database Password"
  48.                 .strDBDatabase =                "The default database"
  49.                 ‘ …
  50.                 ‘Pass the class our SQL statement, use ? in the WHERE clause
  51.                 ‘You can also use this to fire off a stored procedure
  52.                 .strQry =                               "Select Field1, Field2 From Table Where Field3 = ? Or Field4 = ?"
  53.                 ‘Pass an array of values to look for in our WHERE clause
  54.                 .arrParamValues =               Array(Val1, Val2) ‘If none, use ""
  55.                 ‘Pass an array of Data Types for our values (These are ADO DataTypes, and the list of them can be found here -> http://www.w3schools.com/ADO/ado_datatypes.asp)
  56.                 ‘Only use the numeric value for this, we also only support the following:
  57.                 ’2, 3, 4, 5, 6, 7, 11, 14, 72, 128, 129, 200, 203, 204
  58.                 .arrParamDataTypes =    Array(200, 200) ‘If none, use ""
  59.                 ‘If we fire a stored procedure, and it has an output variable
  60.                 ‘.intRetDataType =              3 ‘Set the return value data type
  61.                 ‘.intRetSize =                  4 ‘Set the return value size
  62.                 ‘Do we want to page the results?
  63.                 .boolUsePaging =                True ‘or False
  64.                 ‘How many records per page do we want to display?
  65.                 .intRecPerPage =                10 ‘any numeric value
  66.                 ‘What page number are we on?
  67.                 .intPageNumber =                intPage
  68.                 ‘What page are we displaying these results?
  69.                 .strPagingPage =                "usage.asp?a=a" ‘We use a=a because the paging method expects a querystring to already be available
  70.                 ‘What should we use to display for the left arrows?
  71.                 .strPagingLeft =                "&laquo;"
  72.                 ‘What should we use to display for the right arrows?
  73.                 .strPagingRight =               "&raquo;"
  74.                 ‘Execute our query, and store the resulting 2d array in a variable for use later
  75.                 ‘If we fire a stored procedure and it has an output variable, this variable will equal that value
  76.                 tmpArray =                              .ExecuteQry()
  77.                 ‘Get the total number of pages returned from the recordset
  78.                 intTotPages =                   .intTotalPages
  79.                 ‘Get the total number of records returned from the recordset
  80.                 intTotRecs =                    .intTotalRecords
  81.                 ‘Get our paging links for the recordset
  82.                 strDisplayPaging =              .RecordPaging()
  83.         End With ‘End our With block
  84. ‘Clean up the object
  85. Set objData = Nothing
  86. ‘——————————————————————————
  87. ‘——————————————————————————
  88. ‘Now that we have done all this, let’s see what the results are.
  89. ‘First we check to see if we have an array
  90. If IsArray(tmpArray) Then
  91.         ‘We have an array, so let’s work with it
  92.         ‘I’d like to put in the info about the paging, total records & total pages
  93.         Response.Write("<strong>" & intTotRecs & "</strong> total records in <strong>" & intTotPages & "</strong> pages.")
  94.         ‘Now I want to display my paging links
  95.         Response.Write(strDisplayPaging)
  96.         ‘Now Let’s display the results of the recordset…
  97.         Response.Write("<table width=""100%"" cellpadding=""2"" cellspacing=""0"" border=""1"">" & VbCrLf)
  98.         ‘Since I know how many columns, I will simply put the table header here
  99.         Response.Write("        <thead>" & VbCrLf)
  100.         Response.Write("                <tr>" & VbCrLf)
  101.         Response.Write("                        <th>ISO CC</th>" & VbCrLf)
  102.         Response.Write("                        <th>ISO CC3</th>" & VbCrLf)
  103.         Response.Write("                        <th>Country Number Code</th>" & VbCrLf)
  104.         Response.Write("                        <th>Country Name</th>" & VbCrLf)
  105.         Response.Write("                </tr>" & VbCrLf)
  106.         Response.Write("        </thead>" & VbCrLf)
  107.         ‘Now let’s start the body of our table
  108.         Response.Write("        <tbody>" & VbCrLf)
  109.         ‘Let’s start a FOR loop to get and display all available rows
  110.         For intRows = 0 To UBound(tmpArray, 2) ‘The 2 is representative of the rows in the recordset
  111.                 ‘If we didn’t know what columns we have, we would do another FOR loop inside this using:
  112.                 ‘UBound(tmpArray, 1)
  113.                 Response.Write("                <tr>" & VbCrLf)
  114.                 Response.Write("                        <td>" & tmpArray(1, intRows) & "</td>" & VbCrLf)
  115.                 Response.Write("                        <td>" & tmpArray(3, intRows) & "</td>" & VbCrLf)
  116.                 Response.Write("                        <td>" & tmpArray(4, intRows) & "</td>" & VbCrLf)
  117.                 Response.Write("                        <td>" & tmpArray(2, intRows) & "</td>" & VbCrLf)
  118.                 Response.Write("                </tr>" & VbCrLf)
  119.         Next
  120.         intRows = Null ‘Clear intRows out
  121.         Response.Write("        </tbody>" & VbCrLf)
  122.         Response.Write("</table>" & VbCrLf)
  123.         ‘Id like to display the record paging again, in case the list is long
  124.         Response.Write(strDisplayPaging)
  125.         ‘I’d also like to display that paging info again
  126.         Response.Write("<strong>" & intTotRecs & "</strong> total records in <strong>" & intTotPages & "</strong> pages.")
  127.         Erase tmpArray ‘Releases the array from memory
  128. Else
  129.         ‘We have no array, so let’s display a message stating this!
  130.         Response.Write("There are no records for that query.")
  131. End If
  132. %>

cls_db.asp

  1. <%
  2. ‘*****************************************************************************  
  3. ‘*****************************************************************************  
  4. ‘  
  5. ‘ This code can be used anywhere you like, all I ask is that you keep this  
  6. ‘ notice here, so people know who actually made it! =D  Thanks!  
  7. ‘  
  8. ‘ This code was developed by Kevin Pirnie, c/o o7th Web Design  
  9. ‘ support@07th.com :: http://www.07th.com  
  10. ‘  
  11. ‘*****************************************************************************  
  12. ‘*****************************************************************************
  13. Class DBv1
  14.     ‘Private Declarations
  15.    Private i, p, pp, strDataLength, objCmd, objRS, objConn, objError
  16.     Private intCurrPage, ini, fim
  17.     ‘Public Declarations
  18.    Public intDBType, strDBUser, strDBPassword, strDBServer, strDBDatabase
  19.         Public strConnString, intCommandType
  20.     Public strQry, arrParamValues, arrParamDataTypes, intRetDataType, intRetSize
  21.     Public boolUsePaging, intTotalPages, intTotalRecords
  22.     Public intRecPerPage, intPageNumber, strPagingPage, strPagingLeft, strPagingRight
  23.     ‘Initialize
  24.    Private Sub Class_Initialize()
  25.         intDBType = 0
  26.         intCommandType = 0
  27.         strDBServer = Null
  28.         strDBUser = Null
  29.         strDBPassword = Null
  30.         strDBDatabase = Null
  31.         strQry = Null
  32.         arrParamValues = Null
  33.         arrParamDataTypes = Null
  34.         boolUsePaging = False
  35.         intTotalPages = 0
  36.         intTotalRecords = 0
  37.         intRecPerPage = 0
  38.         intPageNumber = 0
  39.         strPagingPage = Null
  40.         strPagingLeft = " &lt; "
  41.         strPagingRight = " &gt; "
  42.     End Sub
  43.     ‘Terminate
  44.    Private Sub Class_Terminate()
  45.         intDBType = 0
  46.         intCommandType = 0
  47.         strDBServer = Null
  48.         strDBUser = Null
  49.         strDBPassword = Null
  50.         strDBDatabase = Null
  51.         strQry = Null
  52.         arrParamValues = Null
  53.         arrParamDataTypes = Null
  54.         boolUsePaging = False
  55.         intTotalPages = 0
  56.         intTotalRecords = 0
  57.         intRecPerPage = 0
  58.         intPageNumber = 0
  59.         strPagingPage = Null
  60.         strPagingLeft = " &lgt; "
  61.         strPagingRight = " &rgt; "
  62.     End Sub
  63.     ‘Execute the Query
  64.    Public Function ExecuteQry()
  65.         Set objConn = CreateObject("ADODB.Connection")
  66.             objConn.Open strConnectionString
  67.                 Set objCmd = CreateObject("ADODB.Command")
  68.                     objCmd.CommandText = strQry
  69.                     objCmd.CommandType = intCommandType
  70.                     If IsArray(arrParamValues) And IsArray(arrParamDataTypes) Then
  71.                         If UBound(arrParamValues) = UBound(arrParamDataTypes) Then
  72.                             For i = 0 To UBound(arrParamValues)
  73.                                 Select Case arrParamDataTypes(i)
  74.                                     Case 2 ‘Small Integer
  75.                                        strDataLength = 2
  76.                                     Case 3 ‘Integer
  77.                                        strDataLength = 4
  78.                                     Case 4 ‘Single
  79.                                        strDataLength = 4
  80.                                     Case 5 ‘Float
  81.                                        strDataLength = 8
  82.                                     Case 6 ‘Currency
  83.                                        strDataLength = 8
  84.                                     Case 7 ‘Date
  85.                                        strDataLength = 8
  86.                                     Case 11 ‘Bit
  87.                                        strDataLength = 1
  88.                                     Case 14 ‘Decimal
  89.                                        strDataLength = 9
  90.                                     Case 72 ‘GUID
  91.                                        strDataLength = 16
  92.                                     Case 128 ‘Binary
  93.                                        strDataLength = 50
  94.                                     Case 129 ‘Char
  95.                                        If Not ReqValue(arrParamValues(i)) Then
  96.                                             strDataLength = 1
  97.                                         Else
  98.                                             strDataLength = Len(arrParamValues(i))
  99.                                         End If
  100.                                     Case 200 ‘VarChar
  101.                                        If Not ReqValue(arrParamValues(i)) Then
  102.                                             strDataLength = 1
  103.                                         Else
  104.                                             strDataLength = Len(arrParamValues(i))
  105.                                         End If
  106.                                     Case 203 ‘NText
  107.                                        If Not ReqValue(arrParamValues(i)) Then
  108.                                             strDataLength = 1
  109.                                         Else
  110.                                             strDataLength = Len(arrParamValues(i))
  111.                                         End If
  112.                                     Case 204 ‘VarBinary
  113.                                        strDataLength = 50
  114.                                     Case Else ‘Hmm…guess
  115.                                        If Not ReqValue(arrParamValues(i)) Then
  116.                                             strDataLength = 1
  117.                                         Else
  118.                                             strDataLength = Len(arrParamValues(i))
  119.                                         End If
  120.                                 End Select
  121.                                 If arrParamDataTypes(i) = 14 Then
  122.                                     Set p = objCmd.CreateParameter(, CInt(arrParamDataTypes(i)), , CInt(strDataLength), InputCleaner(arrParamValues(i)))
  123.                                     p.NumericScale = 2
  124.                                     p.Precision = 10
  125.                                     objCmd.Parameters.Append p
  126.                                 Else
  127.                                     objCmd.Parameters.Append (objCmd.CreateParameter(, CInt(arrParamDataTypes(i)), , CInt(strDataLength), InputCleaner(arrParamValues(i))))
  128.                                 End If
  129.                             Next
  130.                             i = Null
  131.                             Erase arrParamValues
  132.                             Erase arrParamDataTypes
  133.                         Else
  134.                             ExecuteQry = "Your values and data type arrays need to be the same length."
  135.                         End If
  136.                     End If
  137.                                         ‘Debug the parameters if necessary
  138.                                         ‘For each Item In objCmd.Parameters
  139.                                         ‘       Write("Name:" & Item.Name & "-Type:" & Item.Type & "-Value:" & Item.Value & "<br />")
  140.                                         ‘Next  
  141.                    Set objCmd.ActiveConnection = objConn
  142.                         Select Case intCommandType
  143.                             Case 1
  144.                             If InStr(1, UCase(strQry), "SELECT") > 0 Then
  145.                                 Set objRS = CreateObject("Adodb.RecordSet")
  146.                                     If boolUsePaging Then
  147.                                         objRS.PageSize = intRecPerPage
  148.                                         objRS.CacheSize = intRecPerPage
  149.                                         objRS.CursorType = 3
  150.                                     End If
  151.                                     objRS.Open objCmd
  152.                                     If Not (objRS.EOF) Then
  153.                                         If boolUsePaging Then
  154.                                             If Not (validNumber(intPageNumber)) Then
  155.                                                 objRS.AbsolutePage = 1
  156.                                             Else
  157.                                                 objRS.AbsolutePage = intPageNumber
  158.                                             End If
  159.                                             ExecuteQry = objRS.GetRows(intRecPerPage)
  160.                                             intTotalPages = objRS.PageCount
  161.                                             intTotalRecords = objRS.RecordCount
  162.                                         Else
  163.                                             ExecuteQry = objRS.GetRows()
  164.                                         End If
  165.                                     Else
  166.                                         ExecuteQry = "There are no records."
  167.                                         Exit Function
  168.                                     End If
  169.                                 Set objRS = Nothing
  170.                                 Exit Function
  171.                             ElseIf InStr(1, UCase(strQry), "INSERT") > 0 Then
  172.                                 If InStr(1, UCase(strQry), "@@IDENTITY") > 0 Or InStr(1, UCase(strQry), "NEWID()") > 0 Then
  173.                                     Set objRS = objCmd.Execute()
  174.                                         If Not (objRS.EOF) Then
  175.                                             ExecuteQry = objRS(0)
  176.                                         End If
  177.                                     Set objRS = Nothing
  178.                                 Else
  179.                                     objCmd.Execute
  180.                                     ExecuteQry = "Your command has been executed."
  181.                                 End If
  182.                             ElseIf (InStr(1, UCase(strQry), "DELETE") > 0 Or InStr(1, UCase(strQry), "UPDATE") > 0 Or Left(UCase(strQry), 2) = "SP") Then
  183.                                 If Left(UCase(strQry), 2) = "SP" And intRetDataType > "" Then
  184.                                     objCmd.Parameters.Append (objCmd.CreateParameter("@ret", intRetDataType, 2, , intRetSize))
  185.                                     objCmd.Execute
  186.                                     ExecuteQry = objCmd.Parameters("@ret")
  187.                                 Else
  188.                                     ExecuteQry = objCmd.Execute
  189.                                 End If
  190.                             End If
  191.                         Case 4
  192.                             If intRetDataType > "" Then
  193.                                 objCmd.Parameters.Append (objCmd.CreateParameter("@ret", intRetDataType, 2, , intRetSize))
  194.                                 objCmd.Execute
  195.                                 ExecuteQry = objCmd.Parameters("@ret")
  196.                             Else
  197.                                 If boolUsePaging Then
  198.                                     Set objRS = CreateObject("Adodb.RecordSet")
  199.                                         objRS.PageSize = intRecPerPage
  200.                                         objRS.CacheSize = intRecPerPage
  201.                                         objRS.CursorType = 3
  202.                                         objRS.CursorLocation = 3
  203.                                         objRS.Open objCmd
  204.                                         If Not (objRS.EOF) Then
  205.                                             If boolUsePaging Then
  206.                                                 If Not (validNumber(intPageNumber)) Then
  207.                                                     objRS.AbsolutePage = 1
  208.                                                 Else
  209.                                                     objRS.AbsolutePage = intPageNumber
  210.                                                 End If
  211.                                                 ExecuteQry = objRS.GetRows(intRecPerPage)
  212.                                                 intTotalPages = objRS.PageCount
  213.                                                 intTotalRecords = objRS.RecordCount
  214.                                             Else
  215.                                                 ExecuteQry = objRS.GetRows()
  216.                                             End If
  217.                                         Else
  218.                                             objCmd.Execute
  219.                                         End If
  220.                                     Set objRS = Nothing
  221.                                 Else
  222.                                     objCmd.Execute
  223.                                 End If
  224.                             End If
  225.                         End Select
  226.                     Set objCmd.ActiveConnection = Nothing
  227.                 Set objCmd = Nothing
  228.             objConn.Close
  229.         Set objConn = Nothing
  230.     End Function
  231.     ‘Paging Links
  232.    Public Function RecordPaging()
  233.         tmpString = ""
  234.         tmpString = tmpString & "<div class=""paging_links"">" & vbCrLf
  235.         If Not (validNumber(intPageNumber)) Then
  236.             CurrentPage = 1 ‘We’re On the first page
  237.            NumPerPageOf = 1
  238.         Else
  239.             CurrentPage = CInt(intPageNumber)
  240.             NumPerPageOf = ((CurrentPage * NumPerPage) – NumPerPage) + 1
  241.         End If
  242.         If CurrentPage > 1 Then
  243.             If CurrentPage > 5 And intTotalPages > 10 Then
  244.                 tmpString = tmpString & "   <span><a href=""" & strPagingPage & "&amp;p=1"">1</a></span> "
  245.                 tmpString = tmpString & "<span class=""prevChunk""> &laquo; </span>"
  246.             End If
  247.             If intTotalPages > 10 Then
  248.                 If CurrentPage > 5 Then
  249.                     If intTotalPages > (CurrentPage + 5) Then
  250.                         ini = (CurrentPage – 4)
  251.                         fim = (CurrentPage + 5)
  252.                     Else
  253.                         ini = (intTotalPages – 9)
  254.                         fim = intTotalPages
  255.                     End If
  256.                 Else
  257.                     ini = 1
  258.                     fim = 10
  259.                 End If
  260.             Else
  261.                 ini = 1
  262.                 fim = intTotalPages
  263.             End If
  264.             For a = ini To fim
  265.                 If a = CInt(intPageNumber) Then
  266.                     tmpString = tmpString & " <span class=""curPage"">" & a & "</span> "
  267.                 Else
  268.                     tmpString = tmpString & "   <span><a href=""" & strPagingPage & "&amp;p=" & a & """>" & a & "</a></span> "
  269.                 End If
  270.             Next: a = Null
  271.         Else
  272.             If intTotalPages = 1 Then
  273.                 tmpString = tmpString & ""
  274.             Else
  275.                 tmpString = tmpString & "<span class=""curPage"">1</span> "
  276.             End If
  277.             If intTotalPages > 10 Then     ‘id=161&MWC=Layouts
  278.                fim = 10
  279.             Else
  280.                 fim = intTotalPages
  281.             End If
  282.             For a = 2 To fim
  283.                 If a = CInt(intPageNumber) Then
  284.                     tmpString = tmpString & "<span class=""curPage"">" & a & "</span> "
  285.                 Else
  286.                     tmpString = tmpString & "   <span><a href=""" & strPagingPage & "&amp;p=" & a & """>" & a & "</a></span> "
  287.                 End If
  288.             Next: a = Null
  289.         End If
  290.         If CurrentPage < intTotalPages – 5 And intTotalPages > 10 Then
  291.             tmpString = tmpString & "<span class=""lastChunk""> &raquo; </span>"
  292.                         tmpString = tmpString & "   <span><a href=""" & strPagingPage & "&amp;p=" & intTotalPages & """>" & intTotalPages & "</a></span> "
  293.         End If
  294.         tmpString = tmpString & "</div>" & vbCrLf
  295.         RecordPaging = tmpString
  296.         tmpString = ""
  297.     End Function
  298.     ‘Get our connection string
  299.    Private Function strConnectionString()
  300.         If ReqValue(strConnString) Then
  301.             strConnectionString = strConnString
  302.         Else
  303.             Select Case intDBType
  304.                 Case 1 ‘SQL 2000
  305.                    strConnectionString = "Provider=SQLOLEDB.1;Password=" & strDBPassword & ";User ID=" & strDBUser & ";Initial Catalog=" & strDBDatabase & ";Data Source=" & strDBServer & ""
  306.                 Case 2 ‘SQL 2005
  307.                    strConnectionString = "Provider=SQLNCLI;Server=" & strDBServer & ";Database=" & strDBDatabase & ";Uid=" & strDBUser & ";Pwd=" & strDBPassword & ";DataTypeCompatibility=80;"
  308.                 Case 3 ‘SQL 2005 Express
  309.                    strConnectionString = "Provider=SQLOLEDB;Data Source=" & strDBServer & ";Persist Security Info=True;Password=" & strDBPassword & ";User ID=" & strDBUser & ";Initial Catalog=" & strDBDatabase & ";DataTypeCompatibility=80"
  310.                 Case 4 ‘MS Access
  311.                    strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBDatabase & ";User Id=" & strDBUser & ";Password=" & strDBPassword & ";"
  312.                 Case 6 ‘MS Access 2007
  313.                    strConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strDBDatabase & ";Persist Security Info=False;"
  314.                 Case 8 ‘Borland Interbase – requires the SIBProvider to be installed on the server
  315.                    strConnectionString = "provider=sibprovider;location=" & strDBServer & ":;data source=" & strDBDatabase & ";user id=" & strDBUser & ";Password=" & strDBPassword & ";"
  316.                 Case 7 ‘MySQL
  317.                    strConnectionString = "Driver={MySQL ODBC 3.51 Driver};Server=" & strDBServer & ";Database=" & strDBDatabase & "; User=" & strDBUser & ";Password=" & strDBPassword & ";Option=3;"
  318.             End Select
  319.         End If
  320.     End Function
  321.     ‘Input cleaning … just in case
  322.    Private Function InputCleaner(ByVal strStringToClean)
  323.         If Not (ReqValue(strStringToClean)) Then
  324.             If InStr(1, strStringToClean, "’") > 0 Then strStringToClean = Replace(strStringToClean, "’", "&#39;")
  325.             If InStr(1, strStringToClean, Chr(34)) > 0 Then strStringToClean = Replace(strStringToClean, Chr(34), "&#34;")
  326.             If InStr(1, strStringToClean, "@") > 0 Then strStringToClean = Replace(strStringToClean, "@", "&#64;")
  327.             If InStr(1, strStringToClean, "|") > 0 Then strStringToClean = Replace(strStringToClean, "|", "&#124;")
  328.             If InStr(1, strStringToClean, "*") > 0 Then strStringToClean = Replace(strStringToClean, "*", "&#42;")
  329.             If InStr(1, strStringToClean, "–") > 0 Then strStringToClean = Replace(strStringToClean, "–", "&#45;&#45;")
  330.             If InStr(1, strStringToClean, "(") > 0 Then strStringToClean = Replace(strStringToClean, "(", "&#40;")
  331.             If InStr(1, strStringToClean, ")") > 0 Then strStringToClean = Replace(strStringToClean, ")", "&#41;")
  332.         End If
  333.         InputCleaner = strStringToClean
  334.     End Function
  335.     ‘Required Value?
  336.    Private Function ReqValue(ByVal strValue)
  337.         ReqValue = True ‘by default
  338.        If strValue = "" Then ReqValue = False
  339.         If IsNull(strValue) Then ReqValue = False
  340.         If Len(strValue) <= 0 Then ReqValue = False
  341.         If IsEmpty(strValue) Then ReqValue = False
  342.     End Function
  343.     ‘Valid Number?
  344.    Private Function validNumber(ByVal strValue)
  345.         If ReqValue(strValue) Then
  346.             validNumber = False ‘Default
  347.            Set objRegExp = New RegExp
  348.                 objRegExp.Pattern = "^(?:-?(?:[0-9]+\.?|[0-9]*(?:\.[0-9]+){1}))$"
  349.                 validNumber = objRegExp.Test(strValue)
  350.             Set objRegExp = Nothing
  351.         End If
  352.     End Function
  353. End Class
  354. %>
 
 

Recordset Paging With .GetRows()

10 Dec

I constantly use a .GetRows() array when displaying a recordset from a database.  However, I found that I needed to be able to page the records ont he page, so that so many records won’t display all at once potentially crashing the page.

Here is a function that I use to display a Google like recordset paging system.

  1. <%
  2. ‘Usage = Response.Write(PagingLinks(TheArrayNameFromRecordset, HowManyPerPage, URLOfThePageWeAreOn, 1stLinkDisplay, PrevLinkDisplay, NextLinkDisplay, LastLinkDisplay, StartingPage#Display, EndingPage#Display))
  3. Function PagingLinks(ByVal strArrName, ByVal strNumPerPage, ByVal strPageName, ByVal strFirst, ByVal strPrev, ByVal strNext, ByVal strLast, ByVal iStart, ByVal iStop)
  4.         PagingLinks = ""
  5.         iStart = Request.QueryString("s")
  6.         iOffset = Request.QueryString("o")
  7.         If Not IsNumeric(iStart) Then
  8.                 iStart = 0
  9.         Else
  10.                 iStart = CLng(iStart)
  11.         End If
  12.         If Not IsNumeric(iOffset) Then
  13.                 iOffset = strNumPerPage
  14.         Else
  15.                 iOffset = CLng(iOffset)
  16.         End If
  17.         If UBound(strArrName, 2) < strNumPerPage Then
  18.                 iRows = (UBound(strArrName,2) + 1)
  19.         Else
  20.                 iRows = UBound(strArrName,2)
  21.         End If
  22.         If iRows > (iOffset + iStart) Then
  23.                 iStop = iOffset + iStart – 1
  24.         Else
  25.                 iStop = iRows
  26.         End If
  27.         If iStop > iRows – iOffset Then
  28.                 iStop = iStop – 1
  29.         End If
  30.         If Not(IsNumeric(iOffset)) Then iOffset = 1
  31.         pages = RoundUp(iRows / iOffset)
  32.         pgNum = RoundUp(iStart / iOffset) + 1
  33.         PagingLinks = PagingLinks & "<a href="" strpagename="" &s="0&o=&quot;" ioffset="" &="">" & strFirst & "</a> "
  34.         If iStart > 0 Then
  35.                 PagingLinks = PagingLinks & "<a href="" strpagename="" &="" &s=" & iStart – iOffset & " &o=" & iOffset & ">" & strPrev & "</a> "
  36.         End If
  37.         Dim MaxPagesToShow, MaxLBound, MaxUBound
  38.         MaxPagesToShow = 10
  39.         MaxLBound = pgNum – Int(MaxPagesToShow/2)
  40.         If MaxLBound < 1 Then MaxLBound = 1
  41.         MaxUBound = MaxLBound + MaxPagesToShow
  42.         If MaxUBound > pages Then
  43.                 MaxUBound = pages
  44.                 MaxLBound = MaxUBound – MaxPagesToShow
  45.         End If
  46.         If MaxLBound < 1 Then MaxLBound = 1
  47.         If (iRows – iOffset) < 0 Then
  48.                 l = 0
  49.         Else
  50.                 l = (iRows – iOffset)
  51.         End If
  52.         For z = MaxLBound To MaxUBound
  53.                 pgOffset = z * iOffset – iOffset
  54.                 If z = pgNum Then
  55.                         PagingLinks = PagingLinks & ("<b>" & z & "</b> ")
  56.                 Else
  57.                         PagingLinks = PagingLinks & ("<a href="" strpagename="" &="" &s=" & pgOffset & " &o=" & iOffset & ">" & z & "</a> ")
  58.                 End If
  59.         Next
  60.         If iStop + 1 < iRows Then
  61.                 PagingLinks = PagingLinks & "<a href="" strpagename="" &="" &s=" & iStart + iOffset & " &o=" & iOffset & ">" & strNext & "</a> "
  62.         End If
  63.         PagingLinks = PagingLinks & "<a href="" strpagename="" &="" &s=" & l & " &o=" & iOffset & ">" & strLast & "</a><br>"
  64. End Function
  65. %>
 
 
 

Optimized by SEO Ultimate