Proper event for loading subfrm procedure (1 Viewer)

AlexN

Registered User.
Local time
Today, 20:10
Joined
Nov 10, 2014
Messages
302
Hi everyone,

I have an unbound MainForm, bearing two subforms (SubFrmA, SubFrmB). The two subforms are related, so when I pick a record from SubFrmA data in SubFrmB changes showing related records. SubFrmB contains thousands of records related to every single record in SubFrmA, that’s why I want it to display only the 30 last ones of them.
I have a little piece of code (given by someone here a long time ago for another project)
Code:
Dim i As Integer
  On Error Resume Next
  DoCmd.RunCommand  acCmdRecordsGoToLast
  For i = 1 To 30
   DoCmd.RunCommand acCmdRecordsGoToPrevious
  Next i
but I don’t know under which event I should put it. Note that under the OnLoad event of the MainForm (or any of the subforms) [FONT=&quot]it doesn’t work.

Any ideas ?

Thanks
[/FONT]
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 18:10
Joined
Jul 9, 2003
Messages
16,269
The two subforms are related, so when I pick a record from SubFrmA data in SubFrmB changes showing related records.


My understanding of your arrangement is that when you click on a record in one sub form, it refreshes the related records in the other sub-form. I think you need to call the code in this procedure.
 

Gasman

Enthusiastic Amateur
Local time
Today, 18:10
Joined
Sep 21, 2011
Messages
14,217
Rather than go previous 30 times, you can just move a set number, forwards or back.?

Code:
DoCmd.GoToRecord acDataForm, Me.Name, acPrevious, 30
 

AlexN

Registered User.
Local time
Today, 20:10
Joined
Nov 10, 2014
Messages
302
That will work if run from the subform itself.
If you are trying to run this from the main form or the other subform, then you need to reference that correctly. See http://access.mvps.org/access/forms/frm0031.htm


Of course proper referencing is needed, but isn't it depending on which event (of which control) I put the code in? Since I don't know where to put it I skipped the references.
 

AlexN

Registered User.
Local time
Today, 20:10
Joined
Nov 10, 2014
Messages
302
My understanding of your arrangement is that when you click on a record in one sub form, it refreshes the related records in the other sub-form. I think you need to call the code in this procedure.


