开发者

Access: Send parameter from textbox to stored procedure

I am trying to create a procedure with a parameter in SQL Server 2008 and use an unbound textbox to get the result as the Control Source in Access 2010 (Access 2000 – 2003 file format .mdb).

I originally had a 开发者_Go百科dlookup as the control source of the textbox, but it’s too slow. How can I call the send the parameter from the textbox to the linked stored procedure?


I'm unsure whether this suggestion will be adequate since DLookup is too slow. You might consider showing us your DLookup statement and tell us about the fields it references and their indexes.

Anyway, I think you could use ADO to fetch a result from your SQL Server stored procedure. Here is a simplified example of one I've used to fetch the result of an sp in SQL Server Express.

Private Sub GetCenterCodes()
    Dim cnn As Object
    Dim rs As Object

    Set cnn = CreateObject("ADODB.Connection")
    cnn.ConnectionString = "DRIVER=SQL Server;SERVER=VM2003\SQLEXPRESS;" & _
        "Trusted_Connection=Yes;DATABASE=Inventory"
    cnn.Open
    Set rs = cnn.Execute("EXEC GetCenterCodes 14, 14, 501")
    Debug.Print rs(0)
    rs.Close
    Set rs = Nothing
    cnn.Close
    Set cnn = Nothing
End Sub

You could build an EXEC statement with value of your parameter. Say it's a numeric value in a text box named txtParam:

"EXEC YourSpName " & Me.txtParam

Or if it's a string, add single quotes in the EXEC:

"EXEC YourSpName '" & Me.txtParam & "'"

Instead of Debug.Print, store the value to your text box destination.

Also, I used ODBC for the connection string. You may prefer OLEDB.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