Combining code isn't working

L

LDMueller

I have a form which runs the following code On Open:

Private Sub Form_Open(Cancel As Integer)
DoCmd.GoToRecord , , acLast
DoCmd.GoToRecord , , acPrevious
DoCmd.GoToRecord , , acPrevious
DoCmd.GoToRecord , , acPrevious
DoCmd.GoToRecord , , acPrevious
Me.cmdAddRecord.SetFocus
End Sub

It puts the focus on the Add Record command button and once I click this
button, it runs the following code:

Private Sub cmdAddRecord_Click()
On Error GoTo Err_cmdAddRecord_Click
Me.AllowAdditions = True
DoCmd.GoToRecord , , acNewRec
Me.SetupDate = Now
NwMtrNum.SetFocus 'Move to the NwMtrNum field.
MsgBox "New matter numbers are incremented by 10's. Note the last
matter number used and increment the number by 10."
Exit_cmdAddRecord_Click:
Exit Sub
Err_cmdAddRecord_Click:
MsgBox Err.Description
Resume Exit_cmdAddRecord_Click
End Sub

All this works fine.

My problem is I want to combine it all on the On Open Event Procedure. When
I code it like the following, it acts like it's forgetting about "going to
Previous" lines and all I see is one line to enter new data. I've even tried
putting a delay code before "Me.AllowAdditions = True" and this didn't help.

Private Sub Form_Open(Cancel As Integer)
DoCmd.GoToRecord , , acLast
DoCmd.GoToRecord , , acPrevious
DoCmd.GoToRecord , , acPrevious
DoCmd.GoToRecord , , acPrevious
DoCmd.GoToRecord , , acPrevious
Me.AllowAdditions = True
DoCmd.GoToRecord , , acNewRec
Me.SetupDate = Now
NwMtrNum.SetFocus 'Move to the NwMtrNum field.
MsgBox "New matter numbers are incremented by 10's. Note the last
matter number used and increment the number by 10."
End Sub

Can anyone help me with this?

Thank you in advance!
 
L

LDMueller

So the user can glance at the last four records entered before they enter
their new record. They need information from the previous record in order to
know what to enter for their new record (if this makes sense).
 
J

John Smith

Have you tried setting allow records first? I've not tried it to check but I
think that should solve your problem:

Private Sub Form_Open(Cancel As Integer)
Me.AllowAdditions = True
DoCmd.GoToRecord , , acLast
DoCmd.GoToRecord , , acPrevious, 4
DoCmd.GoToRecord , , acNewRec
Me.SetupDate = Now
NwMtrNum.SetFocus 'Move to the NwMtrNum field.
MsgBox "New matter numbers are incremented by 10's. Note the last matter
number used and increment the number by 10."
End Sub

Incidentally, you could set =Now() as the default value for your SetupDate column.

HTH
John
##################################
Don't Print - Save trees
 
L

Linq Adams via AccessMonster.com

This code

DoCmd.GoToRecord , , acLast
DoCmd.GoToRecord , , acPrevious, 4
DoCmd.GoToRecord , , acNewRec

and this

DoCmd.GoToRecord , , acLast
DoCmd.GoToRecord , , acPrevious
DoCmd.GoToRecord , , acPrevious
DoCmd.GoToRecord , , acPrevious
DoCmd.GoToRecord , , acPrevious
Me.AllowAdditions = True
DoCmd.GoToRecord , , acNewRec

is never going to make anything visible to the user except the new record;
it'll run too fast! There's nothing in the code to cause the execution to
pause between each GoToRecord. There are some custom functions out there,
generally named "Pause" that are designed to do this, and you could try to
find one and use it between each statement.You might also try placing a
messagebox after each statement to halt execution until the user can glance
at the record.

And whatever you use needs to be in the FormLoad event, not the FormOpen
event.
 
J

John Smith

Are you sure? I have successfully used similar code in the past to position a
continuous form, though not in conjunction with modifying AllowAdditions.

I'd agree about the event though, I hadn't spotted that.

HTH
John
##################################
Don't Print - Save trees
 
L

Linq Adams via AccessMonster.com

I'm positive! You could use

DoCmd.GoToRecord , , acLast
DoCmd.GoToRecord , , acPrevious, 4

to position it 4 records back from the last record, if you stopped at that,
but unless you do something to pause the code at this point, you're not going
to actually be able to view the record.The next line of code

DoCmd.GoToRecord , , acNewRec

will move it to the new record in a nanosecond.
 
J

John Smith

Maybe it's a version issue. I have just dug out an old database with

DoCmd.GoToRecord , , A_LAST
DoCmd.GoToRecord , , A_PREVIOUS, 2
DoCmd.GoToRecord , , A_NEWREC

in it and in A2003 it leaves the form with three records showing and the
cursor in the new record.

HTH
John
##################################
Don't Print - Save trees
 
L

Linq Adams via AccessMonster.com

I get it now; I was talking about a Single view form and you're talking about
a continuous or datasheet view form!

John said:
Maybe it's a version issue. I have just dug out an old database with

DoCmd.GoToRecord , , A_LAST
DoCmd.GoToRecord , , A_PREVIOUS, 2
DoCmd.GoToRecord , , A_NEWREC

in it and in A2003 it leaves the form with three records showing and the
cursor in the new record.

HTH
John
##################################
Don't Print - Save trees
I'm positive! You could use
DoCmd.GoToRecord , , acLast
[quoted text clipped - 4 lines]
DoCmd.GoToRecord , , acNewRec
will move it to the new record in a nanosecond.
 
J

John Smith

Ah, now I also understand why we differed. Glad it's not a version issue or I
could have had a problem waiting to jump out at me at some point! I am fairly
sure that the OP was using a continuous form so it should work for them.

John
##################################
Don't Print - Save trees
I get it now; I was talking about a Single view form and you're talking about
a continuous or datasheet view form!

John said:
Maybe it's a version issue. I have just dug out an old database with
DoCmd.GoToRecord , , A_LAST
DoCmd.GoToRecord , , A_PREVIOUS, 2
DoCmd.GoToRecord , , A_NEWREC
in it and in A2003 it leaves the form with three records showing and the
cursor in the new record.
I'm positive! You could use
DoCmd.GoToRecord , , acLast
[quoted text clipped - 4 lines]
DoCmd.GoToRecord , , acNewRec
will move it to the new record in a nanosecond.
 

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