有很多方法可以在查询中使用参数。我将尝试为其中的大多数示例以及适用的示例提供示例。
首先,我们将讨论Access独有的解决方案,例如表单,报表和域聚合。然后,我们将讨论DAO和ADO。
在Access中,您可以直接在sql代码中使用表单和报表上控件的当前值。这限制了对参数的需求。
您可以通过以下方式引用控件:
Forms!MyForm!MyText@R_126_2419@
用于对表单进行简单控制
Forms!MyForm!MySubform.Form!MyText@R_126_2419@
用于子窗体上的控件
Reports!MyReport!MyText@R_126_2419@
用于控制报告
DoCmd.Runsql "INSERT INTO Table1(Field1) SELECT Forms!MyForm!MyText@R_126_2419@" 'Inserts a single value
DoCmd.Runsql "INSERT INTO Table1(Field1) SELECT Field1 FROM Table2 WHERE ID = Forms!MyForm!MyText@R_126_2419@" 'Inserts from a different table
使用时DoCmd.Runsql
,常规查询(在GUI中),表单和报告记录源,表单和报告过滤器,域聚合DoCmd.OpenForm
以及DoCmd.OpenReport
使用DAO或ADODB执行查询时(例如,打开记录集CurrentDb.Execute
)
Access中的TempVar是全局可用的变量,可以在VBA中或使用宏进行设置。它们可以重用于多个查询。
TempVars!MyTempVar = Me.MyText@R_126_2419@.Value 'Note: .Value is required
DoCmd.Runsql "INSERT INTO Table1(Field1) SELECT Field1 FROM Table2 WHERE ID = TempVars!MyTempVar"
TempVars.Remove "MyTempVar" 'Unset TempVar when you're done using it
TempVars的可用性与表单和报表中的值相同:不适用于ADO和DAO,可用于其他用途。
我建议使用TempVars在打开窗体或报告控件名称时使用参数,因为如果打开它的对象关闭,则TempVars仍然可用。我建议为每个表单或报表使用唯一的TempVar名称,以避免在刷新表单或报表时出现怪异现象。
就像TempVars一样,您可以使用自定义函数和静态变量来存储和检索值。
Option Compare Database
Option Explicit
Private ThisDate As Date
Public Function GetThisDate() As Date
If ThisDate = #12:00:00 AM# Then
' Set default value.
ThisDate = Date
End If
GetThisDate = ThisDate
End Function
Public Function SetThisDate(ByVal NewDate As Date) As Date
ThisDate = NewDate
SetThisDate = ThisDate
End Function
接着:
SetThisDate SomeDateValue ' Will store SomeDateValue in ThisDate.
DoCmd.Runsql "INSERT INTO Table1(Field1) SELECT Field1 FROM Table2 WHERE [SomeDateField] = GetThisDate()"
同样,可以为设置和获取私有静态变量的值创建带有 可选 参数的 单个 函数: __
Public Function ThisValue(Optional ByVal Value As Variant) As Variant
Static CurrentValue As Variant
' Define default return value.
Const DefaultValue As Variant = Null
If Not IsMissing(Value) Then
' Set value.
CurrentValue = Value
ElseIf IsEmpty(CurrentValue) Then
' Set default value
CurrentValue = DefaultValue
End If
' Return value.
ThisValue = CurrentValue
End Function
设置值:
ThisValue "Some text value"
要获取值:
CurrentValue = ThisValue
在查询中:
ThisValue "SomeText" ' Set value to filter on.
DoCmd.Runsql "INSERT INTO Table1(Field1) SELECT Field1 FROM Table2 WHERE [SomeField] = ThisValue()"
的用途DoCmd.SetParameter
相当有限,因此我将作简要介绍。它允许您设置使用的参数DoCmd.OpenForm
,DoCmd.OpenReport
以及一些其他的DoCmd
语句,但它不工作DoCmd.Runsql
,过滤器,DAO和ADO。
DoCmd.SetParameter "MyParameter", Me.MyText@R_126_2419@
DoCmd.OpenForm "MyForm",,, "ID = MyParameter"
在DAO中,我们可以使用DAO.QueryDef
对象创建查询,设置参数,然后打开记录集或执行查询。您首先设置查询的sql,然后使用QueryDef.Parameters
集合来设置参数。
在我的示例中,我将使用隐式参数类型。如果要使它们显式,请在查询中添加一个PARAMETERS
声明。
'Execute query, unnamed parameters
With CurrentDb.CreateQueryDef("", "INSERT INTO Table1(Field1) SELECT Field1 FROM Table2 WHERE Field1 = ?p1 And Field2 = ?p2")
.Parameters(0) = Me.Field1
.Parameters(1) = Me.Field2
.Execute
End With
'Open recordset, named parameters
Dim rs As DAO.Recordset
With CurrentDb.CreateQueryDef("", "SELECT Field1 FROM Table2 WHERE Field1 = FirstParameter And Field2 = SecondParameter")
.Parameters!FirstParameter = Me.Field1 'Bang notation
.Parameters("SecondParameter").Value = Me.Field2 'More explicit notation
Set rs = .OpenRecordset
End With
尽管仅在DAO中可用,但是您可以为DAO记录集设置许多内容,以使它们使用参数,例如表单记录集,列表框记录集和组合框记录集。但是,由于Access在排序和筛选时使用的是文本而不是记录集,因此如果您这样做,则可能会出现问题。
您可以通过使用ADODB.Command
对象在ADO中使用参数。使用Command.CreateParameter
创建的参数,然后将它们添加到Command.Parameters
收藏。
您可以使用.Parameters
ADO中的集合来显式声明参数,或将参数数组传递给Command.Execute
方法以隐式传递参数。
ADO不支持命名参数。虽然您可以传递名称,但不会对其进行处理。
'Execute query, unnamed parameters
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
With cmd
Set .ActiveConnection = CurrentProject.Connection 'Use a connection to the current database
.CommandText = "INSERT INTO Table1(Field1) SELECT Field1 FROM Table2 WHERE Field1 = ? And Field2 = ?"
.Parameters.Append .CreateParameter(, adVarWChar, adParamInput, Len(Me.Field1), Me.Field1) 'adVarWChar for text @R_126_2419@es that may contain unicode
.Parameters.Append .CreateParameter(, adInteger, adParamInput, 8, Me.Field2) 'adInteger for whole numbers (long or integer)
.Execute
End With
'Open recordset, implicit parameters
Dim rs As ADODB.Recordset
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
With cmd
Set .ActiveConnection = CurrentProject.Connection 'Use a connection to the current database
.CommandText = "SELECT Field1 FROM Table2 WHERE Field1 = @FirstParameter And Field2 = @SecondParameter"
Set rs = .Execute(,Array(Me.Field1, Me.Field2))
End With
适用与打开DAO记录集相同的限制。尽管这种方式仅限于执行查询和打开记录集,但是您可以在应用程序中的其他位置使用这些记录集。