2014-10-31 23 views
0

所以我一種新的對VBA的訪問,只是碰到了與代碼一個巨大的問題更新一個記錄集。讓我解釋一下,我有一個表單,它載入當前記錄中的數據並允許用戶編輯它。目前我正在創建保存按鈕,以便已更改的數據在數據庫中正確更新。我有一個檢查,以確保文本框中的數據與記錄集中的數據相同。問題是如果數據庫中的數據爲空,它將運行,就好像它與文本框中的數據相同,即使文本框中包含「更新的組」。對我而言,這有點疏忽,有點令人失望。我的問題是有人知道解決這個問題的方法嗎?VBA:根據目前的數據

在此先感謝您的任何信息。

這裏的答案:如果RstRecSet( 「組名」)值& 「」 <> txtGroupNameEdit.Value & 「」 -Wayne

這裏是我的代碼:

Dim searchGroup As String 
Dim db As DAO.Database 
Dim RstRecSet As DAO.Recordset 
Dim Rst As DAO.Recordset 
Set db = CurrentDb 
searchGroup = txtGroupNrEdit 
Set RstRecSet = db.OpenRecordset("Select * from tblGroupHeader Where groupNum like '*" & searchGroup & "*';", dbOpenDynaset) 
Set Rst = db.OpenRecordset("Select * from tblAuditTable;", dbOpenDynaset) 

If RstRecSet("GroupName").Value <> txtGroupNameEdit.Value Or RstRecSet("PlanStartDate").Value <> txtStartDateEdit.Value _ 
Or RstRecSet("CanDate").Value <> txtCanDateEdit.Value Or RstRecSet("ClerkID").Value <> txtClerkIDEdit.Value _ 
Or RstRecSet("PopulationType").Value <> txtGroupTypeEdit.Value Or RstRecSet("COC").Value <> chkCOCEdit.Value _ 
Or RstRecSet("CDHFinancialPartner").Value <> cmbCDHEdit.Value Or RstRecSet("CDHProdOptions").Value <> chkCDHEdit.Value _ 
Or RstRecSet("MemberLevelBenefits").Value <> chkMembBeneEdit.Value Or RstRecSet("Comments").Value <> txtCommentsEdit.Value _ 
Or RstRecSet("AlsoKnownAs").Value <> txtKnownEdit.Value Or RstRecSet("BCBS").Value <> chkBCBSEdit.Value _ 
Or RstRecSet("Other").Value <> chkOtherEdit.Value Then 
    RstRecSet.Edit 
    Rst.AddNew 
     If RstRecSet("AlsoKnownAs").Value <> txtKnownEdit.Value Then 
     Rst("GroupName").Value = txtGroupNameEdit.Value 
     Rst("GroupNum").Value = txtGroupNrEdit.Value 
     RstRecSet("AlsoKnownAs").Value = txtKnownEdit.Value 
     Rst("AlsoKnownAs").Value = RstRecSet("AlsoKnownAs") 
     Rst("DateChanged").Value = Date 
     End If 
     If RstRecSet("PlanStartDate").Value <> txtStartDateEdit.Value Then 
     Rst("GroupName").Value = txtGroupNameEdit.Value 
     Rst("GroupNum").Value = txtGroupNrEdit.Value 
     RstRecSet("PlanStartDate").Value = txtStartDateEdit.Value 
     Rst("PlanStartDate").Value = RstRecSet("PlanStartDate") 
     Rst("DateChanged").Value = Date 
     End If 
     If RstRecSet("CanDate").Value <> txtCanDateEdit.Value Then 
     Rst("GroupName").Value = txtGroupNameEdit.Value 
     Rst("GroupNum").Value = txtGroupNrEdit.Value 
     RstRecSet("CanDate").Value = txtCanDateEdit.Value 
     Rst("CanDate").Value = RstRecSet("CanDate") 
     Rst("DateChanged").Value = Date 
     End If 
     If RstRecSet("PopulationType").Value <> txtGroupTypeEdit.Value Then 
     Rst("GroupName").Value = txtGroupNameEdit.Value 
     Rst("GroupNum").Value = txtGroupNrEdit.Value 
     RstRecSet("PopulationType").Value = txtGroupTypeEdit.Value 
     Rst("PopulationType").Value = RstRecSet("PopulationType") 
     Rst("DateChanged").Value = Date 
     End If 
     If RstRecSet("CDHFinancialPartner").Value <> cmbCDHEdit.Value Then 
     Rst("GroupName").Value = txtGroupNameEdit.Value 
     Rst("GroupNum").Value = txtGroupNrEdit.Value 
     RstRecSet("CDHFinancialPartner").Value = cmbCDHEdit.Value 
     Rst("CDHFinancialPartner").Value = RstRecSet("CDHFinancialPartner") 
     Rst("DateChanged").Value = Date 
     End If 
     If RstRecSet("MemberLevelBenefits").Value <> chkMembBeneEdit.Value Then 
     Rst("GroupName").Value = txtGroupNameEdit.Value 
     Rst("GroupNum").Value = txtGroupNrEdit.Value 
     RstRecSet("MemberLevelBenefits").Value = chkMembBeneEdit.Value 
     Rst("MemberLevelBenefits").Value = RstRecSet("MemberLevelBenefits") 
     Rst("DateChanged").Value = Date 
     End If 
     If RstRecSet("Other").Value <> chkOtherEdit.Value Then 
     Rst("GroupName").Value = txtGroupNameEdit.Value 
     Rst("GroupNum").Value = txtGroupNrEdit.Value 
     RstRecSet("Other").Value = chkOtherEdit.Value 
     Rst("Other").Value = RstRecSet("Other") 
     Rst("DateChanged").Value = Date 
     End If 
     If RstRecSet("ClerkID").Value <> txtClerkIDEdit.Value Then 
     Rst("GroupName").Value = txtGroupNameEdit.Value 
     Rst("GroupNum").Value = txtGroupNrEdit.Value 
     RstRecSet("ClerkID").Value = txtClerkIDEdit.Value 
     Rst("ClerkID").Value = RstRecSet("ClerkID") 
     Rst("DateChanged").Value = Date 
     End If 
     If RstRecSet("COC").Value <> chkCOCEdit.Value Then 
     Rst("GroupName").Value = txtGroupNameEdit.Value 
     Rst("GroupNum").Value = txtGroupNrEdit.Value 
     RstRecSet("COC").Value = chkCOCEdit.Value 
     Rst("COC").Value = RstRecSet("COC") 
     Rst("DateChanged").Value = Date 
     End If 
     If RstRecSet("CDHProdOptions").Value <> chkCDHEdit.Value Then 
     Rst("GroupName").Value = txtGroupNameEdit.Value 
     Rst("GroupNum").Value = txtGroupNrEdit.Value 
     RstRecSet("CDHProdOptions").Value = chkCDHEdit.Value 
     Rst("CDHProdOptions").Value = RstRecSet("CDHProdOptions") 
     Rst("DateChanged").Value = Date 
     End If 
     If RstRecSet("Comments").Value <> txtCommentsEdit.Value Then 
     Rst("GroupName").Value = txtGroupNameEdit.Value 
     Rst("GroupNum").Value = txtGroupNrEdit.Value 
     RstRecSet("Comments").Value = txtCommentsEdit.Value 
     Rst("SpecialConsideration").Value = RstRecSet("Comments") 
     Rst("DateChanged").Value = Date 
     End If 
     If RstRecSet("BCBS").Value <> chkBCBSEdit.Value Then 
     Rst("GroupName").Value = txtGroupNameEdit.Value 
     Rst("GroupNum").Value = txtGroupNrEdit.Value 
     RstRecSet("BCBS").Value = chkBCBSEdit.Value 
     Rst("BCBS").Value = RstRecSet("BCBS") 
     Rst("DateChanged").Value = Date 
     End If 
     If RstRecSet("GroupName").Value <> txtGroupNameEdit.Value Then 
     Rst("GroupName").Value = txtGroupNameEdit.Value 
     Rst("GroupNum").Value = txtGroupNrEdit.Value 
     RstRecSet("GroupName").Value = txtGroupNameEdit.Value 
     Rst("GroupName").Value = RstRecSet("GroupName") 
     Rst("DateChanged").Value = Date 
     End If 
    Rst.Update 
    RstRecSet.Update 
