' Global variables, available to all functions
Private MyDeser As WDDXDeserializer  'Macromedia's WDDX deserializer
Private MySer As WDDXSerializer      'Macromedia's WDDX serializer
Private MyRS As WDDXRecordset        'WDDX-style recordset object
Private RequestStruct As WDDXStruct  'WDDX-style structure object
Private RowNum As Integer            'The "current" row of recordset
Private RobotServer As String        'Webserver's hostname



' This executes when the application first runs
Private Sub Form_Load()

   ' Create Instances of Macromedia objects
   Set MySer = New WDDXSerializer
   Set MyDeser = New WDDXDeserializer
   Set RequestStruct = New WDDXStruct

   ' Initialize the global MyRS Variable
   ' This holds the book data throughout the app
   Set MyRS = New WDDXRecordset

   ' Add columns we'll be dealing with to recordset
   MyRS.addColumn ("BOOKID")
   MyRS.addColumn ("ISBN")
   MyRS.addColumn ("TITLE")
   MyRS.addColumn ("AUTHORFIRSTNAME")
   MyRS.addColumn ("AUTHORLASTNAME")
   MyRS.addColumn ("WASEDITED")

   ' Webserver to interact with Book Robot at
   RobotServer = "http://127.0.0.1"

End Sub


' This fills form controls with data from recordset
Private Sub InitControls()
  Dim Count, ThisTitle

  ' Clear all current items out of the list box
  List1.Clear

  ' For each row in the recordset, get the title
  ' and then add the title to the list box
  For Count = 1 To MyRS.getRowCount
    ThisTitle = MyRS.getField(Count, "TITLE")
    List1.AddItem (ThisTitle)
  Next Count

End Sub


' This runs when a user selects a book on the form
Private Sub List1_Click()
  ' When user clicks on a book, set global RowNum variable
  ' to the list's index plus one, so all recordset functions
  ' can use RowNum to refer to the current row
  RowNum = List1.ListIndex + 1

  ' Set various data-entry blanks to values from recordset
  txtISBN.Text = MyRS.getField(RowNum, "ISBN")
  txtTitle.Text = MyRS.getField(RowNum, "TITLE")
  txtAuthorF.Text = MyRS.getField(RowNum, "AUTHORFIRSTNAME")
  txtAuthorL.Text = MyRS.getField(RowNum, "AUTHORLASTNAME")

End Sub


' This executes if user clicks "Keep Edits" button
Private Sub btnKeepEdits_Click()

  ' Update columns in current row of recordset
  MyRS.setField RowNum, "ISBN", txtISBN.Text
  MyRS.setField RowNum, "TITLE", txtTitle.Text
  MyRS.setField RowNum, "AUTHORFIRSTNAME", txtAuthorF.Text
  MyRS.setField RowNum, "AUTHORLASTNAME", txtAuthorL.Text
  MyRS.setField RowNum, "WASEDITED", "Yes"

  ' Update the current item in the list of titles
  List1.List(RowNum - 1) = txtTitle.Text

End Sub
