退出表单前访问必填字段

时间:2017-05-19 16:51:10

标签: vba ms-access

我有这个BeforeUpdate代码来检查是否填写了某些字段。这些字段是必填字段,必须填写或记录不应保存。如果在没有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子中创建了Yes / No消息框。但是它不会停止子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字段之前提示用户。

2 个答案:

答案 0 :(得分: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设置条件需求,以将某些控件的tag属性设置为required。当然,如果这样做,则还需要在切换记录时在on_current事件中初始化这些标记值。 非常感谢Craig Dolphin先生

答案 1 :(得分:0)

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

如果验证成功,请保存记录,如果没有通知用户。

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