首页 | 免费域名 | 个人服务器 | 一流信息监控拦截系统 | 虚拟主机知识库 | ASP 空间 | ASP技术大全 | 下载中心 | 客户服务中心
  7i24 > ASP技术大全 > ASP与数据库 > 微软数据库文章 >
    7i24 .Com  
  将参数传递到数据访问页

7i24.Com不停为您服务
将参数传递到数据访问页

Meyyammai Subramanian
Microsoft Corporation

2001 年 4 月

适用于:
  Microsoft® Access 2002

摘要:本文将阐述如何创建一个接受参数并根据参数值检索记录的页面。

目录
简介
创建一个用于提示参数的页面
创建一个根据特定参数显示记录的页面

简介
参数查询使您可以提示信息,例如在基于查询的窗体或报告中检索记录所用的条件。您也可以使用 cookie 将参数从一个数据访问页传递到另一个数据访问页。

以下图例显示了“参数”页,该页提示开始和结束日期。



图 1:接受参数的页面

当用户输入有效的开始和结束日期后,如果单击“导航 URL”按钮,“参数结果”页将打开,显示在开始和结束日期之间的所有记录。



图 2:特定开始和结束日期之间的记录

数据访问页显示了在 1996 年 7 月 1 日和 1996 年 7 月 15 日之间的记录。

创建一个用于提示参数的页面
在“设计”视图中打开一个新的数据访问页。


在页面上添加用于接受用户参数的文本框,然后为其命名。例如,在“参数”页上将文本框命名为 txtStart 和 txtEnd。


添加一个用于导航到结果页的命令按钮,然后为其命名。例如,在“参数”页中将按钮命名为 btnNavigate。


向命令按钮的 OnClick 事件添加代码,以写入 cookie 并导航到结果页。
以下是导航到结果页的命令按钮的示例 OnClick 事件。

<SCRIPT language=VBS>
Sub btnNavigate_onClick
Dim pStartingDate
Dim pEndingDate
Dim URL

Const strParm_1 = "StartingDate"
Const strParm_2 = "EndingDate"

URL = "Parameters_Results.htm"
' 文本框中的值存储在 value 属性中,
' 而 bound span 控件将值存储在 InnerText 中。
pStartingDate = txtStart.value
pEndingDate = txtEnd.value

setVariable strParm_1, pStartingDate
setVariable strParm_2, pEndingDate

window.navigate(URL)
End Sub

</SCRIPT>

在单独的全局脚本块中添加函数以写入、读取和删除 cookie。
<SCRIPT language=VBS>
OPTION EXPLICIT
Dim NOT_FOUND
NOT_FOUND = "NOT_FOUND"

Sub SetVariable(strVariableName, varVariableValue)
  Document.Cookie = strVariableName & "=" & varVariableValue
End Sub

Sub KillVariable(strVariableName)
  SetVariable strVariableName, "NULL;expires=Monday, _
                 01-Jan-95 12:00:00 GMT"
End Sub

Function ReadVariable(strVariableName)
' 在用于查找 cookie 中变量
' 的字符串处理代码中使用这五个变量。
Dim intLocation
Dim intNameLength
Dim intValueLength
Dim intNextSemicolon
Dim strTemp

' 计算变量名的长度和位置。
intNameLength = Len(strVariableName)
intLocation = Instr(Document.Cookie, strVariableName)

' 检查变量名是否存在。
If intLocation = 0 Then
  ' 找不到变量,因此无法读取变量。
  ReadVariable = NOT_FOUND
Else
  ' 获取一个更小的子字符串进行处理。
  strTemp = Right(Document.Cookie, Len(Document.Cookie) _
                      - intLocation + 1)

  ' 检查并确保找到了完整的字符串,而不仅仅是一个
  ' 子字符串。
  If Mid(strTemp, intNameLength + 1, 1) <> "=" Then
    '啊呀,只找到子字符串,而没有找到完整的字符串。
    ReadVariable = NOT_FOUND

    ' 请注意,当且仅当对其名称为上一个变量
    ' 的子字符串的变量执行搜索时,它会错误地给出
    ' “未找到”的结果。例如,这将会导致
    ' 失败:
    ' 搜索:MyVar
    ' Cookie 包含:MyVariable=2;MyVar=1

  Else
    ' 找到完整的字符串。
    intNextSemicolon = Instr(strTemp, ";")

    ' 如果未找到,则获取 cookie 的最后一个元素。
    If intNextSemicolon = 0 Then intNextSemicolon = Len(strTemp) _
                                 + 1

    ' 检查空变量 (Var1=;)。
    If intNextSemicolon = (intNameLength + 2) Then
      ' 变量为空。
      ReadVariable = ""
    Else
      ' 正常计算此值。
      intValueLength = intNextSemicolon - intNameLength – 2
      ReadVariable = Mid(strTemp, intNameLength + 2, _
                         intValueLength)
    End If
  End If
