Solved How do I set main form record by selecting sub form field? (1 Viewer)

5hadow

Member
Local time
Today, 15:39
Joined
Apr 26, 2021
Messages
89
Basically my subform is a glorified list box. I'd like to be able to select a record on it to display all of the relevant data on the main form. Subform is based on a much smaller query, but same data as main form.

Parent / Child relationships don't work because main form filters the subform (Need it to be the other way around).

I've tried this:


Code:
Private Sub Form_Current()
   If Not Me.NewRecord Then
      Me.Parent.Recordset.FindFirst "ID = " & Me.Recordset.Fields("ID")
      If Not Me.Parent.Recordset.NoMatch Then
         Me.Parent.Bookmark = Me.Parent.Recordset.Bookmark
      End If
   End If
End Sub

But I get run-time error 3021 - No current record.

When stepping trough the code there is a current record, and I know there is a matching record on main form.

Anyone know what's going on?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:39
Joined
Oct 29, 2018
Messages
21,474
Hi. Have you considered using a split form or an emulated one?
 

Josef P.

Well-known member
Local time
Today, 21:39
Joined
Feb 2, 2023
Messages
826
... and I know there is a matching record on main form.
Is the subform bound to the main form? .. then check call of Form.Current

I would be careful when using Recordset directly.
Me.Parent.Recordset.FindFirst already moves the record in the form.

A little better in my opinion is:
Code:
Dim rst As DAO.Recordset

If Not Me.NewRecord Then

   Set rst = Me.Parent.RecordsetClone '<-- use Clone
   rst.FindFirst "ID = " & Me.Recordset.Fields("ID")
   If Not rst.NoMatch Then
      Me.Parent.Bookmark = rst.Bookmark
   End If

End If

Note: I do not like accesses from the subform to the main form. I would control the record navigation in the main form also in the main form. Maybe even with an extra event that is raised by code in the subform.
 
Last edited:

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 20:39
Joined
Jul 9, 2003
Messages
16,282
I prefer a combo box on the main form.

I demonstrate the method on my blog here:-


The blog is actually about correcting a problem with the early version of the Northwind sample database, a problem which if not corrected, could result in you recording incorrect information.
 

bastanu

AWF VIP
Local time
Today, 12:39
Joined
Apr 13, 2010
Messages
1,402
I'd use RecordsetClone as suggested and also add .Form after Me.Parent. Another option would be to use Docmd.FindRecord:
Code:
Me.Parent.Form.Controls("ID").SetFocus
DoCmd.FindRecord Me.ID
Cheers,
 

5hadow

Member
Local time
Today, 15:39
Joined
Apr 26, 2021
Messages
89
Ok everyone, thank you for all of your help. After doing everything that everyone has suggested, I've given up after none of it worked. What I ended up doing was making a hidden textbox on main form, changing it's query ID field to filter based on that text box, and set oncurrent event on the subform to pass on the user ID.
This now works just fine.

Again, thanks for help everybody!
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 20:39
Joined
Jul 9, 2003
Messages
16,282
I've given up after none of it worked
Looks like you've come up with a good solution to your problem!

I thought it was worth mentioning that I'm not surprised you've had problems.Interactions between forms and subforms can be problematic as MS Access can randomly alter things and mess up what you're trying to do.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 20:39
Joined
Sep 12, 2006
Messages
15,657
Just an observation. You shouldn't expect a subform to have any influence on its parent, although your description of subform may mean something else.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:39
Joined
May 21, 2018
Messages
8,529
What you are doing is a "split form." A main form in single form view and an non-linked subform in continuous form view. You can do what you want without code using the splitform wizard.
split.jpg


However, many people here prefer
 

Users who are viewing this thread

Top Bottom