Home > Articles > Programming > Visual Basic

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

Saving the List of Selected Publishers

To allow the user to add a publisher to the Selected Publishers list, I modified the Detail.htm page to include a hyperlink to a new AddProc action, as shown in Figure 1.

Figure 1 Hyperlink to the AddProc action.

The "Add to Selected Publishers List" hyperlink contains the uid value, the action code, and the selected publisher's ID:

<a href="PubWebUI.asp?uid=2001062416463943523&amp;action=AddProc&amp;PubID=68">

This new AddProc action is included in WebMain's big select statement:

Select Case gAspReq.QueryString("action")
    Case "SearchSend"
      Call SearchSend
    Case "SearchProc"
      Call SearchProc
    Case "DetailProc"
      Call DetailProc
    Case "AddProc"
      Call AddProc
    Case Else ' Otherwise send a default page.
      Call SearchSend
  End Select

It also adds the selected publisher to mUserState's Publishers collection property using this code:

Private Sub AddProc()
  Dim lHTML As String
  Dim lDB  As ADODB.Connection
  Dim lRS  As ADODB.Recordset
  
  ' Query the database for the indicated Publisher.
  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 PubID = " & gAspReq.QueryString.Item("PubID"))
  
  ' Add it to the state object's collection.
  Dim lPubState As clsPubState
  Set lPubState = New clsPubState
  
  lPubState.PubID = lRS!PubID
  lPubState.Name = "" & lRS!Name
  lPubState.Phone = "" & lRS!Telephone
  
  Call mUserState.Publishers.Add(lPubState)
  
  ' Load the confirmation HTML file.
  lHTML = LoadHTML("AddConfirm.htm")

  ' Replace the tokens in the HTML with the Publisher data.
  lHTML = Replace(lHTML, "{Name}", "" & lPubState.Name)

  mHTMLSendString = lHTML
End Sub

The AddProc procedure sends the user a confirmation by sending the contents of the AddConfirm.htm file with appropriate tokens replaced, as shown in Figure 2.

Figure 2 Add Confirmation.

Finally, another new action, ViewSelected, is added to WebMain's big select statement:

Select Case gAspReq.QueryString("action")
    Case "SearchSend"
      Call SearchSend
    Case "SearchProc"
      Call SearchProc
    Case "DetailProc"
      Call DetailProc
    Case "AddProc"
      Call AddProc
    Case "ViewSelected"
      Call ViewSelected
    Case Else ' Otherwise send a default page.
      Call SearchSend
  End Select

This action tells WebMain to call the ViewSelected procedure. The ViewSelected procedure simply loads the ViewSelected.htm file into an HTML string variable, creates an HTML table of selected publishers by iterating through the publishers saved in the current mUserState.Publishers collection property, and puts this HTML string in mHTMLSendString so it will later be sent by WebMain:

Private Sub ViewSelected()
  Dim lHTML   As String
  Dim lTable  As String
  Dim lPubState As clsPubState
  
  lTable = "<table border=1><TR><TD>Name</TD><TD>Telephone</TD></TR>"
  
  For Each lPubState In mUserState.Publishers
    ' Hyperlink to show the detail for this publisher.
    lTable = lTable _
      & "<TR><TD><a href='PubWebUI.asp?uid={uid}&action=DetailProc&PubID=" _
      & lPubState.PubID & "'>" & lPubState.Name & "</TD>"
    lTable = lTable & "<TD>" & lPubState.Phone & "</TD></TR>"
  Next lPubState
  lTable = lTable & "</table>"

  ' Load the HTML file and replace the tokens.
  lHTML = LoadHTML("ViewSelected.htm")
  lHTML = Replace(lHTML, "{PublishersTable}", lTable)
  mHTMLSendString = lHTML

End Sub

A sample response sent to the user from ViewSelected is shown in Figure 3.

Figure 3 Selected publishers, as sent from the ViewSelected procedure.

And the matching state file contains these five records:

"book"
3
68,"TAB BOOKS","800-822-8138"
478,"TAB BOOKS SOFTWARE","800-822-8138"
629,"WARNER BOOKS",""
  • + Share This
  • 🔖 Save To Your Account