当 运行 a DoCmd.Requery in Access VBA 时列表框记录取消选择

Listbox records deselecting when running a DoCmd.Requery in Access VBA

在我的应用中,用户从他们想要编辑的列表框中选择记录。然而,在他们能够编辑之前,还有一个额外的查询来确保他们想要更改的信息是正确的。以下是流程:

  1. 用户输入一个 Item_Number 并单击一个按钮来查询数据库中包含该 Item_Number 的所有位置。这些填充到列表框中。
  2. 用户从列表框中选择 he/she 希望编辑的位置
  3. 用户为所选项目输入新的 Last_Cost and/or 新的 Vendor_ID 并点击确认编辑
  4. 在 VBA 中,有一个针对另一个数据库的查询,以确保新的 Vendor_ID 有效
  5. 流程继续

问题是当 VBA 代码重新查询时

DoCmd.Requery (VendorNumbers)

执行,它取消选择列表框中的所有选定位置。我已经能够将问题缩小到 VBA 代码的这个确切部分。

我想知道是否有办法确保选中的记录保持选中状态,或者是否有办法存储选中的行以便在重新查询代码为 运行?

完整代码。它可能看起来有点混乱,因为我没有用 "Me."

标记任何内容
Private Sub EditInLocationsButton_Click()
 EditVendorIDFixed = Null
 EditVendorIDFixed = "               " & EditVendorIDInput

 DoCmd.Requery (VendorNumbers)
 If EditVendorIDFixed = "               " Or DCount("*", "VendorNumbers") > 0 Then
    EditResponse = MsgBox("Are you sure you want to edit the selected items?", vbYesNo, "Edit Selected Items?")
    If EditResponse = vbYes Then
        'EditItemNumberResultsList.RowSource = ""
        Dim i As Integer
        For i = 0 To EditItemNumberResultsList.ListCount - 1
            If EditItemNumberResultsList.Selected(i) Then
                LastCost = EditItemNumberResultsList.Column(4, i)
                EditID = EditItemNumberResultsList.Column(5, i)
                VendorID = EditItemNumberResultsList.Column(6, i)
                'MsgBox for verification purposes
                MsgBox (LastCost & "-" & EditID & "-" & VendorID)
                If EditLastCostInput = Null Then
                    LastCostforEditQuery = LastCost
                Else
                    LastCostforEditQuery = EditLastCostInput
                End If

                If EditVendorIDInput = Null Then
                    VendorIDforEditQuery = VendorID
                Else
                    VendorIDforEditQuery = EditVendorIDFixed
                End If
            End If
            txtEditID = EditID
            With DoCmd
                .SetWarnings False
                .OpenQuery "EditIminvlocRecords"
                .SetWarnings True
            End With
        Next i
        MsgBox ("The selected plants have been updated.")
    End If
Else
    MsgBox ("Error: The requested Vendor ID does not exist.")
End If
End Sub

下面是 VendorNumbers 查询:

SELECT dbo_apvenfil_sql.vend_no
FROM dbo_apvenfil_sql
WHERE (((dbo_apvenfil_sql.vend_no)=[Forms]![EditItemsInExistingPlants]![EditVendorIDFixed]))
ORDER BY dbo_apvenfil_sql.vend_no;

同样,这只是为了验证他们输入的 Vendor_ID 是否有效,即存在现有行。

我认为这可能是最佳选择,因为我不知道您是否能够阻止重新查询导致刷新。我会撒谎说我测试了这个 SQL 但这是我将如何执行您正在寻找的内容的基本思想。无需重新查询 VendorNumbers,您基本上只需在 VBA 中创建它 运行 即可。这也假设 EditItemsInExistingPlants 是您 运行ning 这段代码的形式,如果不是,则只需适当调整。

Private Sub EditInLocationsButton_Click()
     EditVendorIDFixed = Null
     EditVendorIDFixed = "               " & EditVendorIDInput

    Dim db          As DAO.Database
    Dim rs          As DAO.Recordset
    Dim strSQLQuery As String

    strSQLQuery = "SELECT dbo_apvenfil_sql.vend_no " & _
                  "FROM dbo_apvenfil_sql " & _
                  "WHERE (((dbo_apvenfil_sql.vend_no)= " & Me.EditVendorIDFixed & ")) " & _
                  "ORDER BY dbo_apvenfil_sql.vend_no"

    Set db = CurrentDb
    Set rs = db.OpenRecordset(strSQLQuery, dbOpenDynaset)

     If EditVendorIDFixed = "               " Or rs.Count > 0 Then
        EditResponse = MsgBox("Are you sure you want to edit the selected items?", vbYesNo, "Edit Selected Items?")
        If EditResponse = vbYes Then

            'EditItemNumberResultsList.RowSource = ""
            Dim i As Integer
            For i = 0 To EditItemNumberResultsList.ListCount - 1
                If EditItemNumberResultsList.Selected(i) Then
                    LastCost = EditItemNumberResultsList.Column(4, i)
                    EditID = EditItemNumberResultsList.Column(5, i)
                    VendorID = EditItemNumberResultsList.Column(6, i)
                    'MsgBox for verification purposes
                    MsgBox (LastCost & "-" & EditID & "-" & VendorID)
                    If EditLastCostInput = Null Then
                        LastCostforEditQuery = LastCost
                    Else
                        LastCostforEditQuery = EditLastCostInput
                    End If

                    If EditVendorIDInput = Null Then
                        VendorIDforEditQuery = VendorID
                    Else
                        VendorIDforEditQuery = EditVendorIDFixed
                    End If
                End If
                txtEditID = EditID
                With DoCmd
                    .SetWarnings False
                    .OpenQuery "EditIminvlocRecords"
                    .SetWarnings True
                End With
            Next i
            MsgBox ("The selected plants have been updated.")
        End If
    Else
        MsgBox ("Error: The requested Vendor ID does not exist.")
    End If
End Sub