调用存储过程时“无法在集合中找到项”

时间:2012-05-18 15:07:41

标签: ms-access vba

这是我的过程:

 ALTER PROCEDURE [dbo].[insert_user]
        @username varchar(50)
     AS
     BEGIN
        IF EXISTS(SELECT userid FROM users WHERE username = @username)
            SELECT userid FROM users WHERE username = @username
        ELSE
            BEGIN
                INSERT INTO users (username) VALUES (@username)
                SELECT userid FROM users WHERE username = @username
            END
     END

这是我的VBA

Public userid As Integer
    Public dbconn As ADODB.Connection

Public Function Startup()
    Dim cmd As New ADODB.Command
    Dim rs As New ADODB.Recordset
    Dim param As New ADODB.Parameter

    Set dbconn = New ADODB.Connection
    dbconn.ConnectionString = "[redacted]"
    dbconn.Open dbconn.ConnectionString

    Set cmd = New ADODB.Command
    cmd.ActiveConnection = dbconn
    cmd.CommandType = adCmdStoredProc
    cmd.CommandText = "insert_user"

    Set param = cmd.CreateParameter("username", adVarChar, adParamInput, 50, Environ("UserDomain") & "dddddddd\" & Environ("Username"))
    cmd.Parameters.Append param

    rs.CursorType = adOpenStatic
    rs.CursorLocation = adUseClient
    rs.LockType = adLockOptimistic
    rs.Open cmd

    MsgBox (rs("userid"))
End Function

每当proc添加新用户时它都会失败但在现有用户返回时工作正常。知道为什么我在插入时没有返回任何记录?

1 个答案:

答案 0 :(得分:1)

也许这篇文章可以帮助您:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=62936

实际上,它是关于向存储过程添加一行:

...
SET NOCOUNT ON
...