Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access 访问传递查询给定错误_Ms Access_Vba - Fatal编程技术网

Ms access 访问传递查询给定错误

Ms access 访问传递查询给定错误,ms-access,vba,Ms Access,Vba,我使用下面的代码来运行passthrough查询。我试图运行passthrough查询,然后检查返回了多少条记录,以确定它是否有效。但是我有一个错误,说 “无效操作” 它为什么这样做?我如何纠正 Dim Item As String Item = InputBox("Enter Item needing a surrogate UPC.", "Enter Item") Set db = CurrentDb Set qdf = db.QueryDefs("spAL_AssignSur

我使用下面的代码来运行passthrough查询。我试图运行passthrough查询,然后检查返回了多少条记录,以确定它是否有效。但是我有一个错误,说

“无效操作”

它为什么这样做?我如何纠正

  Dim Item As String
  Item = InputBox("Enter Item needing a surrogate UPC.", "Enter Item")


Set db = CurrentDb

Set qdf = db.QueryDefs("spAL_AssignSurrogateUPC")
qdf.ReturnsRecords = True
qdf.SQL = "spAL_AssignSurrogateUPC '" & Item & "'"
With qdf.OpenRecordset(dbOpenSnapshot)       '<--- Error triggered here.
    If qdf.RecordCount = 1 Then
        MsgBox "Surrogate UPC assigned."
    Else
        MsgBox "ERROR.  Could not assign surrogate UPC."
    End If
End With
Dim项作为字符串
Item=InputBox(“输入需要代理UPC的项目”,“输入项目”)
Set db=CurrentDb
设置qdf=db.querydfs(“spAL_assignprograteupc”)
qdf.ReturnsRecords=True
qdf.SQL=“spAL\u assignprogrationteupc'”和Item&“
对于qdf.OpenRecordset(dbOpenSnapshot)“使用EXEC语法

qdf.SQL = "EXEC spAL_AssignSurrogateUPC '" & Replace(Item, "'", "''") & "';" 
(我假设它是SQL Server。)

我还添加了一个replace函数,以处理
Item
字符串中的单引号。这也有助于防止SQL注入


编辑:

尝试这样做,而不是使用With语句

Dim rs as DAO.Recordset

...

Set rs = qdf.OpenRecordset(dbOpenSnapshot)
If rs.EOF Then
    MsgBox "ERROR.  Could not assign surrogate UPC."
Else
    MsgBox "Surrogate UPC assigned."
End If

spAL_AssignProgregateUpc是否返回行?@ConradFrix-如果有效,则返回1行,如果无效,则不返回任何内容。替换将无法编译。抱歉,您使用的是VBA,而不是VB.NET。我还添加了另一种测试查询结果的方法。请参阅我的编辑。