Results 1 to 4 of 4
  1. #1
    petemon is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Mar 2012
    Posts
    3

    Unhappy Fill Listbox From SQL Server Stored Procedure With Parameters

    Hi All.


    I've seen posts that had some information about this, but I just can't seem to put it together.

    I have an Access 03 database. I want to fill a multi-column listbox with the results from a stored procedure on SQL 2005 server. The stored procedure requires 2 numeric parameters. It returns 5 columns of text data.

    The sp works in SQL Management Studio and I can get data if I build a Pass-Thru query in Access (via the designer). I want to code this to re-fill the listbox based on the selection from another listbox.

    I thought it would be as easy as:
    Me.List1.RowSource = "Exec sp_QView1 1234, 2" - but that doesn't return anything, no error either.

    I'm a beginner, I've tried to adapt about 20 code clips from all over the web but can't get anything to work. Can someone please just give me a simple example of how to do this?

    Thanks in advance.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,816
    Google: Access listbox SQL stored procedure

    Review: http://www.pcreview.co.uk/forums/sql...-t1625164.html

    It looks like you are on the right track. Maybe just need the listbox Requery line in your code.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    petemon is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Mar 2012
    Posts
    3
    Thanks, I saw that article. It doesn't work and I'm beginning to think you can't populate a listbox that way (via SQL passthru).

    I tried this, doesn't work either. Query1 is a passthru query I built in the designer, but I have no idea why it would need to be in there. sp_QView2 is a stored proc I created that doesn't need any parameters, just to make it simpler for testing. I arrived at the code below based on the following:

    http://www.access-programmers.co.uk/...d.php?t=146414

    Dim db As DAO.Database
    Dim qdf As DAO.QueryDef
    Set db = CurrentDb
    Set qdf = db.QueryDefs("Query1")
    sSQL = "Exec sp_QView2"
    qdf.SQL = sSQL
    Me.List1.RowSource = sSQL
    qdf.SQL = sSQL
    Me.List1.Requery
    qdf.Close
    Set qdf = Nothing
    Set db = Nothing

  4. #4
    petemon is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Mar 2012
    Posts
    3

    Solved.

    OK, figured it out...


    Private Sub Command0_Click()

    Dim cnn As ADODB.Connection
    Dim rs As ADODB.Recordset

    Set cnn = New ADODB.Connection
    cnn.ConnectionString = "Driver={SQL Server};Server=MyServer\MyInstance;Database=MyData base;TrustedConnection=Yes;"
    cnn.CursorLocation = adUseClient
    cnn.Open

    Set rs = New ADODB.Recordset
    Set rs = cnn.Execute("EXEC sp_QView1 " & Text3 & ",2")

    Set Me.List1.Recordset = rs

    rs.Close
    Set rs = Nothing
    cnn.Close
    Set cnn = Nothing
    End Sub

Please reply to this thread with any new information or opinions.

Similar Threads

  1. write stored procedure with 'if else'
    By shital in forum Access
    Replies: 9
    Last Post: 04-11-2018, 11:47 AM
  2. Replies: 5
    Last Post: 09-26-2011, 03:58 AM
  3. Executing Oracle Stored Procedure
    By gsurfdude in forum Programming
    Replies: 0
    Last Post: 03-01-2011, 09:23 AM
  4. Replies: 3
    Last Post: 05-23-2010, 05:23 AM
  5. Replies: 0
    Last Post: 05-12-2010, 09:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums