0

I'm using the following code to run a passthrough query. I'm trying to run the passthrough query, then check how many records are returned to figure out if it worked. But I get an error saying

"Invalid Operation"

Why is it doing this and how can I correct?

  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
2
  • does spAL_AssignSurrogateUPC return rows? Commented Jan 25, 2012 at 17:45
  • @ConradFrix -If it works it returns 1 row, if it doesn't work it returns nothing. Commented Jan 25, 2012 at 17:46

1 Answer 1

1

Use the EXEC syntax

qdf.SQL = "EXEC spAL_AssignSurrogateUPC '" & Replace(Item, "'", "''") & "';" 

(I assume it's SQL Server.)

I also added a replace function in order to cope with single quotes in the Item string. This also helps prevent SQL injections.


EDIT:

Try to do this, instead of using the With-statement

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
Sign up to request clarification or add additional context in comments.

3 Comments

And the replace won't compile.
Sorry, you are using VBA, not VB.NET. It changed it.
I also added another way of testing the result of the query. See my edit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.