2014-10-07 48 views
0

我正在尋找在訪問表單上執行多字段驗證的最佳方法。它是一樣簡單的領域是空的或不,但條件是棘手的。標準不起作用,這意味着它始終移動到其他部分,而將所需字段留空。我在訪問一個初學者,肯定是在VB編碼,但這裏是我通過谷歌搜索迄今:Access 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 

任何幫助深表感謝。

+0

這很難讀。爲什麼不使用嵌套的If語句? – 2014-10-07 17:44:15

+0

你能舉個例子嗎?我一直在測試代碼,看起來IF語句中的任何超過2個條件都沒有讀入條件。 – 2014-10-07 17:44:59

+0

我不認爲你需要vbnullstring。並且如果您正在檢查空字符串或null。嘗試isnothing(object.value)或object.value =「」 – 2014-10-07 17:49:13

回答

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 
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