简体   繁体   中英

Pass through query with userinput in access form

I have Access front end which is powered by SQL Server in backend.

I made a form which requires user input, based on the input it should run a pass through query and should display result in the subform. I have read on internet this cannot be done through sql command and it requires sql coding to be wrapped in VBA coding.

I have made a code this like this:

Private Sub id_AfterUpdate()
Dim MyDb As Database, MyQry As QueryDef
Set MyDb = CurrentDb()
Set MyQry = MyDb.CreateQueryDef("")

MyQry.Connect = "ODBC;DSN=mikecollections;UID=***;PWD=****;DATABASE=mikecollections;"
MyQry.SQL = "select currency.tb_coins.id,documenttype,documentsubtype,documentname" & _
            "from currency.tb_coins" & _
            "inner join collectibles.tb_documents on tb_coins.id=collectibles.tb_documents.prodid" & _
            "where currency.tb_coins.id=[forms]![test_form]![id]"

End Sub

This code should fire after I enter value in the id field in the form, but nothing happens. I do not know how to make this code work. Im new to SQL and VBA, Pls help!

You logically can't refer to an Access object in a passthru query.
So you sql should pass the value of the control, instead of referencing the control itself.
If id is a long :

MyQry.SQL = "select currency.tb_coins.id,documenttype,documentsubtype,documentname" & _
            "from currency.tb_coins" & _
            "inner join collectibles.tb_documents on tb_coins.id=collectibles.tb_documents.prodid" & _
            "where currency.tb_coins.id= " & [forms]![test_form]![id]

Then you need to add the code to assign that querydef to a recordset , in order to read it. Something like:

set rs = myQry.OpenRecordset

If you prefer, you can also name your queryDef. it will then be available like any other query:

Set MyQry = MyDb.CreateQueryDef("someName")
'''specify all properties here...
doCmd.OpenQuery "someName"

I created a stored procedure in sql server and created a parameter in access to pass into sql server and got it working. Thank you Hansup and iDevlop for all the help.

Private Sub userinput_AfterUpdate()
Dim qrypass As DAO.QueryDef
Dim rst As DAO.Recordset

Set qrypass = CurrentDb.QueryDefs("qu_test")
qrypass.SQL = _
    "exec collectibles.sp_coinsvsdocuments " _
    & "@userinput=" _
    & Forms!test_form!userinput

End Sub

Here is a solution for all Microsoft Access

just copy the below code into a Microsoft Access Module and change the parameter

Sub test()
' The "Kim_ZZ_Orderlines" has a string 'arg1' in the sql so we just replace this 
' with the new value

RunPassThrough "KIM_ZZ_Orderlines", "Indput value", "10"
End Sub


Sub RunPassThrough(QueryToRun, ArgPrompt, ArgDefault)
' John Henriksen
' 2021-09-23
' Sub procedure to run a Pass-through query with argument
' in the query there must be the string 'Arg1" you want to replace
' e.g. AND APPS.OE_ORDER_LINES_ALL.ATTRIBUTE1     = 'arg1'

arg1 = VBA.InputBox(ArgPrompt, ArgPrompt, ArgDefault)

Dim db As DAO.Database
Dim qry, qryNy As String
Dim qrydefOrginal As DAO.QueryDef


Set db = CurrentDb
Set qdef = db.QueryDefs(QueryToRun)

' Save the original sql
qry = qdef.sql


' Make a new sql, here we search for the string "arg1" and replace with our arg1 value
qryNy = VBA.Replace(qry, "arg1", arg1)

' Set the new sql
qdef.sql = qryNy


DoCmd.OpenQuery QueryToRun

' Set the original sql
qdef.sql = qry


Set qdef = Nothing
Set db = Nothing


End Sub

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