You mean in the OnClick Event of the first subform? (tried it, doesn't work)
 

AlexN

Registered User.
Local time
Today, 20:10
Joined
Nov 10, 2014
Messages
302
Rather than go previous 30 times, you can just move a set number, forwards or back.?

Code:
DoCmd.GoToRecord acDataForm, Me.Name, acPrevious, 30


Could be done but still don't know where to put the code.
 

isladogs

MVP / VIP
Local time
Today, 18:10
Joined
Jan 14, 2017
Messages
18,207
Of course proper referencing is needed, but isn't it depending on which event (of which control) I put the code in? Since I don't know where to put it I skipped the references.

I'm not sure there's much logic in that....:rolleyes:

First of all it depends whether both subforms are children of the main form or if the second subform is a sub-subform of the first

If the former is true, then follow the relevant references in the link followed by a requery if needed. If the latter, then you may just need a requery (or nothing at all)

Agree with Gasman's suggestion to simplify the code

EDIT
Use the same event as that used to filter the first subform
 

Gasman

Enthusiastic Amateur
Local time
Today, 18:10
Joined
Sep 21, 2011
Messages
14,217
Well if you put it in the click event of subforma and used the name of subformb in the GoToRecord line I would have expected that to work.

You are not using form references, so executing anywhere except subformb is not going to work.

Try from the click event of subformA

Code:
DoCmd.GoToRecord acDataForm, "SubFormB", acLast
DoCmd.GoToRecord acDataForm, "SubFormB", acPrevious, 30
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 18:10
Joined
Jul 9, 2003
Messages
16,269
EDIT
Use the same event as that used to filter the first subform


Yes that's what I said in post 3 "I think you need to call the code in this procedure."

But the OP said he'd already tried that and it didn't work?

Edit:-
You mean in the OnClick Event of the first subform? (tried it, doesn't work)

Don't understand why it does one thing and not the other? Is there's something we're not being told!
 
Last edited:

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 18:10
Joined
Jul 9, 2003
Messages
16,269
Just a thought maybe it's not an event that is filtering the subform? maybe it uses queries? need a bit more information really!
 

AlexN

Registered User.
Local time
Today, 20:10
Joined
Nov 10, 2014
Messages
302
Let’s go again

MainForm is unbound and has only a few textboxes whose (imput) values filter SubFormA (bound to table). SubFormA and SubFormB (bound to another table) are connected via a textfield, call it ID. This way, when I pick a record form (filtered) SubFormA, SubFormB shows all (hundreds, even thousands) records of its table. So far so good, everything works smoothly.
Because of the vast number of related records in SubFormB, every time I pick a (filtered) record in SubFormA, I want it to load of course all records, but display only last 30 that are more significant.

I tried to use this little piece of code I posted, with the correct references (not posted), under various events on all three forms but managed nothing. So, I’m asking for any ideas not necessarily including the code I posted.

Thank you

@Gasman
Tried these two lines of code you suggested under the OnClick event of SubFrmA. It only worked for the first click. When I picked another record (clicked again on another) SubFormB showed all related records focusing on the first
 

moke123

AWF VIP
Local time
Today, 13:10
Joined
Jan 11, 2013
Messages
3,909
Just a thought, why not set the rowsource of subform b upon selection from subform a.

"select top 30 .... order by whatever criteria."
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 18:10
Joined
Jul 9, 2003
Messages
16,269
MainForm is unbound and has only a few textboxes whose (imput) values filter SubFormA (bound to table).

When when you enter a new value in that input "textbox" or "text boxes" what event re-lists the data displayed? There must be some VBA code, something happening under the hood that creates the new sets of records?

If it's not an event, explain how it works.. .



Sent from my Pixel 3a using Tapatalk
 

theDBguy

I’m here to help
Staff member
Local time
Today, 10:10
Joined
Oct 29, 2018
Messages
21,447
Hi Alex. I’m sorry if you have tried this already and I missed it, but how about in the Current event of subform A, something like:
Code:
Me.Parent.SomeTextbox.SetFocus
Me.Parent.SubformB.SetFocus
DoCmd.GoToRecord , , acLast
DoCmd.GoToRecord , , acPrevious, 30
Just make sure you use the correct names for your objects. Hope that helps...
 

AlexN

Registered User.
Local time
Today, 20:10
Joined
Nov 10, 2014
Messages
302
Just a thought, why not set the rowsource of subform b upon selection from subform a.

"select top 30 .... order by whatever criteria."


Thank you for your answer.
"Select" would force SubFormB to load only selected records. I want it to load them all but display only last 30
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 01:10
Joined
May 7, 2009
Messages
19,227
add Timer event to SubformB.
Set the Timer Interval to 100.
add code to timer event:
Code:
Private Sub Form_Timer()
    Dim i As Integer
    On Error Resume Next
    Me.TimerInterval = 0
    DoCmd.Echo False
    With Me.Recordset
        .MoveLast
        Me.Bookmark = .Bookmark
        For i = 1 To 30
            .MovePrevious
        Next
    End With
    DoCmd.Echo True
End Sub

add code to SubformA, Current Event:
Code:
Private Sub Form_Current()
Me.Parent![SubfrmB].Form.TimerInterval = 100
End Sub
 

AlexN

Registered User.
Local time
Today, 20:10
Joined
Nov 10, 2014
Messages
302
When when you enter a new value in that input "textbox" or "text boxes" what event re-lists the data displayed? There must be some VBA code, something happening under the hood that creates the new sets of records?

If it's not an event, explain how it works.. .



Sent from my Pixel 3a using Tapatalk


Under the OnChange event of the textboxes there's a procedure that makes SubFormA "relist" data (the filter as you type procedure). Problem is, filtered SubFormA could show let's say 10 records. Picking on of them is triggering SubFormB's requery.
 

AlexN

Registered User.
Local time
Today, 20:10
Joined
Nov 10, 2014
Messages
302
@ arnelgp
@ theDBguy


Thank you both for your answers. I'll try them both and I'll provide feedback.
 

AlexN

Registered User.
Local time
Today, 20:10
Joined
Nov 10, 2014
Messages
302
@ arnelgp
@ theDBguy


You guys are my Sunday heroes. Both suggestions seem to work perfectly.

@ arnelgp Immediate click on another record in SubFormA makes SubFormB go to the first record. Should I raise the timer interval?



Thank you!!! Thank you!!! Thank you all!!!
 

Users who are viewing this thread

Top Bottom