Access Cookbook - Ken Getz [288]
Variables cannot be typed in VBScript. All variables are variants.
To get started with VBScript, take a look at the code we've written for this sample. Follow these steps to look at the code:
In Access, open the Param OrdersByDate.htm data access page. Note that the two text box controls are named txtStartDate and txtEndDate. The command button is named cmdOK.
Select View → HTML Source from the menu. The Microsoft Script Editor will be launched, and you'll see the HTML code the browser uses to display the page.
Press Ctrl-F to do a search. Search for the string "script". The cursor should land on the script containing the event procedure for the cmdOK button's onclick event:
The first two lines of code use the document's Cookie property to record the parameters entered in the text boxes. Each time the code sets the cookie to a new variable = value, that string is appended to whatever the string already contains, with a semicolon separating the variable = value pairs. That is, if the start date is 6/1/97 and the end date is 6/30/97, the cookie will look like this:
startdate=6/1/97;enddate=6/30/97
The third line of code causes the browser to open OrdersByDate.htm.
Close the Microsoft Script Editor and the Param OrdersByDate.htm data access page.
Open the OrdersByDate.htm data access page in design view.
Select View → HTML Source to launch the Microsoft Script Editor. Search for the word "script".
There are two custom scripts in this data access page. The first contains a general-use function named ReadVarInCookie. The code looks like this:
The function takes an argument of the variable names for which we're searching (startdate and enddate, in our case). It returns the value associated with that variable name. Remember, it's the cookie that is being searched for the variable and value, and the cookie looks like this:
startdate=6/1/97;enddate=6/30/97
The first line following the variable declarations uses the built-in Split function to parse the document's cookie into an array of variable = value pairs. That is, it looks for semicolons and creates an array element for each string between the semicolons:
varSplit = split(document.cookie,"; ")
The for loop iterates through each element in the resulting array and checks the first part of the element to see if the string matches the name of the variable sent:
for intCount = lbound(varSplit) to ubound(varSplit)
if left(varSplit(intCount),len(strVariable)) = strVariable then
If the if statement evaluates to True, the code looks for the value on the other side of the equals sign and returns that value:
intFind = instr(varSplit(intCount),"=")
ReadVarInCookie = mid(varSplit(intCount),intFind + 1)
If the variable name is not found, the function returns the value NOT_FOUND.
Scroll down to the second script. This script is not tied to an event, nor is it even contained in a procedure. Rather, the script runs when the page loads:
-->