End If 
+0

你的代碼似乎不符合你所描述的。它看起來像你檢查,「有什麼不同?」如果是的話,除了編輯記錄之外,實際上還添加了新記錄。我不確定你實際上在問什麼。 – enderland 2014-10-31 14:03:02

+0

不要注意僅用於審計表的添加記錄。像你這樣的作品引發了它的影響; 「有什麼不同?改變不同的東西」。問題是什麼時候讓我們說「KnownAs」在記錄中沒有什麼不會觸發這個「有什麼不同?」聲明(我認爲是因爲它爲空),它只是假定它是相同的。 – SaladSnake 2014-10-31 14:22:36

+0

空值可能導致在比較過程中出現奇怪的結果,因此您需要確定記錄集字段是否爲空。即'如果RstRecSet(「GroupName」)。值&「」<> txtGroupNameEdit。價值&「」.... – 2014-10-31 14:39:43

回答

0

空值可能會導致在比較過程中出現奇怪的結果。你可以試試下面的:

If RstRecSet("GroupName").Value & "" <> txtGroupNameEdit.Value & "" Or ... 
0

但你保存按鈕只需要代碼這一行:

如果me.dirty然後me.dirty =假

爲什麼你需要這一切其他代碼和檢查?當您以訪問形式編輯數據時,數據不會更改或提交到表格,直到您允許保存數據。而更重要的是,如果控件沒有改變,那麼該控件的事件代碼不會運行。因此,實際上你不需要編寫大量代碼是浪費大量時間。

而且你當然可以(也應該)採取額外的代碼,更新事件之前進行檢查的形式值和日期。此事件發生在表單中的數據寫入表之前,因此您可以添加代碼以測試空文本框,或者在將數據寫回到表之前,具有非常複雜的代碼和規則來測試數據。

您重新發明這裏沒有方向盤,浪費了寶貴的開發人員的時間。您保存按鈕只需要上述一行代碼即可保存數據。

爲了防止保存時,您都會只需將測試和驗證碼更新事件之前進行的形式,該事件將不觸發,如果用戶沒有修改任何數據。也許你解釋一下數據問題的驗證是什麼。保存按鈕不是必需的,並且請記住,ctrl-s,ctrl-f4和其他許多關鍵字都會保存數據,但在所有情況下,更新事件之前的測試代碼都會運行並觸發。

+0

Hello Albert,我需要代碼檢查以查看用戶是否更改了表單中的數據。讓我解釋。當表單加載時,它會加載記錄集中的數據,讓我們說A.然後用戶可以選擇編輯數據,然後他們可以點擊「保存」。記錄更新之前的檢查僅用於確保用戶實際上已更改文本框中的值。 不幸的是,這涉及到我添加了很多代碼。感謝您的時間,並讓我知道這個骯髒的財產,我真的不知道它存在。 謝謝, – SaladSnake 2014-11-05 16:16:32