I have a form that shows all user accounts available in the program. The users are listed in a listbox control named "lstUsers", which has it's record source value set to gather records from tblUsers
There is a button on this form named "cmdDeleteUser" and when it is clicked, it should delete the user that is currently selected in "lstUsers". It doesn't work though, the record still remains in the table.
Here is the code for the click button action:
Here is the code for the function "DeleteUser()":
There is a button on this form named "cmdDeleteUser" and when it is clicked, it should delete the user that is currently selected in "lstUsers". It doesn't work though, the record still remains in the table.
Here is the code for the click button action:
Code:
Private Sub cmdDeleteUser_Click()
Dim MsgAnswer As Integer
If IsNull(lstUsers.Value) Then
MsgBox "No user selected.", vbCritical, "Error"
Else
MsgAnswer = MsgBox("Are you sure you want to delete this user?", vbOKCancel, "Delete User")
If MsgAnswer = 1 Then
DeleteUser
End If
End If
End Sub
Here is the code for the function "DeleteUser()":
Code:
Function DeleteUser()
Dim dbsATEVRS As DAO.Database
Dim rstUsers As DAO.Recordset
Dim strSQL As String
Dim strName As String
Set dbsATEVRS = CurrentDb
strSQL = "SELECT * FROM tblUsers"
Set rstUsers = dbsATEVRS.OpenRecordset(strSQL, dbOpenDynaset)
Do Until rstUsers.EOF
If rstUsers![UserID] = lstUsers.Value Then
rstUsers.Delete
End If
rstUsers.MoveNext
Loop
MsgBox "User Deleted", vbCritical
End Function