Go To a Record by Primary Key (1 Viewer)

music_al

Registered User.
Local time
Today, 16:14
Joined
Nov 23, 2007
Messages
200
Hi

I have a Single Form and a Datasheet on a form which hold the same data. I sometimes need to filter the datasheet and select a record and then have the Single Form jump to that record. This isn't working using ...

Code:
DoCmd.GoToRecord , , acGoTo, Selected_Candidate

...so I want to return the Primary Key when I select the record in the datasheet and then go to that record in the single form.

How would I do this ?

Thank you in advance
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:14
Joined
Feb 28, 2001
Messages
27,338
Here's a suggestion: Create a combo box with the control wizards enabled. One of the options allows you to select a value and use it to navigate to a particular record. Build one of those and see how that code works, then try to do something similar using whatever record selection method you wanted to use.

Hint: It involves using .Recordset, .RecordsetClone and the .Bookmark property of a recordset.
 

Micron

AWF VIP
Local time
Today, 11:14
Joined
Oct 20, 2018
Messages
3,478
Single Form and a Datasheet on a form
These are 2 separate forms?
You can get the value of any field using the Current event of a form and if filtering a different form, apply that filter to it in that event.


If there's only 1 form involved, you could use a split form, but I confess I've not used them beyond just playing around so I don't know if they have any drawbacks. However, you can jump to any "main" form record simply by choosing a record in the datasheet portion. Also, navigating to a main record selects the datasheet record.
I'm curious as to why the same records are on different forms.
 

music_al

Registered User.
Local time
Today, 16:14
Joined
Nov 23, 2007
Messages
200
Yes they are 2 separate forms on the same parent form. Each form (a datasheet and a single form) have the same data source.

This is because the users wanted to see an Excel/table type view and when they select a particular record on the datasheet, they want the single form below it to jump to that record.

I managed to get this to work using GoTo Record, but when the datasheet is filtered, it doesn't work.
 

music_al

Registered User.
Local time
Today, 16:14
Joined
Nov 23, 2007
Messages
200
I created a split form which opens fine on its own, but when I drag it onto a tab, its only showing the single form and not the datasheet. Any ideas why ?
 

isladogs

MVP / VIP
Local time
Today, 16:14
Joined
Jan 14, 2017
Messages
18,259
The built-in split form doesn't work as a subform e.g. in a tabbed form
If you look at your split form in design view, you will see it is just a single form.
It has some clever trickery behind the scenes to make it work as a split form (BUT NOT when used as a subform)

However the good news is the emulated split form DOES work as a subform / in a tabbed form. See link in my last post.
 

Cahyanto

New member
Local time
Today, 08:14
Joined
Nov 1, 2018
Messages
6
You just need couples of vba code
in the datasheet form module create function like this

Code:
Function LinkData2Forms(DataSheetPrimaryCtl As Control, SingleFormName As String, PrimaryFieldName As String)
    Dim frm As Form, rc As Recordset
    If Not IsLoaded(SingleFormName) Then Exit Function
    Set frm = Forms(SingleFormName)
    Set rc = frm.RecordsetClone
    rc.FindFirst BuildCriteria(PrimaryFieldName, rc.Fields(PrimaryFieldName).Type, "=" & DataSheetPrimaryCtl.Value)
    If rc.NoMatch Then DoEvents Else frm.Bookmark = rc.Bookmark
End Function

'And then put this code in the "On Current" event in the datasheet
LinkData2Forms Me("DatasheetPrimaryControl"), "SingleFormName", "PrimaryFieldName"

hopefully can help :)
 

Users who are viewing this thread

Top Bottom