通过更改组合框访问vba移至记录

时间:2019-03-06 14:01:09

标签: ms-access

一段时间以来,我一直对此有疑问,似乎无法破解。

我创建了一个单独的表单(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

3 个答案:

答案 0 :(得分:0)

在窗体的记录集中的书签和此记录集的副本在通常情况下是不同的。直接使用记录集而不是克隆:

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

答案 1 :(得分:0)

您对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

答案 2 :(得分:0)

好吧,我终于使它起作用了。对我来说,解决方案相当混乱。它涉及手动调用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