Private Sub Command2_Click() Dim cmd As ADODB.Command Dim p1 As ADODB.Parameter Dim p2 As ADODB.Parameter Dim rstRet As ADODB.Recordset Set cmd = New ADODB.Command 'p_getMaxID为存储过程名字,有2个参数,第二个参数为Output With cmd .CommandText = "p_getMaxID" .CommandType = adCmdStoredProc Set .ActiveConnection = 你的有效连接对象 Set p1 = .CreateParameter("OrderName", adVarChar, adParamInput, 30, "asdf") Set p2 = .CreateParameter("MaxID", adInteger, adParamOutput) '如果这里加了注释的这个语句,会出错 'MsgBox .Parameters.Count .Parameters.Append p1 .Parameters.Append p2 '如果这里加了注释的这个语句,得不到Ouput返回值' Set rstRet = .Execute .Execute MsgBox p2.Value End With End Sub
[解决办法] 我以前有同样的问题,后改了下 Set cmd = New ADODB.Command Set cmd.ActiveConnection = m_Cn cmd.CommandText = strSQL 'cmd.CommandType = adCmdFile idx = m_colParameters.Count Dim varParams() As Variant ReDim varParams(idx) For idx = 1 To m_colParameters.Count ' Set objParam = cmd.CreateParameter(CStr(idx), adVariant, adParamInput) ' objParam.Value = CVar(m_colParameters.Item(idx)) ' cmd.Parameters.Append objParam varParams(idx) = CVar(m_colParameters.item(idx)) Next '*** set the parameter collection to nothing so it is not '*** used again Set m_colParameters = Nothing Set rs = cmd.Execute(, varParams)