访问删除 table 条记录 vba

Access delete table record vba

我有一个名为 'Users' 的 table,格式如下:

Username | Name | Surname | Email              | Role        | Company |
------------------------------------------------------------------------
jsmith   | John | Smith   | john.smith@ABC.com | Local admin | ABC     |
jdoe     | Jane | Doe     | jane.doe@DEF.com   | User        | DEF     |

我有一个名为 'User Information' 的绑定表单,我可以在其中以下一种格式循环用户:

---------------------------------       ---------------------------------
| Username | jsmith             |       | Username | jdoe               |
---------------------------------       ---------------------------------
| Name     | John               |       | Name     | Jane               |
---------------------------------       ---------------------------------
| Surname  | Smith              |       | Surname  | Doe                |
---------------------------------   >   ---------------------------------
| Email    | john.smith@ABC.com |       | Email    | jane.doe@DEF.com   |
---------------------------------       ---------------------------------
| Role     | Local admin        |       | Role     | User               |
---------------------------------       ---------------------------------
| Company  | ABC                |       | Company  | DEF                |
---------------------------------       ---------------------------------

接下来是字段名称:


Username_Label |用户名

Name_Label |姓名

Surname_Label |姓

Email_Label |电子邮件

Role_Label |角色

Company_Label |公司


我有一个用于退出表单的按钮,一个用于保存数据更改的按钮,我想添加用于删除用户的按钮。

你能给我一些建议吗?

这是'User Information'表格vba代码:

Option Compare Database
Private msaved As Boolean

Private Sub Delete_User_Click()

End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
    If msaved = False Then
        Cancel = True
        Me.Undo
        Cancel = False
    End If

End Sub

Private Sub Exit_Click()

    If MsgBox("Are you sure you want to close?", vbInformation + vbYesNo) = vbYes Then
        DoCmd.Close acForm, "User Information"
    Else
        Cancel = True
    End If

End Sub

Private Sub Save_Data_Click()

    If Me.Dirty Then

        If MsgBox("Are you sure you want to save user data?", vbInformation + vbYesNo) = vbYes Then
            msaved = True
            DoCmd.RunCommand acCmdSaveRecord

            MsgBox "User data saved.", vbInformation + vbOKOnly
        Else
             Exit Sub
        End If

        msaved = False
        DoCmd.Close acForm, "User Information"
    Else
        MsgBox "You have made no changes.", vbInformation + vbOKOnly
    End If

End Sub

Private Sub Form_Current()

    msaved = False
    txtFucus.SetFocus

    For Each ctrl In Me.Controls
        If ctrl.Tag = "CHKLEN" Then 'check the tag of the textbox control for the indicator
            ctrl.Locked = False
        End If
    Next ctrl

End Sub

我成功删除了用户。

也许有人会从中受益。干杯。 :)

Private Sub Delete_User_Click()

    If MsgBox("Are you sure you want to delete user?", vbInformation + vbYesNo) = vbYes Then
    strSQL = "DELETE * FROM [Users]" & "WHERE [Username] = '" & Me.Username & "'"
        CurrentDb.Execute strSQL, dbFailOnError
        MsgBox "User deleted.", vbInformation + vbOKOnly"
    End If

End Sub