Question : Selecting second or third record on a sub form

Hi. I have a main form which also has a subform.
This sub form randomly has 3 names in it.

I want to be able on the main form in a field select the name in the first row. if that name is equal to another field on the main form I want it to select the name in the second row and finaly if the name in the first and the second are on the main form i want it to select the name in the thrid row.

the path from the main form to the name on the sub form is:
[qryRandomSalesPersonNext subform].Form!EmpID

the field i want the name on the main form to go in is called txtRandom

The field on the main form that it checks to see if name already exists are called:
FIRSTOwner
SECONDOwner

How can i do this?
is it possible on the control of the field txtRandom to look up the 2nd row or does it always return the top row?

Answer : Selecting second or third record on a sub form

Hi,

of course you cannot set a field to a value if it already contains a formula for calculation. In this case it needs to be an empty and unbound field. And as this is not a saved field you can use the code above in the Form Current event which occurs on any record change in the main form.
Also, this code will not work if you don't have at least three records in your subform.

1. remove the
=[qryRandomSalesPersonNext subform].Form!EmpID
from txtRandom

2. copy this code into your main form:
Private Sub Form_Current()
    Dim rs As DAO.Recordset
    Set rs = Me.[qryRandomSalesPersonNext subform].Form.RecordsetClone
    rs.MoveFirst
    If Me.txtRandom = Me.FIRSTOwner Then
        rs.MoveNext
        If Me.txtRandom = Me.SECONDOwner Then
            rs.MoveNext
        End If
    End If
    If Not (rs.EOF Or rs.BOF) Then
        Me.txtRandom = rs.Fields("EmpID")
    End If
    Set rs = Nothing
End Sub

then it should work.

Cheers,

Christian
Random Solutions  
 
programming4us programming4us