Search code examples
vbams-accessmenusubform

How to link a sub form to a from in Access using two primary keys


In access I am trying to make a navigational subform. However, my current code only works with one primary key. Does anyone know haw to change code so that it works when the primary key is made up of two fields??

My current code is as follows;

Dim rs As DAO.Recordset

Set rs = Me.Parent.RecordsetClone

rs.FindFirst "[Fist Primary key]= " & Me![first primary key].Value & ""

If rs.NoMatch = False Then
    Me.Parent.Bookmark = rs.Bookmark
End If

Set rs = Nothing

Solution

  • Just use an AND statement

    Dim rs As DAO.Recordset
    
    Set rs = Me.Parent.RecordsetClone
    
    rs.FindFirst "[Fist Primary key]= " & Me![first primary key].Value & " AND [Second Primary key]= " & Me![second primary key].Value
    
    If rs.NoMatch = False Then
        Me.Parent.Bookmark = rs.Bookmark
    End If
    
    Set rs = Nothing