0
votes

So I'm trying to teach myself VBA again and I'm having a cople of troubles. I'm trying to add new users to a table but keep getting the above error when I click my "Update" button. The text field will be in the form of 2 letters and 5 numbers. XX11111 for example.

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
What line is it stopping on?Sam
you are missing a ' in the update command and where condition what that supposed to check??Krish

2 Answers

1
votes

you are missing a ' in this line:

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

change it to

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

and :

" WHERE LoginName=" & Me.txtLoginName.Tag

to

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

The error pretty much gives you the answer. You need 2 parameters for the function it is failing on. One thing to try is to change

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

and

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

To

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

and

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