Go to last record on subform

S

Saylindara

I have put the following in the On Load event on a subform, but the subform
still opens at the first record. What am I doing wrong?

Private Sub Form_Load()
If Not Me.NewRecord Then
RunCommand acCmdRecordsGoToLast
End If


End Sub
 
T

Tom van Stiphout

On Sun, 18 Jan 2009 08:16:01 -0800, Saylindara

It worked for me in the A2007 Northwind application, Order Details
form.
Of course since it is in Form_Load it will run only once, not every
time you select another record in the parent form (in case that's what
you wanted).

I do think DoCmd.RunCommand and RunCommand are equivalent.

-Tom.
Microsoft Access MVP
 
S

Saylindara

This is obviously the problem then. I need something that works for every
record in the mainform. Is there a solution?
 
T

tina

first of all, i'm guessing that you put in the "If Not Me.NewRecord" bit
because you get an error if there are no existing records in the subform,
correct? instead, try

On Error Resume Next
DoCmd.RunCommand acCmdRecordsGoToLast

that should be fine when the subform loads. but if the subform is linked to
the mainform, the code will not run when you move from one *mainform* record
to another, so the subform will just go to the first record of its'
recordset.

to accomplish that, you'll need to put code in the *mainform's* Current
event. i've found it a bit harder to get navigation code in a mainform to
work on the recordset in a subform, but you could try

On Error Resume Next
Me!SubformControlName.SetFocus
With Me!SubformControlName
DoCmd.RunCommand acCmdRecordsGoToLast
End With
Me!SomeControlInTheMainForm.SetFocus

hth
 
S

Saylindara

Thank you everybody for your help.
Putting the code in the On Current Event certainly put the record selector
indicator at the last record but the form did not scroll to the last record.
At the moment I've only got a few records and it is not a problem but sooner
or later there will be a hundred or more. Is there a way to make the form
scroll to the last record.
 
T

tina

to get that, i think you're going to have to SetFocus to a control in the
subform. then you can immediately SetFocus back to a control in the
mainform, if you want.

hth
 
S

Saylindara

I'm confused and possibly missing the point.
Surely the idea of having a subform is that you can enter data. Does
everyone have to scroll through hundreds of records to get to the end before
they can enter anything? Also I want people looking at the subform to see the
latest entries. I would have thought this was pretty basic.
Please forgive me, Ive never built a database before and I confuse easily.
If setting focus to the subform and back to the mainform is the answer,
would you please explain in basic idiot how you do that.
 
J

John W. Vinson

I'm confused and possibly missing the point.
Surely the idea of having a subform is that you can enter data. Does
everyone have to scroll through hundreds of records to get to the end before
they can enter anything?

Of course not. A basic generic subform has navigation buttons (at the bottom
left); click the *> button to jump to the new record.
Also I want people looking at the subform to see the
latest entries. I would have thought this was pretty basic.

You can put VBA code or a Macro in the form's load event to go to the new
record, or to the last record.
 
T

tina

okay. your previous post said that putting the "go to last" command in the
Current event worked to move the cursor to the last record. so just move the
focus to a control in the subform, as

Me!SubformControlName.Form!NameOfControlInSubform.SetFocus

using the correct names of your subform control, and the control in the
subform, of course. then run the "go to last" commmand, then set focus to a
mainform control (if you want).

hth
 

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