Data Access  «Prev 

Using onbeforeopen in SQL-Server

src="images/records-onbeforeopen2.gif" width="420" height="549" layout="responsive" alt="Scripting with Record Sets">
Scripting with Record Sets[Steps]

  1. This is the onbeforeopen event handler code. The code is executed just before the recordset object is loaded and changes the default SQL query to retrieve only records of books on special. The name BookRecords was assigned to this instance of the recordset object in the Binding data-bound Design Time Controls lesson. This function is executed just before the BookRecords recordset object is loaded.
  2. Declare the variable querystring. It will contain the SQL query.
  3. This statement assigns a SQL query to querystring. This SQL query specifies to return only records with a Status of 'S' (where Status = 'S'), which indicates that the book is on special.
  4. The recordset object's setSQLText method is called and passes querystring as the parameter.
  5. These lines define a standard HTML table and the table's first row.
  6. This line begins a VBScript For … Next loop that will execute once for each column in the database table.
  7. The purpose of the For…Next loop is to display the column names. This line gets the field name and displays it as table data in the HTML table row.
  8. Now that the column headings are displayed, it's time to display one row in the HTML table for each row in the database table. This line uses the recordset control's end of file, EOF, property to execute a Do…While loop until the end of the recordset is reached.
  9. This For…Next loop also executes one time for each column in the database table, this time to display the column data.
  10. This line gets the database column value from the recordset and displays it in the HTML table.
  11. After a row is displayed, the recordset object's MoveNext method is called to move to the next row. The Loop statement indicates the end of the Do…While loop body.
  12. This statement closes the recordset after all the rows are displayed.