To ensure that the script executes only once, create a Boolean variable and toggle its value depending on whether the script has executed or not.
In the following sample resolution, a Boolean variable is dimensioned. The first time that the script executes, a message box appears, and the Boolean variable is then set to
True to indicate that the script has executed. The second time that the script executes, because the value is now
True, it skips the first part of the
If statement and only resets the variable to
False for the next record.
To see how this works, follow these steps:
- Open the sample database, Northwind.mdb.
- In the Database window, click Pages under Objects, and then click New.
- In the New Data Access Page dialog box, click Page Wizard, click Shippers in the Choose the table or query where the object's data comes from box, and then click OK.
- Click the >> button to move all the fields from the Available Fields list to the Selected Fields list. Click Finish.
- Add an unbound text box to the Header: Shippers section.
- In the properties sheet, click the Data tab, and then set the ControlSource property of the unbound text box to the following:
Expr1: document.parentWindow.execScript("subTest()","VBScript")
- On the Tools menu, point to Macro, and then click Microsoft Script Editor.
- On the HTML menu, point to Script Block, and then click Client.
- Type the following:
<SCRIPT language=vbscript>
<!--
Dim blnVar
Sub subTest()
If Not blnVar Then
MsgBox "Testing"
blnVar = True
Else
blnVar = False
End If
End Sub
-->
</SCRIPT>
- Press ALT+TAB to switch back to the Microsoft Access.
- On the View menu, click Page View and note that as you scroll through the records, the message box appears twice for each record.