【问题标题】:Access vba moving to a record by changing a combo box通过更改组合框访问 vba 移动到记录
【发布时间】:2019-03-07 18:28:51
【问题描述】:

我一直有这个问题,似乎无法破解它。

我创建了一个单独的表单 (frmAdd) 来添加新记录。我想要它做的是当记录被创建到

  1. 将新记录添加到组合框 - 完成
  2. 让组合框显示到新记录 - 完成
  3. 将绑定表单移至新记录 - 不工作

该项目肯定在列表中,因为它显示了,但表单仍显示先前的记录。我使用了刷新和重新查询,但无济于事。 调用 After_Update 过程的原因是它不会自行运行(这可能是一个线索)

我在下面附上了代码和表单图像。您将在显示表单上看到一个记录显示在组合框中,但另一条记录显示在表单的其余部分。如果有任何帮助,我将不胜感激

Private Sub CboFind_AfterUpdate()
    Dim rs As DAO.Recordset

    If Not IsNull(Me.cboFind) Then
        'Save before move.
        If Me.Dirty Then
            Me.Dirty = False
        End If
        'Search in the clone set.
        Set rs = Me.RecordsetClone
        rs.FindFirst "[ClientID] = " & Me.cboFind
        If rs.NoMatch Then
        Else
            'Display the found record in the form.
            Me.Bookmark = rs.Bookmark
        End If
        Set rs = Nothing
    End If
End Sub

Private Sub cmdNew_Click()
    Dim ID As Integer
    Dim strSQL As String
    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    DoCmd.OpenForm "frmAdd", , , , , acDialog
    If NewRec = True Then
        Set db = CurrentDb
        strSQL = "SELECT clientid,sname,fname,address,suburb from TBLCLIENTS where sname = '" & pubSName & "' AND fname = '" & pubFName & "' AND address = '" & pubAddr & "' AND suburb = '" & pubSuburb & "'"
        Set rst = db.OpenRecordset(strSQL)
        ID = rst!ClientID
        Me.cboFind.SetFocus
        Me.cboFind.Value = ID
        Call CboFind_AfterUpdate
        Me.cboFind.Requery
        Set rst = Nothing
        Set db = Nothing
    End If

End Sub

Add Form

Display Form

【问题讨论】:

    标签: ms-access


    【解决方案1】:

    表单记录集中的书签和该记录集的克隆在常见情况下是不同的。直接使用记录集而不是克隆:

    Me.Recordset.FindFirst "[ClientID] = " & Me.cboFind
    

    而不是

        Set rs = Me.RecordsetClone
        rs.FindFirst "[ClientID] = " & Me.cboFind
        If rs.NoMatch Then
        Else
            'Display the found record in the form.
            Me.Bookmark = rs.Bookmark
        End If
        Set rs = Nothing
    

    【讨论】:

    • 这个代码少了很多,但不幸的是没有任何区别。还是谢谢!
    【解决方案2】:

    您对 RecordsetClone 的使用是正确的,但您可能没有组合框中的 ID。

    直接查找记录和/或尽快重新查询组合框:

    Dim rs As DAO.Recordset
    
    If NewRec = True Then
        Set db = CurrentDb
        strSQL = "SELECT clientid,sname,fname,address,suburb from TBLCLIENTS where sname = '" & pubSName & "' AND fname = '" & pubFName & "' AND address = '" & pubAddr & "' AND suburb = '" & pubSuburb & "'"
        Set rst = db.OpenRecordset(strSQL)
        ID = rst!ClientID
        Me.cboFind.Requery
        Me.cboFind.SetFocus
        Me.cboFind.Value = ID
    
        Set rs = Me.RecordsetClone
        Debug.Print "[ClientID] = " & ID
        rs.FindFirst "[ClientID] = " & ID
        If Not rs.NoMatch Then
            'Display the found record in the form.
            Me.Bookmark = rs.Bookmark
        End If
    
        Set rst = Nothing
        Set db = Nothing
    End If
    

    【讨论】:

    • 问题仍然存在,组合框包含新记录,但表单的其余部分指向不同的记录。还是谢谢
    • 如果 ID 是正确的(如图所示使用 Debug 仔细检查),那么就会发生其他事情。
    【解决方案3】:

    好吧,我终于让它工作了。对我来说,解决方案相当混乱。它涉及手动调用 cbofind_AfterUpdate 子例程,但在一组请求之后,如下所示。谢谢大家帮助。非常感谢。

        If NewRec = True Then
            Set db = CurrentDb
            strSQL = "SELECT clientid,sname,fname,address,suburb from TBLCLIENTS where sname = '" & pubSName & "' AND fname = '" & pubFName & "' AND address = '" & pubAddr & "' AND suburb = '" & pubSuburb & "'"
            Set rst = db.OpenRecordset(strSQL)
            ID = rst!ClientID
            Me.cboFind.Requery
            Me.cboFind.SetFocus
            Me.cboFind.Value = ID
            Me.Recordset.FindFirst "[ClientID] = " & Me.cboFind
            Me.Form.Refresh
            Me.Form.Requery
            Me.cboFind.Requery
            Call CboFind_AfterUpdate
        Set rst = Nothing
        Set rs = Nothing
        Set db = Nothing
    End If
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-20
      • 1970-01-01
      相关资源
      最近更新 更多