Link to home
Start Free TrialLog in
Avatar of mjelec
mjelec

asked on

Using a sql stored proc as an access combobox rowsource

So, I am trying, as the title suggests, to use a SQL stored proc as the rowsource for a combobox, and I am having a great deal of difficulty.  Here is the latest code I have tried:

Private Sub Form_Load()

    Dim conn As ADODB.Connection
    Set conn = SQLConnection()

    conn.Open

    Me!cboProject.RowSource = "EXEC Ren.up_RenManJobSelect"

    conn.Close
   
End Sub

I have also attempted to use a recordset, but that didn't work either.  Is there something I am missing?
SOLUTION
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mjelec
mjelec

ASKER

It does, and I had tried something like what you have suggested, but without the conn.execute part.

When I tried what you posted, and added parens around the "EXEC Ren.up_RenManJobSelect", I get an "Object doesn't support this property or method" error on the

Me!cboProject.Recordset = rst

line.

I tried to change it to

Me!cboProject.RowSource = rst

but that didn't work either.
Please post your Stored Procedure.
Avatar of mjelec

ASKER

ALTER PROC [Ren].[up_RenManJobSelect] @Action INT = 0
AS
    IF @Action = 0
        BEGIN
            SELECT  LTRIM(JMMCU) AS JobNo ,
                    JMDL01 AS JobDesc
            FROM    JDE.dbo.F5600006 J
            WHERE   JMRP07 = 81
                    AND JMSTYL LIKE 'J_'
                    AND ( JMRP25 = ''
                          OR RIGHT(JMMCU, 2) = RIGHT(RTRIM(JMRP25), 2)
                        )
        END
    ELSE
        BEGIN
            SELECT  Job AS UserJob
            FROM    MJE.Master.Users U
                    JOIN ( SELECT   LTRIM(JMMCU) AS JobNo ,
                                    JMDL01 AS JobDesc
                           FROM     JDE.dbo.F5600006 J
                           WHERE    JMRP07 = 81
                                    AND JMSTYL LIKE 'J_'
                                    AND ( JMRP25 = ''
                                          OR RIGHT(JMMCU, 2) = RIGHT(RTRIM(JMRP25),
                                                              2)
                                        )
                         ) J ON U.Job = J.JobNo
            WHERE   U.DomainUserID = SYSTEM_USER      
        END
It looks like the stored procedure is fine.  Please post your VB code as it stands now, so that we can see why it is failing.
Avatar of mjelec

ASKER

Private Sub Form_Load()

    Dim conn As ADODB.Connection
    Dim rs As New ADODB.Recordset
    Set conn = SQLConnection()

    conn.Open

    Set rs = conn.Execute("EXEC Ren.up_RenManJobSelect")

    Me!cboProject.Recordset = rs

    conn.Close
   
End Sub

And the error I get is Run-time '438':

Object doesn't support this property or method
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mjelec

ASKER

So, I added Set to the line of code you recommended, and I get a different error now:

Run-time error '7965':

The object you entered is not a valid Recordset property.

Here is the current code:

Private Sub Form_Load()

    Dim conn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Set conn = SQLConnection()

    conn.Open

    Set rs = conn.Execute("EXEC Ren.up_RenManJobSelect")

    Set Me!cboProject.Recordset = rs

    conn.Close
   
End Sub

The error is occurring on the Set Me!cboProject.Recordset = rs line.

I am not certain if a fire hose is what is causing the problem here, but I am open to any advice.  I can say that the data going into the combobox will be static, so the concept of a fire hose seems like it would work.  I have used what I am doing here, or something similar, to populate access forms.  At this point, I am just looking for a good way to get data into a combobox, whether it is similar to this or not.  We are trying to avoid putting the SQL code directly into the VB for Access, to avoid SQL injections and maintain security.  Beyond that, I am open to suggestions.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mjelec

ASKER

Yes, that works.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mjelec

ASKER

I did try this at one point, but I was uncertain how to add multiple columns using that method, Scott.  I need both JobNo and JobDesc for each record.
No points please:

          YourCombo.AddItem rs("JobNo") & ";" & rs("JobDesc")

Make sure your combo is setup for 2 columns.
Avatar of mjelec

ASKER

Okay, I actually found something that seems to be working, but I would love to get comments and feedback on it, let me know if I am missing anything.

Private Sub Form_Load()

    Dim qdf As DAO.QueryDef
   
    Set qdf = CurrentDb.QueryDefs("Projects")
    qdf.Connect = "ODBC;DRIVER={sql server};DATABASE=MyDb;SERVER=MyServer;Trusted_Connection=Yes;"
    qdf.SQL = "EXEC Ren.up_RenManJobSelect"

    Me.cboProject.RowSource = "Projects"

End Sub
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial