在关闭时访问 VBA 防止表单记录输入 [英] Access VBA Preventing form record entry on close

查看:42
本文介绍了在关闭时访问 VBA 防止表单记录输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Access 数据库 VBA.

I am working with Access Database VBA.

我注意到如果用户填写了一些文本框,然后单击了窗口关闭按钮,表单会将其记录到记录中.

I have noticed if a user has filled a few of the text boxes in and then clicks the windows close button, the form logs that into the records.

我想知道防止表单在关闭时输入未完成记录的最佳方法是什么?

I am wondering what is the best way to prevent the form from entering the uncompleted record on close?

有几个站点指向在 beforeupdate 函数中放置代码.

There were a few sites pointing to placing a code in the beforeupdate function.

这是我试过的.

Private Sub frmRecLog_BeforeUpdate(Cancel As Integer)


DoCmd.SetWarnings False
Me.Undo

Cancel = True

Exit Sub

End Sub

这段代码对我根本不起作用..我尽力了,哈哈.

This code does not work at all for me.. I tried my best, haha.

任何事情都有帮助.

推荐答案

所以,你需要的是插入一个命令按钮Save.如果用户没有点击Save,那么它不会保存任何记录.他们将收到一条警告,指出数据未保存.您需要声明一个私有布尔变量并编写代码来保存和警告.所以完整的代码将如下所示.

So, what you need is to insert a command button Save. If user do not hit on Save then it will not save any records. They will get a warning that data is not saved. You need declare a private boolean variable and write codes to save and warning. So full code will be like below.

Option Compare Database
Option Explicit
Private blnSaveRecord As Boolean

Private Sub cmdSave_Click()
    blnSaveRecord = True
    DoCmd.RunCommand (acCmdSaveRecord)
    blnSaveRecord = False
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
 
    If Not blnSaveRecord Then
        Cancel = True
        strMsg = "Please save the record.," & _
                 vbNewLine & "or press ESC from keyboard to cancel the operation."
        Call MsgBox(strMsg, vbInformation, "Save Record")
        'Me.Undo 'You can set undo option here if you do not want to press ESC.
    End If
    
End Sub

这篇关于在关闭时访问 VBA 防止表单记录输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