dao 记录集更新错误记录

发布于 2024-11-30 04:28:16 字数 2616 浏览 1 评论 0原文

我正在尝试创建一个可用于创建新记录或更新另一个记录的表单。目前它是通过文本框(新建或编辑)的值来完成的。该结构工作正常,但由于某种原因,当它执行编辑功能时,它将更改保存到错误的记录。例如,如果我正在编辑记录1027,当我提交它时,它会更新记录1073。它是一致的,它总是会更新相同的错误记录。编辑1000,更新1073;如果我更新 1081,它就会更新 1073,依此类推。有没有办法指定应该编辑哪条记录?是的,记录号是主键/id。代码如下:

Private Sub btnSubmit_Click()

 Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim strTable As String
    Dim strField As String
    Dim ID As Long
    Dim newID As Long

    strTable = "record_holdData"
    Set db = CurrentDb
    Set rs = db.OpenRecordset(strTable)

'button has 2 modes
If txtMode.Value = "NEW" Then

    With rs
        .AddNew
        .Fields("PO_no") = txtPONum
        .Fields("prodSupervisor") = cboProdSup
        .Fields("qaSupervisor") = cboQASup
        .Fields("labTech") = cboLabTech
        .Fields("flavor") = cboFlavor
        .Fields("lineNumber") = cboLineNumber
        .Fields("container") = cboContainer
        .Fields("package") = cboPackage
        .Fields("holdQty") = txtQty
        .Fields("productionDate") = txtProdDate
        .Fields("dateCode") = txtDatecode
        .Fields("component") = cboComponent
        .Fields("nonconformance") = cboDiscrepancy
        .Fields("foundDuring") = cboFoundAt
        .Fields("responsibility") = cboRespCode
        .Fields("comments") = txtDescription
        .Fields("rootCause") = txtRootCause
        .Fields("holdStatus") = 1
        .Fields("dateOpened") = Now()
        .Update
        .Bookmark = .LastModified
        newID = !ID
    End With

        MsgBox ("Hold information saved!")
        btnPrintTag.Enabled = True
        DoCmd.OpenReport "Holdtag", acViewPreview, , "[ID] = " & newID
        DoCmd.Close

ElseIf txtMode.Value = "EDIT" Then
    'do editing stuff
    With rs
        .Edit
        .Fields("PO_no") = txtPONum
        .Fields("prodSupervisor") = cboProdSup
        .Fields("qaSupervisor") = cboQASup
        .Fields("labTech") = cboLabTech
        .Fields("flavor") = cboFlavor
        .Fields("lineNumber") = cboLineNumber
        .Fields("container") = cboContainer
        .Fields("package") = cboPackage
        .Fields("holdQty") = txtQty
        .Fields("productionDate") = txtProdDate
        .Fields("dateCode") = txtDatecode
        .Fields("component") = cboComponent
        .Fields("nonconformance") = cboDiscrepancy
        .Fields("foundDuring") = cboFoundAt
        .Fields("responsibility") = cboRespCode
        .Fields("comments") = txtDescription
        .Fields("rootCause") = txtRootCause
        .Fields("lastEditDate") = Now()
        .Update
    End With

    MsgBox ("Information Updated")

End If

End Sub

I'm trying to have a form usable for both creating a new record or updating another. Currently it is doing it through the value of a textbox (new or edit). The structure works fine, but for some reason, when it is performing the edit function, it is saving changes to the wrong record. For instance, if I am editing record 1027, when i submit it, it'll update record 1073. Its consistent, it'll always update the same, wrong record. Edit 1000, it'll update 1073; if i update 1081, it'll update 1073, and so on. Is there a way to specify which record it should be editing? yes, the record number is the primary key/id. Heres the code:

Private Sub btnSubmit_Click()

 Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim strTable As String
    Dim strField As String
    Dim ID As Long
    Dim newID As Long

    strTable = "record_holdData"
    Set db = CurrentDb
    Set rs = db.OpenRecordset(strTable)

