简体   繁体   中英

How to re-write a passthrough query on the fly using a MS Access form?

I wrote a stored procedure to copy, re-write, and then append the new modified data. This stored procedure requires two parameters. In access I have a working passthrough query that executes my SP like the following

execute [dbo].[CopyConData_StoS @Original = '1144', @Copy = '898'

The ODBC Connect Str points to the develoment database where my SP lives.

ODBC;DSN=DevDB;UID=jsmith;Trusted_Connection=Yes;DATABASE=TRDatabase

I want to create a form and re write the SQL on the fly so I can input whatever parameters I want into fields and the SP use them when executing my SP.

I created a form and inspected the form's On Click Event Procedure and was presented with the following

Private Sub cmdRunPT_Click()

    Dim strSQL As String
    Dim qd As QueryDef
    Dim db As Database

    strSQL = "Execute...."      ' Modify as needed according to values in form's text boxes

    Set db = CurrentDb
    Set qd = db.QueryDefs("MyPassThruQuery")
    qd.SQL = strSQL
    qd.Execute dbFailOnError

    Set qd = Nothing
    Set db = Nothing

End Sub

I am trying to figure out how I can modify that to run my passthrough and be able to input the two parameters manually. Would I also need to pass in my connection string so it knows to use my dev db?

I'm basically wanting to use a form in MSAccess to allow me to input the two parameters needed to run the SP. In the passthrough query I gave an example of the two parameters are hard coded. I'd like to have a form where I can just input those. So I need to re-write the query on the fly some how.

Thanks

Current code - telling me Cannot execute select query

Private Sub Command8_Click()

    Dim strSQL As String
    Dim qd As QueryDef
    Dim db As Database

    strSQL = "Execute [dbo].[CopyConData_StoS] " & _
    " @Original = '" & Me.Original & "', @Copy = '" & Me.Copy & "'"

    Set db = CurrentDb
    Set qd = db.QueryDefs("CopyConData_StoS")
    qd.SQL = strSQL
    qd.Execute dbFailOnError

    Set qd = Nothing
    Set db = Nothing

End Sub

What you have looks fine.

Assuming that you created and tested the pt query in Access, you have say this:

execute [dbo].[test4] 'a', 'b'

Try running the query – it should work just fine.

Once (and only after) you have the query working, then in code what you have should work fine, or even this will work:

With CurrentDb.QueryDefs("CopyConData_StoS")
    .SQL = strSQL
   .Execute
 End With

Note how the above eliminates the need to declare any vars except for your strSQL

However, if the PT query returns records, then you want to change above to:

 Dim strSQL  As String
‘ code here to setup strSQL

Dim rst     As DAO.Recordset
With CurrentDb.QueryDefs("CopyConData_StoS")
   .SQL = strSQL
   .ReturnsRecords = True
   Set rst = .OpenRecordset
End With

So it really comes down to if you to “execute” a action query, or that the proc returns records. If it returns records, then you can't use execute but use OpenRecordSet.

As pointed out you don't need to mess or set the connection string in code if the PT query already been created and setup). In fact you best leave the connection stuff out of your code – as above shows this thus allows rather simple use of t-sql via a PT query saved in Access.

Also check if the 2 params are string (nvchar()) or numbers – if one of the parameters is a number then you want to drop the single quotes. Best to test with a working PT query in Access before you write any VBA. (it seems you checked and they are strings)

I may be missing something, but your code seems almost complete.

Assuming you have text boxes txtOriginal and txtCopy on your form (and hopefully have validation rules to limit their values to positive numbers), you need:

strSQL = "Execute [dbo].[CopyConData_StoS] " & _
         " @Original = '" & Me.txtOriginal & "', @Copy = '" & Me.txtCopy & "'"

Debug.Print strSQL   ' goes into Direct Window (Ctrl+g) for debugging

Everything else is already in place.

Note: your parameters seem like numbers (IDs), not sure why you are passing them as strings (using single quotes).

Unless the connection string of MyPassThruQuery has changed, you don't need to re-assign it.
qd.SQL = strSQL is all you need.

Edit

For the error message, two things:

  • Ditch dbFailOnError , it is only valid for normal Access action queries.
  • Open your Pass-Through query in design view, open the property sheet, make sure that Returns records is False.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM