所以我一種新的對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
你的代碼似乎不符合你所描述的。它看起來像你檢查,「有什麼不同?」如果是的話,除了編輯記錄之外,實際上還添加了新記錄。我不確定你實際上在問什麼。 – enderland 2014-10-31 14:03:02
不要注意僅用於審計表的添加記錄。像你這樣的作品引發了它的影響; 「有什麼不同?改變不同的東西」。問題是什麼時候讓我們說「KnownAs」在記錄中沒有什麼不會觸發這個「有什麼不同?」聲明(我認爲是因爲它爲空),它只是假定它是相同的。 – SaladSnake 2014-10-31 14:22:36
空值可能導致在比較過程中出現奇怪的結果,因此您需要確定記錄集字段是否爲空。即'如果RstRecSet(「GroupName」)。值&「」<> txtGroupNameEdit。價值&「」.... – 2014-10-31 14:39:43