Access VB中的多字段验证?

时间:2014-10-07 17:31:32

标签: vba validation ms-access access-vba

我正在寻找在访问表单上执行多字段验证的最佳方法。它就像字段是否为空一样简单,但条件是棘手的。标准不起作用,这意味着它总是移动到else,同时将所需的字段留空。我是访问的初学者,绝对是VB编码,但这是我到目前为止通过谷歌搜索:

Private Sub EventSummaryPreview_Click()
If _
    ( _
        Me.FRAUD_TYP = "Account Takeover" And _
        Len([Form]![dbo_FPI_CASE_CEO Subform]![CEO_COMPANY_ID] & vbNullString) = 0 And _
        Nz([Form]![dbo_FPI_CASE_TRANS Subform]![TRANS_DT], 0 & vbNullString) = 0 And _
        Len([Form]![dbo_FPI_CASE_TRANS Subform]![TRANS_CEO_USER_ID] & vbNullString) = 0 And _
        Nz([Form]![dbo_FPI_CASE_TRANS Subform]![TRANS_AMT], 0 & vbNullString) = 0 And _
        Len([Form]![dbo_FPI_CASE_TRANS Subform]![TRANS_DEBIT_ACCT_NBR] & vbNullString) = 0 And _
        Len(Me.NOTES & vbNullString) = 0 _
    ) Or _
    ( _
        Me.FRAUD_TYP = "Account Takeover" And _
        Len([Form]![dbo_FPI_CASE_TRANS Subform]![TRANS_DT] & vbNullString) = 0 And _
        Nz([Form]![dbo_FPI_CASE_TRANS Subform]![TRANS_AMT], 0 & vbNullString) = 0 And _
        Len([Form]![dbo_FPI_CASE_TRANS Subform]![TRANS_KEY] & vbNullString) = 0 And _
        Len(Me.NOTES & vbNullString) = 0 _
    ) _
Then
    MsgBox "Required enrichment data elements are not populated, please correct your data."
    OK = True
Else
    CurrentDb.Execute "INSERT INTO dbo_FPI_CASE_ENRICHMENT (DC_NO, ENRICH_FLG, ENRICH_FLG_TS, ENRICH_STATUS) " _
    & "VALUES (" & DC_NO & ", 1, Now(), 'OPEN')"
End If
End Sub

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

就个人而言,我发现花时间让这样的东西更具可读性是非常值得的。您可以通过声明多个布尔变量并为其指定值来实现此目的。

Dim cond1 as boolean, cond2 as boolean
Dim cond1A as boolean, cond1B as boolean, cond1C as boolean ' etc.,
Dim cond2A as boolean, cond2B as boolean, cond2C as boolean ' etc.,

' Breaking it down 1 by 1 helps you confirm that you have all of the proper criteria
' Putting these conditions in parenthesis will allow it to evaluate as true or false
cond1A = (Me.FRAUD_TYP = "Account Takeover")
cond1B = ((Len([Form]![dbo_FPI_CASE_CEO Subform]![CEO_COMPANY_ID] & vbNullString) = 0)
cond1C = (Nz([Form]![dbo_FPI_CASE_TRANS Subform]![TRANS_DT], 0 & vbNullString) = 0)

等,

cond1 = (cond1A AND cond1B AND cond1C)
cond2 = (cond2A AND cond2B AND cond2C)

If cond1 or cond2 then
    ' If either are true then do this Action
else
    ' neither are true Action
endif

答案 1 :(得分:0)

这可能是也可能不是实现我的目标的最佳方式,但这是我最终得到的代码。我将改为Archias'建议当我有另一个版本时,感谢所有提供输入的人!

If _ IsNull(Me.FRAUD_TYP.Value) Then MsgBox "Required enrichment data elements are not populated, please correct your data." OK = True ElseIf (Me.FRAUD_TYP = "Account Takeover" And IsNull([Form]![dbo_FPI_CASE_TRANS Subform]![TRANS_KEY].Value)) Then If Me.FRAUD_TYP = "Account Takeover" And IsNull([Form]![dbo_FPI_CASE_TRANS Subform]![TRANS_DT].Value) Then MsgBox "Required enrichment data elements are not populated, please correct your data." OK = True ElseIf Me.FRAUD_TYP = "Account Takeover" And IsNull([Form]![dbo_FPI_CASE_TRANS Subform]![TRANS_CEO_USER_ID].Value) Then MsgBox "Required enrichment data elements are not populated, please correct your data." OK = True ElseIf Me.FRAUD_TYP = "Account Takeover" And IsNull([Form]![dbo_FPI_CASE_CEO Subform]![CEO_COMPANY_ID].Value) Then MsgBox "Required enrichment data elements are not populated, please correct your data." OK = True ElseIf Me.FRAUD_TYP = "Account Takeover" And IsNull([Form]![dbo_FPI_CASE_TRANS Subform]![TRANS_AMT].Value) Then MsgBox "Required enrichment data elements are not populated, please correct your data." OK = True ElseIf Me.FRAUD_TYP = "Account Takeover" And IsNull([Form]![dbo_FPI_CASE_TRANS Subform]![TRANS_DEBIT_ACCT_NBR].Value) Then MsgBox "Required enrichment data elements are not populated, please correct your data." OK = True ElseIf Me.FRAUD_TYP = "Account Takeover" And IsNull(Me.NOTES) Then MsgBox "Required enrichment data elements are not populated, please correct your data." OK = True End If ElseIf (Me.FRAUD_TYP = "Account Takeover" And Not IsNull([Form]![dbo_FPI_CASE_TRANS Subform]![TRANS_KEY].Value)) Then If Me.FRAUD_TYP = "Account Takeover" And IsNull([Form]![dbo_FPI_CASE_CEO Subform]![CEO_COMPANY_ID].Value) Then MsgBox "Required enrichment data elements are not populated, please correct your data." OK = True ElseIf Me.FRAUD_TYP = "Account Takeover" And IsNull([Form]![dbo_FPI_CASE_TRANS Subform]![TRANS_DT].Value) Then MsgBox "Required enrichment data elements are not populated, please correct your data." OK = True ElseIf Me.FRAUD_TYP = "Account Takeover" And IsNull([Form]![dbo_FPI_CASE_TRANS Subform]![TRANS_AMT].Value) Then MsgBox "Required enrichment data elements are not populated, please correct your data." OK = True ElseIf Me.FRAUD_TYP = "Account Takeover" And IsNull(Me.NOTES) Then MsgBox "Required enrichment data elements are not populated, please correct your data." OK = True End If Else CurrentDb.Execute "INSERT INTO dbo_FPI_CASE_ENRICHMENT (DC_NO, ENRICH_FLG, ENRICH_FLG_TS, ENRICH_STATUS) " _ & "VALUES (" & DC_NO & ", 1, Now(), 'OPEN')" End If