如果访问表中填写了特定的字段,则必须填写字段



我有一个访问表单,其中我想使3个字段(FYear, cboPeriodicity, cboPeriod)是强制性的,如果另一个字段cboCompliance由用户填写。

我已经尝试了下面的代码,如果这些字段中的任何一个为空,我将取消保存。并通过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

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

提前感谢。

我的代码中有一个愚蠢的错误。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

最新更新