使用参数存储过程

时间:2012-07-11 15:26:33

标签: vba ms-access stored-procedures access-vba

以下代码返回带有硬编码值的存储过程。我需要允许74更改为从组合框中选择的任何内容。任何帮助是极大的赞赏。我在Access中使用传递查询。

Private Sub ok_Click()
    Dim objConnection As New ADODB.Connection    
    Dim objCom As ADODB.Command
    Dim provStr As String

    Set objCom = New ADODB.Command

    objConnection.Provider = "sqloledb"

    provStr = "Data Source=**;" & "Initial Catalog=IKB_QA;User Id=**;Password=**;"

    objConnection.Open provStr

    With objCom
        .ActiveConnection = objConnection
        .CommandText = "dbo.ix_spc_planogram_match 74"
        .CommandType = adCmdStoredProc

        .Execute
    End With

End Sub

3 个答案:

答案 0 :(得分:2)

您可以使用命令对象的参数字段进行更简洁的方法:

With objCom          
    .ActiveConnection = objConnection          
    .CommandText = "dbo.ix_spc_planogram_match"          
    .CommandType = adCmdStoredProc            

    .Parameters.Refresh
    .Parameters(1).Value = ComboBox1.Value

    .Execute      
End With  

答案 1 :(得分:2)

以下代码从表单中获取参数并执行存储过程。

Dim Cmd1 As ADODB.Command
Dim lngRecordsAffected As Long
Dim rs1 As ADODB.Recordset
Dim intRecordCount As Integer
'-----
Dim cnnTemp As ADODB.Connection
Set cnnTemp = New ADODB.Connection
cnnTemp.ConnectionString = "DRIVER=SQL Server;SERVER=***;" & _
"Trusted_Connection=No;UID=***;PWD=***;" & _
"Initial Catalog=IKB_QA;"
cnnTemp.ConnectionTimeout = 400
'Open Connection
cnnTemp.Open
Set Cmd1 = New ADODB.Command
Cmd1.ActiveConnection = cnnTemp

'---

With Cmd1
Dim localv As Integer
Dim inputv

localv = [Forms]![start]![Selection]![cat_code]
.CommandText = "dbo.ix_spc_planogram_match " & inputv
.CommandType = adCmdStoredProc
Set inputv = Cmd1.CreateParameter("@catcode", 3, 1, 10000, localv)
Cmd1.Parameters.Append inputv
Set rs1 = Nothing
Set rs1 = Cmd1.Execute

localv = 0
Do While Not rs1.EOF

Debug.Print rs1.Fields.Item("POG_DBKEY").Value = "POG_DBKEY"
Debug.Print rs1.Fields.Item("COMP_POG_DBKEY").Value = "COMP_POG_DBKEY"
Debug.Print rs1.Fields.Item("CURR_SKU_CNT").Value = "CURR_SKU_CNT"
Debug.Print rs1.Fields.Item("COMP_SKU_CNT").Value = "COMP_SKU_CNT"
Debug.Print rs1.Fields.Item("SKU_TOTAL").Value = "SKU_TOTAL"
Debug.Print rs1.Fields.Item("MATCHD").Value = "MATCHD"
localv = localv + 1

rs1.MoveNext
Loop
localv = localv

rs1.Close
Set rs1 = Nothing
Set rs1 = Nothing

End With
End Sub

答案 2 :(得分:0)

你可以尝试这种连接:

替换你的陈述:

.CommandText = "dbo.ix_spc_planogram_match 74"

使用:

.CommandText = "dbo.ix_spc_planogram_match " & yourComboBox.Text

假设组合框名称是yourComboBox