Go To Main Form Record from selected subform record

G

Guest

I put a datasheet subform in my mainform that works great.
The main form is a list of workers. When a worker is selected, all the
other workers for the same company and with the same craft appear in a list
in the subform.
Is there a way by selecting a different worker in the list, that the main
form record can go to his record ? I saw how to do that by linking a second
subform but the main form already shows the information. Thanks
 
T

tina

i assume your mainform RecordSource includes a primary key field that
identifies each "worker" record; i'll call it WorkerID. and that the same
primary key value is available in the subform record, too (as a foreign key
field); i'll call it WorkerIDfk. i also assume that the key value is a
number, not text. if so, try something along these lines (air code)

Dim lngID As Long
lngID = Me!WorkerIDfk

Me.Parent!WorkerID.SetFocus
DoCmd.FindRecord lngID

substitute the correct control names in place of WorkerIDfk and WorkerID.
then put the code in the subform's module, wherever you want to trigger the
search.

hth
 
D

Dirk Goldgar

Rich J said:
I put a datasheet subform in my mainform that works great.
The main form is a list of workers. When a worker is selected, all
the other workers for the same company and with the same craft appear
in a list in the subform.
Is there a way by selecting a different worker in the list, that the
main form record can go to his record ? I saw how to do that by
linking a second subform but the main form already shows the
information. Thanks

Suppose you use the Click event of the form displayed in the subform for
this. Note that this event will fire when the user clicks on the record
selector to the left of each detail record, but not AFAIK at any other
time. You could pick some other event, or use multiple events, but I'll
give an example based on the the subform's Click event and the
assumption that there is a field named EmployeeID that is the unique key
for the table on which the main form and subform are based.

'----- start of example code -----
Private Sub Form_Click()

If IsNull(Me.EmployeeID) Then Exit Sub

If Me.Dirty Then Me.Dirty = False

Me.Parent.Recordset.FindFirst "EmployeeID=" & Me.EmployeeID

End Sub
'----- end of example code -----
 
G

Guest

Thank you both for your help. Amazing simplicity in your solutions. I
modified the name of the ID field and it works brilliantly
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top