End If
End Function
</SCRIPT>

创建一个根据特定参数显示记录的页面
在“设计”视图中打开一个新的数据访问页。


在页面上添加所需字段。


在结尾的 <HEAD> 前添加代码,以读取 cookie 并检索与指定参数相匹配的记录。
以下是“参数结果”页所使用的示例脚本。该脚本从“Employee Sales by Country”查询中检索在开始和结束日期之间的记录。

<SCRIPT language=VBScript>

Dim pStartingDate
Dim pEndingDate
Dim errNot_Found
errNot_Found = "NOT_FOUND"
pStartingDate = ReadVariable("StartingDate")
pEndingDate = ReadVariable("EndingDate")

If ((pStartingDate = errNot_Found) or _
          (pEndingDate = errNot_Found)) Then
  pStartingDate = inputbox("请输入一个开始日期:", _
          "开始日期")
  pEndingDate = inputbox("请输入一个结束日期:","结束日期")
End If

' 以下是 Jet 语法,用于将参数值添加到
' 数据源控件。
MSODSC.RecordsetDefs("Employee Sales by Country").parametervalues _
                .Add "[Starting Date]", pStartingDate
MSODSC.RecordsetDefs("Employee Sales by Country").parametervalues _
                .Add "[Ending Date]", pEndingDate

' 删除 cookie。
document.cookie = ""

' 以下是 SQL Server 语法,用于将参数值添加到
' 数据源控件。
' document.msodsc.recordsetdefs(0).parametervalues.Add "&ParmName", _
' parameter_value

</SCRIPT>

向页面脚本添加函数以写入、读取和删除 cookie。
<SCRIPT language=VBS>
OPTION EXPLICIT
Dim NOT_FOUND
NOT_FOUND = "NOT_FOUND"

Sub SetVariable(strVariableName, varVariableValue)
  Document.Cookie = strVariableName & "=" & varVariableValue
End Sub

Sub KillVariable(strVariableName)
  SetVariable strVariableName, "NULL;expires=Monday, 01-Jan-95 _
                         12:00:00 GMT"
End Sub

Function ReadVariable(strVariableName)
' 在用于查找 cookie 中变量的字符串
' 处理代码中使用这五个变量。
Dim intLocation
Dim intNameLength
Dim intValueLength
Dim intNextSemicolon
Dim strTemp

' 计算变量名的长度和位置。
intNameLength = Len(strVariableName)
intLocation = Instr(Document.Cookie, strVariableName)

' 检查是否存在变量名。
If intLocation = 0 Then
  ' 找不到变量,因此无法读取变量。
  ReadVariable = NOT_FOUND
Else
  ' 获取一个更小的子字符串进行处理。
  strTemp = Right(Document.Cookie, Len(Document.Cookie) _
                      - intLocation + 1)

  ' 检查并确保找到了完整的字符串,而不仅仅是一个
  ' 子字符串。
  If Mid(strTemp, intNameLength + 1, 1) <> "=" Then
    ' 啊呀,只找到子字符串,而没有找到完整的字符串。
   ReadVariable = NOT_FOUND

    ' 请注意,当且仅当对其名称为上一个变量
    ' 的子字符串的变量执行搜索时,它会错误地给出
    ' “未找到”的结果。例如,这将会导致
    ' 失败:
    ' 搜索:MyVar
    ' Cookie 包含:MyVariable=2;MyVar=1

  Else
    ' 找到完整的字符串。
    intNextSemicolon = Instr(strTemp, ";")

    ' 如果未找到,则获取 cookie 的最后一个元素。
    If intNextSemicolon = 0 Then intNextSemicolon = Len(strTemp) _
                                  + 1
    ' 检查空变量 (Var1=;)
    If intNextSemicolon = (intNameLength + 2) Then
      ' 变量为空。
      ReadVariable = ""
    Else
      ' 正常计算此值。
      intValueLength = intNextSemicolon - intNameLength – 2
      ReadVariable = Mid(strTemp, intNameLength + 2, _
                         intValueLength)
    End If
  End If
End If
End Function



  2002年1月8日  阅读 1022 次  发送此页给朋友  来源:    版权争议  删除

相关文章:   近期热点:

上一篇: Microsoft SQL Server 2000 分布式查询:OLE DB 连接
下一篇: 使用 Microsoft 数据引擎创建和部署 Access 解决方案
返回上一层...
搜索:

(C)2004-2022 7i24.Com 保留所有权利