MS 访问 vba - 错误
MS ACCESS vba - error
我正在创建 MS Access 报告。我遇到了一个小问题,我不知道哪里出了问题。所以我想做的是
1) Select 查询中的所有内容(查询需要 "TO" 和 "FROM" 日期。我将这些值传递给 frmX,然后在查询中引用它)。当我 运行 在打开 frmX 的情况下自行查询时 - 它 运行 没问题。
2)我试图改变数据中的一些值
3) 将新值插入 tempTable1
这是我的代码:
dim rs1 as DAO.Recordset
dim rs2 as DAO.Recordset
CurrentDb.Execute "DELETE FROM [tempProvider-Detail]"
'Repopulating temp table
DoCmd.OpenQuery "qryProvider-FINAL"
'Input Source
Set rs1 = CurrentDb.OpenRecordset("Select * from [qryProvider-Final]", , dbOpenSnapshot)
'Target Source
Set rs2 = CurrentDb.OpenRecordset("Select * from tempProvider-DETAIL", dbOpenDynamic)
这里有趣的是它不会挂断 DoCMD.OpenQuery - 但是当我开始设置 rs1 ...... 然后它告诉我它需要 2 个参数。我不知道为什么 - 因为查询已经打开 - 当我尝试自己打开它时它工作正常它打开(我在查询中引用的 frmX 中有日期)。
请帮帮我!
所以我按照 Heinzi 的帮助做了这个.. 仍然得到同样的错误怎么了??????
DoCmd.OpenQuery "qryProvider-FINAL"
Set qdf = CurrentDb.QueryDefs("qryProvider-FINAL")
qdf.Parameters(0) = [Forms]![frmX]![txtFrom]
qdf.Parameters(1) = [Forms]![frmX]![txtTo]
Set rs1 = qdf.OpenRecordset
strSQL = "SELECT * FROM [qryProvider-FINAL];"
'Input Source
Set rs1 = CurrentDb.OpenRecordset(strSQL, , dbOpenSnapshot) ---this is where it hangs up
使用 CurrentDb.OpenRecordset 打开记录集时不能引用表单控件。它只是不受支持。详细信息可以在以下 MSDN 文章中找到:
The answer is that youre invoking the Jet engine in a different context here, and that makes all the difference. When you get data from a parameter query that uses a form to supply the parameter via the Access user interface, as in the earlier example, Access can evalute the expression involved and supply a value to Jet. When you get data from a parameter query that uses a form to supply the parameter via VBA, instead of through a form, the bits of Access that manage user interface matters arent involved. Consequently, Jet is passed the string "[Forms]![frmSelectCountry]![cboCountry]" instead of the value in cboCountry. Because Jet doesnt know how to evaluate the expression, it cant open the recordset.
这行得通吗:
Sub Test()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rst As DAO.Recordset
Dim prm As DAO.Parameter
Set db = CurrentDb
Set qdf = db.QueryDefs("qryProvider-FINAL")
For Each prm In qdf.Parameters
prm.Value = Eval(prm.Name)
Next prm
Set rst = qdf.OpenRecordset
End Sub
我正在创建 MS Access 报告。我遇到了一个小问题,我不知道哪里出了问题。所以我想做的是
1) Select 查询中的所有内容(查询需要 "TO" 和 "FROM" 日期。我将这些值传递给 frmX,然后在查询中引用它)。当我 运行 在打开 frmX 的情况下自行查询时 - 它 运行 没问题。 2)我试图改变数据中的一些值 3) 将新值插入 tempTable1
这是我的代码:
dim rs1 as DAO.Recordset
dim rs2 as DAO.Recordset
CurrentDb.Execute "DELETE FROM [tempProvider-Detail]"
'Repopulating temp table
DoCmd.OpenQuery "qryProvider-FINAL"
'Input Source
Set rs1 = CurrentDb.OpenRecordset("Select * from [qryProvider-Final]", , dbOpenSnapshot)
'Target Source
Set rs2 = CurrentDb.OpenRecordset("Select * from tempProvider-DETAIL", dbOpenDynamic)
这里有趣的是它不会挂断 DoCMD.OpenQuery - 但是当我开始设置 rs1 ...... 然后它告诉我它需要 2 个参数。我不知道为什么 - 因为查询已经打开 - 当我尝试自己打开它时它工作正常它打开(我在查询中引用的 frmX 中有日期)。
请帮帮我!
所以我按照 Heinzi 的帮助做了这个.. 仍然得到同样的错误怎么了??????
DoCmd.OpenQuery "qryProvider-FINAL"
Set qdf = CurrentDb.QueryDefs("qryProvider-FINAL")
qdf.Parameters(0) = [Forms]![frmX]![txtFrom]
qdf.Parameters(1) = [Forms]![frmX]![txtTo]
Set rs1 = qdf.OpenRecordset
strSQL = "SELECT * FROM [qryProvider-FINAL];"
'Input Source
Set rs1 = CurrentDb.OpenRecordset(strSQL, , dbOpenSnapshot) ---this is where it hangs up
使用 CurrentDb.OpenRecordset 打开记录集时不能引用表单控件。它只是不受支持。详细信息可以在以下 MSDN 文章中找到:
The answer is that youre invoking the Jet engine in a different context here, and that makes all the difference. When you get data from a parameter query that uses a form to supply the parameter via the Access user interface, as in the earlier example, Access can evalute the expression involved and supply a value to Jet. When you get data from a parameter query that uses a form to supply the parameter via VBA, instead of through a form, the bits of Access that manage user interface matters arent involved. Consequently, Jet is passed the string "[Forms]![frmSelectCountry]![cboCountry]" instead of the value in cboCountry. Because Jet doesnt know how to evaluate the expression, it cant open the recordset.
这行得通吗:
Sub Test()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rst As DAO.Recordset
Dim prm As DAO.Parameter
Set db = CurrentDb
Set qdf = db.QueryDefs("qryProvider-FINAL")
For Each prm In qdf.Parameters
prm.Value = Eval(prm.Name)
Next prm
Set rst = qdf.OpenRecordset
End Sub