Home > Articles > Programming > Visual Basic

  • Print
  • + Share This
Like this article? We recommend

Saving the Search Filter String

At this point, the mechanism to retain the user's state information is complete. The only thing left is to change the SearchProc and SearchSend procedures to use it. I like to get the easy stuff done first, so let's attempt to retain the filter text for the search form. When the user sends a request to do a search, the search string they enter must be assigned to the mUserState.Filter property so that it will get saved to the state file when this request has completed. Also, whenever the Search form is sent, the {Filter} token needs to be replaced with the contents of the clsUserState.Filter property. These changes are indicated in bold below:

Private Sub SearchSend()
  Dim lHTML As String
  lHTML = LoadHTML("Search.htm")
  lHTML = Replace(lHTML, "{Filter}", mUserState.Filter)
  lHTML = Replace(lHTML, "{PublishersTable}", "")
  mHTMLSendString = lHTML
End Sub

Private Sub SearchProc()
  Dim lHTML  As String
  Dim lTable As String
  Dim lDB   As ADODB.Connection
  Dim lRS   As ADODB.Recordset
    
  ' Get the Filter value from the form.
  mUserState.Filter = gAspReq.Item("txtFilter")
  
  ' Query the database for the matching Publishers and store
  ' the results into the lTable string array.
  Set lDB = New ADODB.Connection
  Call lDB.Open("Driver={Microsoft Access Driver (*.mdb)}; DBQ=" _
    & App.Path & "\Biblio.mdb")
  Set lRS = lDB.Execute("SELECT * FROM Publishers" _
    & " WHERE Name LIKE '%" & mUserState.Filter & "%'" _
    & " ORDER BY Name")
  
  lTable = "<table border=1><TR><TD>Name</TD><TD>Telephone</TD></TR>"
  Do Until lRS.EOF = True
    ' Hyperlink to show the detail for this publisher.
    lTable = lTable _
      & "<TR><TD><a href='PubWebUI.asp?uid={uid}&action=DetailProc&PubID=" _
      & lRS!PubID & "'>" & lRS!Name & "</TD>"
    lTable = lTable & "<TD>" & lRS!Telephone & "</TD></TR>"
    Call lRS.MoveNext
  Loop
  lTable = lTable & "</table>"

  ' Load the HTML file and replace the tokens.
  lHTML = LoadHTML("Search.htm")
  lHTML = Replace(lHTML, "{Filter}", mUserState.Filter)
  lHTML = Replace(lHTML, "{PublishersTable}", lTable)
  mHTMLSendString = lHTML
End Sub

Run the project, and type the default URL (http://localhost/PubWebUI.asp) for the sample application. The blank Search Publishers form should be displayed in the browser. If you view the source code for this form, you see the newly generated uid is included in the HTML. For example:

action="PubWebUI.asp?uid=2001062416314756881&action=SearchProc"

And you notice that a state file named 2001062416314756881.usf has been created. Open the state file with Notepad, and it should look similar to this:

""
0

Return to the browser, search for publishers with a name like "book," and then view the contents of the state file again and now it should look like:

"book"
0

The first record contains the search filter, and the second record contains the number of publishers saved to the list. In this case, zero.

  • + Share This
  • 🔖 Save To Your Account