'button has 2 modes
If txtMode.Value = "NEW" Then

    With rs
        .AddNew
        .Fields("PO_no") = txtPONum
        .Fields("prodSupervisor") = cboProdSup
        .Fields("qaSupervisor") = cboQASup
        .Fields("labTech") = cboLabTech
        .Fields("flavor") = cboFlavor
        .Fields("lineNumber") = cboLineNumber
        .Fields("container") = cboContainer
        .Fields("package") = cboPackage
        .Fields("holdQty") = txtQty
        .Fields("productionDate") = txtProdDate
        .Fields("dateCode") = txtDatecode
        .Fields("component") = cboComponent
        .Fields("nonconformance") = cboDiscrepancy
        .Fields("foundDuring") = cboFoundAt
        .Fields("responsibility") = cboRespCode
        .Fields("comments") = txtDescription
        .Fields("rootCause") = txtRootCause
        .Fields("holdStatus") = 1
        .Fields("dateOpened") = Now()
        .Update
        .Bookmark = .LastModified
        newID = !ID
    End With

        MsgBox ("Hold information saved!")
        btnPrintTag.Enabled = True
        DoCmd.OpenReport "Holdtag", acViewPreview, , "[ID] = " & newID
        DoCmd.Close

ElseIf txtMode.Value = "EDIT" Then
    'do editing stuff
    With rs
        .Edit
        .Fields("PO_no") = txtPONum
        .Fields("prodSupervisor") = cboProdSup
        .Fields("qaSupervisor") = cboQASup
        .Fields("labTech") = cboLabTech
        .Fields("flavor") = cboFlavor
        .Fields("lineNumber") = cboLineNumber
        .Fields("container") = cboContainer
        .Fields("package") = cboPackage
        .Fields("holdQty") = txtQty
        .Fields("productionDate") = txtProdDate
        .Fields("dateCode") = txtDatecode
        .Fields("component") = cboComponent
        .Fields("nonconformance") = cboDiscrepancy
        .Fields("foundDuring") = cboFoundAt
        .Fields("responsibility") = cboRespCode
        .Fields("comments") = txtDescription
        .Fields("rootCause") = txtRootCause
        .Fields("lastEditDate") = Now()
        .Update
    End With

    MsgBox ("Information Updated")

End If

End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还如梦归 2024-12-07 04:28:16

抱歉我抓住了。问题是我基本上是在每次调用子例程时重新定义记录集。我将第二个块更改为以下内容:

ElseIf txtMode.Value = "EDIT" Then
    'do editing stuff
    Set rs = db.OpenRecordset("SELECT * FROM record_holdData WHERE ID=" & txtID)
    With rs
        .Edit
        .Fields("PO_no") = txtPONum
        .Fields("prodSupervisor") = cboProdSup
        .Fields("qaSupervisor") = cboQASup
        .Fields("labTech") = cboLabTech
        .Fields("flavor") = cboFlavor
        .Fields("lineNumber") = cboLineNumber
        .Fields("container") = cboContainer
        .Fields("package") = cboPackage
        .Fields("holdQty") = txtQty
        .Fields("productionDate") = txtProdDate
        .Fields("dateCode") = txtDatecode
        .Fields("component") = cboComponent
        .Fields("nonconformance") = cboDiscrepancy
        .Fields("foundDuring") = cboFoundAt
        .Fields("responsibility") = cboRespCode
        .Fields("comments") = txtDescription
        .Fields("rootCause") = txtRootCause
        .Fields("lastEditDate") = Now()
        .Update
    End With

Sorry i caught it. Problem was I was basically redefining the recordset each time the subroutine was called. I changed the second block to the following:

ElseIf txtMode.Value = "EDIT" Then
    'do editing stuff
    Set rs = db.OpenRecordset("SELECT * FROM record_holdData WHERE ID=" & txtID)
    With rs
        .Edit
        .Fields("PO_no") = txtPONum
        .Fields("prodSupervisor") = cboProdSup
        .Fields("qaSupervisor") = cboQASup
        .Fields("labTech") = cboLabTech
        .Fields("flavor") = cboFlavor
        .Fields("lineNumber") = cboLineNumber
        .Fields("container") = cboContainer
        .Fields("package") = cboPackage
        .Fields("holdQty") = txtQty
        .Fields("productionDate") = txtProdDate
        .Fields("dateCode") = txtDatecode
        .Fields("component") = cboComponent
        .Fields("nonconformance") = cboDiscrepancy
        .Fields("foundDuring") = cboFoundAt
        .Fields("responsibility") = cboRespCode
        .Fields("comments") = txtDescription
        .Fields("rootCause") = txtRootCause
        .Fields("lastEditDate") = Now()
        .Update
    End With
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文