【问题标题】:How to enter a parameter in a Where clause?如何在 Where 子句中输入参数?
【发布时间】:2016-11-09 21:01:21
【问题描述】:

使用这个 VBA 代码我得到一个错误:

参数太少。预计 1。

我不知道如何正确输入。

Dim rs_invoice As DAO.Recordset
Set rs_invoice = CurrentDb.OpenRecordset("SELECT * FROM order_tbl WHERE invoice_no Is Null AND company_name='" _
 & Me.cmb_start_company.Column(1) & "' AND shiped=" & "True")


If Not (rs_invoice.EOF And rs_invoice.BOF) Then
    rs_invoice.MoveFirst
    Do Until rs_invoice.EOF = True
        rs_invoice.Edit
        rs_invoice!invoice_no = invoice_number
        rs_invoice.Update

        rs_invoice.MoveNext
    Loop
Else
   'No records
End If

'Finish


rs_invoice.Close 'Close the recordset
Set rs_invoice = Nothing 'Clean up

【问题讨论】:

  • 难道你不能用一个 SQL 命令按照UPDATE order_tbl SET invoice_no = " & invoice_number & " WHERE invoice_no Is Null AND company_name='" & Me.cmb_start_company.Column(1) & "' AND shiped=True 的行做同样的事情吗?虽然我提倡使用 SQL 参数而不是字符串连接。
  • 谢谢,但我真的不知道如何使用此代码,您可以发布示例吗?谢谢
  • 我不熟悉VBA,但看起来What is the VBA equivalent for using Command.Prepare in ADO.NET可以帮助你。
  • 我遇到了同样的错误:/

标签: database vba ms-access where


【解决方案1】:

只是猜测。你可以试试这个:

"SELECT * FROM order_tbl WHERE (invoice_no Is Null) AND (company_name='" _
 & Me.cmb_start_company.Column(1) & "' )AND (shiped=" & "True")

选项 2:

dim str_test as string
str_test =     "SELECT * FROM order_tbl WHERE (invoice_no Is Null) AND (company_name='" & Me.cmb_start_company.Column(1) & "' )AND (shiped=" & "True")

debug.print str_test

您在即时窗口中得到了什么?

选项 3:

  "SELECT * FROM order_tbl WHERE (invoice_no Is Null) AND (company_name='" _
     & Me.cmb_start_company.Column(1) & "' )AND (shiped=True)"

选项 4: 然后在这里使用示例,它应该可以工作。 https://msdn.microsoft.com/en-us/library/bb243786(v=office.12).aspx 可能。只需确保以相同的方式设置记录集:

Dim dbsNorthwind As DAO.Database
Dim rstProducts As DAO.Recordset
Dim strSQL As String

Set dbsNorthwind = CurrentDb
strSQL = "SELECT * FROM Products WHERE Discontinued = No " & _
         "ORDER BY ProductName"
Set rstProducts = dbsNorthwind.OpenRecordset(strSQL)

【讨论】:

  • @Pecurka - 见选项 2
  • 我在即时窗口 SELECT * FROM order_tbl WHERE (invoice_no Is Null) AND (company_name='Bogdanic GmbH doo') AND (shiped=True)
  • 酷。现在你可以将它作为 sql 粘贴到 MS Access 中,看看你是否得到了一些结果? quackit.com/pix/microsoft_access/microsoft_access_2016/tutorial/…
  • 是的,我在查询中得到了结果,但我在表中的 vba 中制作了这段代码,而不是在查询中
  • 好吧,我只是在调试。你在哪一行得到错误?
【解决方案2】:

我对 Access/DAO 不是很熟悉,但您将参数值连接到查询中。使用 ADO,您的命令文本将看起来像这样:

Const sql As String = "SELECT * FROM order_tbl WHERE invoice_no Is Null AND company_name = ? AND shiped = True"

? 是参数值的占位符;而不是直接调用OpenRecordset(sql),您需要创建一个Command 并添加一个正确的Parameter

似乎 DAO 对此的处理方式有所不同,请参阅 Parameterized queries in Access - 其中一个答案肯定适用于此。因为我不喜欢随机的临时查询访问我的数据库,所以我可能会使用this answer 中的QueryDefs 方法:

Dim qdf As Querydef
Dim rst As Recordset

'then we'll open up the query:
Set qdf = CurrentDB.QueryDefs(qryname)

'Now we'll assign values to the query using the parameters option:
qdf.Parameters(0) = qryStartDate
qdf.Parameters(1) = qryEndDate

'Now we'll convert the querydef to a recordset and run it
Set rst = qdf.OpenRecordset

那就是:

Dim query As QueryDef
Set query = CurrentDB.QueryDefs("TheQuery")

query.Parameters(0) = Me.cmb_start_company.Column(1)

Dim result As Recordset
Set result = query.OpenRecordset

【讨论】:

  • 刚刚意识到,我不知道错误消息是如何说参数是预期的,因为它是一个临时查询。尽管如此,一种不同的、更强大的方法也不会受到伤害。
  • 我已经用新的查询完成了它。谢谢大家:)
猜你喜欢
  • 2010-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多