Stored procedure with parameters in Access

时间:2015-07-31 20:34:12

标签: sql vba stored-procedures sql-server-2012 ms-access-2013

I have this stored procedure that I run in Access.

Private Sub ReviewTrns_Click()
    Dim cdb As DAO.Database, qdf As DAO.QueryDef
        Set cdb = CurrentDb
        Set qdf = cdb.CreateQueryDef("")

        qdf.Connect = cdb.TableDefs("dbo_FreeShipping").Connect
        qdf.SQL = "EXEC dbo.UpdateTrns"

        qdf.ReturnsRecords = False
        qdf.Execute dbFailOnError
        Set qdf = Nothing
        Set cdb = Nothing
MsgBox "Records Updated!"

End Sub

It works perfectly. dbo_FreeShipping is a linked table, dbo.UpdateTrns is a stored procedure. I also want to add input value for parameter @variable1

Could it be done with message box input?

2 个答案:

答案 0 :(得分:0)

只需将用户输入的结果连接到sql调用即可。尝试类似:

Private Sub ReviewTrns_Click()

  Dim input As String
  input = InputBox("Enter variable1") //get the user input

  Dim cdb As DAO.Database, qdf As DAO.QueryDef
  Set cdb = CurrentDb
  Set qdf = cdb.CreateQueryDef("")

  qdf.Connect = cdb.TableDefs("dbo_FreeShipping").Connect
  qdf.SQL = "EXEC dbo.UpdateTrns @variable1='" & input & "'" // concatenate parameter

  qdf.ReturnsRecords = False
      qdf.Execute dbFailOnError
      Set qdf = Nothing
      Set cdb = Nothing

  MsgBox "Records Updated!"

End Sub

答案 1 :(得分:0)

我从gba中的警告框获取输入变量的goto方法是InputBox()。例如:

Public myStr as String

public sub GetInputStr()
    myStr = InputBox("Enter your variable here")
end sub

然后您应该能够在模块中的其他子设备中使用myStr。希望这有帮助!