【问题标题】:Access Required Fields Before Exiting Form退出表单前访问必填字段
【发布时间】:2017-05-19 16:51:10
【问题描述】:

我有这个更新前代码来检查是否填写了某些字段。这些字段是必需的,必须填写,否则记录不应保存。如果其他字段没有填写 ID 和 Staff 字段,则弹出消息框提示(这些是必填字段等)。

Private Sub Form_BeforeUpdate(Cancel As Integer)

If Nz([ID], "") = "" Then
    MsgBox "The ID field is required.", vbExclamation, "Required Field"
    Cancel = True
End If
If Nz([Staff], "") = "" Then
    MsgBox "Staff field is required.", vbExclamation, "Required Field"
    Cancel = True
    Me.[Staff].SetFocus
End Sub

我有一个“关闭表单”按钮,如下所示:

Private Sub CmdCloseForm_Click()
    DoCmd.Close , ""
End Sub

单击此按钮时,我收到未填写字段的警告,但随后表单关闭。我想要一个是/否消息框,询问用户是否仍想关闭表单。我在 BeforeUpdate 子中创建了一个是/否消息框。但是它不会停止子 CmdCloseForm。

有没有办法创建一个消息框来确认用户是否要退出表单?

例如:

If MsgBox("Would you like to close the form still? Changes won't be saved.", vbYesNo + vbQuestion, "Warning") = vbNo Then
    Exit Sub
End If

如果我将上面的 Msgbox 放入 CmdCloseForm 函数中,它会在让用户知道他们缺少 ID/Staff 字段之前提示用户。

【问题讨论】:

  • 也许你应该在按钮点击事件中进行数据验证。

标签: vba ms-access


【解决方案1】:

我是由 Craig Dolphin 先生提出的这个解决方案,它对我有用。 他的方法是在通用模块中保存一个通用的验证函数(即不在表单模块中)。

Public Function validateform(myform As Form) As Boolean
'returns true if all required fields have data, or false if not. 
'It will also create a popup message explaining which fields need data

Dim boolresponse As Boolean
Dim strError As Variant
Dim ctl As Control
boolresponse = True
strError = Null
With myform
For Each ctl In .Controls
  With ctl
    If .Tag = "required" Then
       If .Value & "" = "" Then
            boolresponse = False
            strError = (strError + ", ") & .Name
        End If
    End If
  End With
Next ctl
End With
If strError & "" <> "" Then MsgBox "The following information must be entered first: " 
& strError, vbInformation
validateform = boolresponse
End Function

然后,对于任何绝对需要的字段,您只需将控件的 Tag 属性设置为 'required'

然后,例如,您可以从“保存”按钮或“关闭”按钮的 onclick 事件调用该函数。

Private Sub Command5_Click()
On Error GoTo Err_Command5_Click


If Me.PrimaryID & "" <> "" Then
If validateform(Me) Then DoCmd.Close
Else
DoCmd.Close
End If

Exit_Command5_Click:
Exit Sub

Err_Command5_Click:
MsgBox Err.Description
Resume Exit_Command5_Click

End Sub

这实质上是检查 auto pk 字段是否具有指示记录已创建的值。如果有,则验证必填字段。如果没有,它会在不检查的情况下关闭。如果记录已创建,则验证表单并仅在填写所有必填字段后关闭。如果需要更多数据,则会弹出一条消息,提醒用户填写哪些字段。

这样做的好处是,如果另一个字段更新为您想要触发附加要求的值,您可以使用 vba 设置条件要求,将某些控件的标记属性设置为必需。当然,如果您这样做,那么您还需要在 on_current 事件中初始化这些标记值,以便在切换记录时使用。 非常感谢 Craig Dolphin 先生

【讨论】:

    【解决方案2】:

    由于您想显示连续的弹出窗口,请完全取消自动更新并在关闭时验证。

    如果验证成功则保存记录,如果不通知用户。

    Private Sub Form_BeforeUpdate(Cancel As Integer)
        Cancel = True  'cancel auto-update
    End Sub
    
    'Validate and either Save or Close
    Private Sub CmdCloseForm_Click()
        If Me.Dirty Then
            If IsFormValidated Then
                DoCmd.RunCommand acCmdSaveRecord
            Else
                If MsgBox("Would you like to close the form still? Changes won't be saved.", vbYesNo + vbQuestion, "Warning") = vbNo Then Exit Sub
            End If
        End If
        DoCmd.Close acForm, Me.Name, acSavePrompt
    End Sub
    
    'Validation
    Private Function IsFormValidated() As Boolean
    
        IsFormValidated = True  'assume all is in order
    
        If Nz([ID], "") = "" Then
            MsgBox "The ID field is required.", vbExclamation, "Required Field"
            IsFormValidated = False
        End If
    
        If Nz([Staff], "") = "" Then
            MsgBox "Staff field is required.", vbExclamation, "Required Field"
            Me.[Staff].SetFocus
            IsFormValidated = False
        End If
    End Function
    

    【讨论】:

    • 感谢您。我曾尝试过类似的方法,但问题仍然是我会有 Y/N 消息框(“您要关闭吗?”),然后提示:“人员字段丢失。” “缺少 ID 字段。”我想要一种按以下顺序显示消息框的方法: 1. 缺少人员字段。 2. 缺少 ID 字段。 3. 是否仍要关闭此表单?
    • 另外,我刚刚尝试了您写的内容,效果很好,我只是想找出一种方法来制作“您要关闭此表单吗?”最后一个消息框。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-19
    相关资源
    最近更新 更多