为 VBA 记录集传递参数
Passing A Parameter for a VBA Recordset
我有一个查询有一个参数不属于结果列的一部分,如下所示。
它通常在我执行查询时弹出。
PARAMETERS SelectedDate DateTime;
Select col1,col2, col3 from qry
where col4 = [SelectedDate]
我需要为 col1 和 col2 创建一个 VBA 记录集。
谁能告诉我如何传递这个参数?有可能吗?
据我了解,您想将参数从 VBA 传递到访问查询?
如果是这样,您需要在 VBA 中创建一个模块。
可能是这样的:
Public Str As String
Public Function str_move() As String
str_move = Str
End Function
因此 Str 是您在 VBA 中的变量。
在您的查询中,您现在可以编写 str_move()
并因此获得此 Str 值。
注意:模块名称不能与函数名称相同。将模块命名为 m_MoveVariables
没关系 - 查询中未使用模块名称。
希望这对你有用。
您可以使用 SELECT
语句作为 DAO.Recordset
的数据源,然后在打开它之前提供参数值。
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rs As DAO.Recordset
Dim strSelect As String
strSelect = "PARAMETERS SelectedDate DateTime;" & vbCrLf & _
"Select col1,col2, col3 from qry" & vbCrLf & _
"where col4 = [SelectedDate];"
Debug.Print strSelect '<-- inspect this in Immediate window; Ctrl+g to go there
Set db = CurrentDb
Set qdf = db.CreateQueryDef(vbNullString, strSelect)
qdf.Parameters("SelectedDate").Value = Date '<-- whatever Date/Time value you want here
Set rs = qdf.OpenRecordset
您甚至可以简单地使用 Recordset,而不使用 QueryDef 对象,
Dim rsObj As DAO.Recordset
Set rsObj = CurrentDB.OpenRecordset("SELECT col1, col2 FROM qry WHERE col4 = " & _
Format(Date(), "\#mm\/dd\/yyyy\#"))
'Date() can be replace with any Date or Date Variable.
我有一个查询有一个参数不属于结果列的一部分,如下所示。
它通常在我执行查询时弹出。
PARAMETERS SelectedDate DateTime;
Select col1,col2, col3 from qry
where col4 = [SelectedDate]
我需要为 col1 和 col2 创建一个 VBA 记录集。
谁能告诉我如何传递这个参数?有可能吗?
据我了解,您想将参数从 VBA 传递到访问查询?
如果是这样,您需要在 VBA 中创建一个模块。
可能是这样的:
Public Str As String
Public Function str_move() As String
str_move = Str
End Function
因此 Str 是您在 VBA 中的变量。
在您的查询中,您现在可以编写 str_move()
并因此获得此 Str 值。
注意:模块名称不能与函数名称相同。将模块命名为 m_MoveVariables
没关系 - 查询中未使用模块名称。
希望这对你有用。
您可以使用 SELECT
语句作为 DAO.Recordset
的数据源,然后在打开它之前提供参数值。
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rs As DAO.Recordset
Dim strSelect As String
strSelect = "PARAMETERS SelectedDate DateTime;" & vbCrLf & _
"Select col1,col2, col3 from qry" & vbCrLf & _
"where col4 = [SelectedDate];"
Debug.Print strSelect '<-- inspect this in Immediate window; Ctrl+g to go there
Set db = CurrentDb
Set qdf = db.CreateQueryDef(vbNullString, strSelect)
qdf.Parameters("SelectedDate").Value = Date '<-- whatever Date/Time value you want here
Set rs = qdf.OpenRecordset
您甚至可以简单地使用 Recordset,而不使用 QueryDef 对象,
Dim rsObj As DAO.Recordset
Set rsObj = CurrentDB.OpenRecordset("SELECT col1, col2 FROM qry WHERE col4 = " & _
Format(Date(), "\#mm\/dd\/yyyy\#"))
'Date() can be replace with any Date or Date Variable.