dao recordset更新错误的记录

时间:2011-08-18 03:00:21

标签: recordset ms-access-2010

我正在尝试使用可用于创建新记录或更新其他记录的表单。目前它正在通过文本框的值(新的或编辑)来完成它。结构工作正常,但由于某种原因,当它执行编辑功能时,它会保存对错误记录的更改。例如,如果我正在编辑记录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

1 个答案:

答案 0 :(得分:0)

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

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