如果特定字段填写在Access Form中,则必须提交Filed

时间:2015-08-13 07:30:12

标签: ms-access access-vba ms-access-2013

我在访问权限中有一个表单,如果用户填写了另一个字段FYear,我希望在其中设置3个字段(cboPeriodicitycboPeriodcboCompliance

我已经尝试了下面的代码,我取消保存,如果这些字段中的任何一个为空。并通过msgBox

提供错误消息
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Form_BeforeUpdate_Err

'Audit Data Values Set
Me.LastModifiedBy.Value = [TempVars]![currentUserID]
Me.LastModifiedTime.Value = Now()

'If Compliance value is filled, make other vaules compulsory
If IsNull(Me.cboCompliance.Value) Or Len(Me.cboCompliance.Value) <= 0 Then
    'DO nothing
Else
    Dim txtFiledisEmpty As String
    Dim wantToCancel As Boolean

    'Finiancial year ending
    If IsNull(Me.FYear.Value) Then
        wantToCancel = True
        txtFiledisEmpty = "If Compliance is selected, FY ENDING cannot be blank" & char(10)
        Me.FYear.SetFocus

    End If
    'Periodicity
    If IsNull(Me.cboPeriodicity.Value) Then
        wantToCancel = True
        txtFiledisEmpty = "If Compliance is selected, PERIODICITY cannot be blank" & char(10)
        Me.cboPeriodicity.SetFocus

    End If
    'Period
    If IsNull(Me.cboPeriod.Value) Then
        wantToCancel = True
        txtFiledisEmpty = "If Compliance is selected, PERIOD cannot be blank"
        Me.cboPeriod.SetFocus

    End If

    MsgBox "Error"
    Cancel = wantToCancel
End If

'Error Handling
Form_BeforeUpdate_Exit:
        Exit Sub
Form_BeforeUpdate_Err:
       MsgBox Error$
       Resume Form_BeforeUpdate_Exit

End Sub

我无法找到(经过大约一小时的调试)问题所在。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我的代码中有一个愚蠢的错误。 Char应该是chr

代码被修改了一点,以更好的消息。

Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Form_BeforeUpdate_Err


'If Compliance value is filled, make other vaules compulsory
If IsNull(Me.cboCompliance.Value) Or Len(Me.cboCompliance.Value) <= 0 Then
    'DO nothing
Else
    Dim txtFiledisEmpty As String
    txtFiledisEmpty = "If Compliance is selected, " & Chr(10)
    Dim wantToCancel As Boolean
    wantToCancel = False

    'Period
    If IsNull(Me.cboPeriod.Value) Then
        wantToCancel = True
        txtFiledisEmpty = txtFiledisEmpty & " > PERIOD cannot be blank" & Chr(10)
        Me.cboPeriod.SetFocus
    End If

    'Periodicity
    If IsNull(Me.cboPeriodicity.Value) Then
        wantToCancel = True
        txtFiledisEmpty = txtFiledisEmpty & " > PERIODICITY cannot be blank" & Chr(10)
        Me.cboPeriodicity.SetFocus
    End If

    'Finiancial year ending
    If IsNull(Me.FYear.Value) Then
        wantToCancel = True
        txtFiledisEmpty = txtFiledisEmpty & " > FY ENDING cannot be blank" & Chr(10)
        Me.FYear.SetFocus
    End If

    If wantToCancel Then
        MsgBox txtFiledisEmpty
    End If

    Cancel = wantToCancel
End If

'Error Handling
Form_BeforeUpdate_Exit:
        Exit Sub
Form_BeforeUpdate_Err:
       MsgBox Error$
       Resume Form_BeforeUpdate_Exit

End Sub