Pass Through Query from SQL

I have a Pass-Through query that is pulling from a SQL procedure. When I put in a form control as the criteria in my query that is using the Pass-Through as the source it doesn't seem to work at all and returns 0 records. But when I change the criteria and hard-code it to a specific value it works fine. Is there something special when using a Pass-Through as the source?
LVL 1
Lawrence SalvucciInformation Technology ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dale FyeOwner, Developing Solutions LLCCommented:
This is because SQL Server has know idea what the value is on your forms control.

Generally, when I want to use a pass-through and a variable value (something that comes from a form), I'll revise the SQL of the pass-thru query in the afterupdate event of the control.  Something like:

Private Sub txt_SomeValue_AfterUpdate

    Dim strSQL as string

    strSQL = "SELECT [Field1], [Field2], [Field3] FROM yourTable " _
                 & "WHERE [SomeTextField] = '" & me.txt_SomeTextField & "'"
    currentdb.querydefs("yourSavedPassThruQuery").SQL = strSQL

End Sub

Then you can call that query and return the results that are specific to the control on your form.
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
This pass-through query is the source for my subform but I want it to requery when a combobox on my main form is changed. Should I put this SQL code in the AfterUpdate event of my combobox on my main form?
Dale FyeOwner, Developing Solutions LLCCommented:
Yes.  But that could cause problems.  Start with something like:
Private Sub txt_SomeValue_AfterUpdate

    Dim strSQL as string

    strSQL = "SELECT [Field1], [Field2], [Field3] FROM yourTable " _
                 & "WHERE [SomeTextField] = '" & me.txt_SomeTextField & "'"
    currentdb.querydefs("yourSavedPassThruQuery").SQL = strSQL
    me.subformControlName.Form.Requery

End Sub

Open in new window


However, since that query is already in use on the active subform, this may generate an error, something like "sorry, cannot do this right now".  If that happens, you will need to release the lock on that query by setting the sourceObject of your subform to "" and then resetting it.  Something like:
Private Sub txt_SomeValue_AfterUpdate

    Dim strSourceObject as string 
    Dim strSQL as string

    strSourceObject = me.subformControlName.SourceObject
    me.subformControlName.SourceObject = ""
    strSQL = "SELECT [Field1], [Field2], [Field3] FROM yourTable " _
                 & "WHERE [SomeTextField] = '" & me.txt_SomeTextField & "'"
    currentdb.querydefs("yourSavedPassThruQuery").SQL = strSQL
    me.subformControlName.SourceObject = strSourceObject

End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.