运行时错误' 3061':参数太少。预期2

时间:2015-04-30 12:17:24

标签: vba access-vba

所以我试图再次教自己VBA,而且我有很多麻烦。我尝试将新用户添加到表中,但是当我点击我的"更新"时,我仍然遇到上述错误。按钮。文本字段将采用2个字母和5个数字的形式。例如XX11111。

Private Sub cmdAdd_Click()
'when we click on button Add there are two options
'1. for insert
'2. for update
If Me.txtLoginName.Tag & "" = "" Then

    'add data to table
    CurrentDb.Execute "INSERT INTO tblUsers(LoginName,UserName,Rank) " & _
            " VALUES('" & Me.txtLoginName & "','" & Me.txtUsername & "','" & Me.cboRank & "')"
Else

       CurrentDb.Execute "UPDATE tblUsers " & _
            "set LoginName=" & Me.txtLoginName & "'" & _
            ", UserName='" & Me.txtUsername & "'" & _
            ", Rank='" & Me.cboRank & "'" & _
            " WHERE LoginName=" & Me.txtLoginName.Tag
End If
'clear form
cmdClear_Click

'refresh data in list on form
frmModifyUsersSub.Form.Requery
End Sub

2 个答案:

答案 0 :(得分:1)

你错过了一个'在这一行:

" set LoginName=" & Me.txtLoginName & "'" & _

将其更改为

" set LoginName='" & Me.txtLoginName & "'" & _

和:

" WHERE LoginName=" & Me.txtLoginName.Tag

" WHERE (LoginName='" & Me.txtLoginName.Tag & "')" ' and I don't know if this your intended where condition.

答案 1 :(得分:0)

错误几乎可以给你答案。它失败的功能需要2个参数。要尝试的一件事是改变

CurrentDb.Execute "INSERT INTO tblUsers(LoginName,UserName,Rank) " & _
        " VALUES('" & Me.txtLoginName & "','" & Me.txtUsername & "','" & Me.cboRank & "')"

CurrentDb.Execute "UPDATE tblUsers " & _
        "set LoginName=" & Me.txtLoginName & "'" & _
        ", UserName='" & Me.txtUsername & "'" & _
        ", Rank='" & Me.cboRank & "'" & _
        " WHERE LoginName=" & Me.txtLoginName.Tag

CurrentDb.Execute "INSERT INTO tblUsers(LoginName,UserName,Rank) " & _
        " VALUES('" & Me.txtLoginName & "','" & Me.txtUsername & "','" & Me.cboRank & "')",dbFailOnError 

CurrentDb.Execute "UPDATE tblUsers " & _
        "set LoginName='" & Me.txtLoginName & "'" & _
        ", UserName='" & Me.txtUsername & "'" & _
        ", Rank='" & Me.cboRank & "'" & _
        " WHERE LoginName='" & Me.txtLoginName.Tag & "'", dbFailOnError