Send focus to beginning of from after click of add new button

J

Jacqueline

I have installed an add new recod button on my form (Access 2003), I would
like to send the user to the top of the new record once the form goes to new,
I have tried a goto control macro but cannot get it to work, and it interfers
with going to a new record. Below is the VB code for the button, and anyone
tell me either how to create the correct macro and what event it would sit
on, or the code to allow the form to send the user to the beginning of the
form once it goes to new.
The name of the control I to goto is: "Level"
Thanks in advance for any help offered


Private Sub cmdNew_Click()
On Error GoTo Err_cmdNew_Click


DoCmd.GoToRecord , , acNewRec

Exit_cmdNew_Click:
Exit Sub

Err_cmdNew_Click:
MsgBox Err.Description
Resume Exit_cmdNew_Click

End Sub
 
J

Jeanette Cunningham

Hi Jacqueline,
I have found that you can usually do this without any code or macros by
using the tab order of the form.
Change the tab order of the detail section of the form so that the first
control in the tab order is the one that you want the cursor to go to when
they click the new record button.
If you need code, use
Me.ControlName.SetFocus

Note: Use the correct name of the control for ControlName
Using SetFocus can easily cause an error if there is some reason why access
can't set focus to that control, so include this in the error handling as
shown below.


You also need some code to save the record before you go to a new record.

Private Sub cmdNew_Click()
On Error GoTo Err_cmdNew_Click

If Me.Dirty = True Then
Me.Dirty = False
End If
DoCmd.GoToRecord , , acNewRec
'if needed, uncomment the line below
'Me.NameOfControl.SetFocus

Exit_cmdNew_Click:
Exit Sub

Err_cmdNew_Click:
If Err.Number = 2110 Then
On Error Resume Next
Else
MsgBox Err.Description
Resume Exit_cmdNew_Click
End If

End Sub



Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
J

Jeanette Cunningham

Slight change in error handling

replace
If Err.Number = 2110 Then
On Error Resume Next

with

If Err.Number = 2110 Then
Resume Next

In other words, remove the 2 words - On Error



Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
L

Linq Adams via AccessMonster.com

Jeanette said:
I have found that you can usually do this without any code or macros by
using the tab order of the form.
Change the tab order of the detail section of the form so that the first
control in the tab order is the one that you want the cursor to go to when
they click the new record button.

Not true, Jeanette. The Tab Order has nothing to do with which control gets
the focus when moving from one record to another record, be it an existing or
a new record. Focus on the new record will be on the control that last had
focus on the previous record. Since you're moving to a new record by clicking
on a custom command button, that button will have the focus on the new record.
You absolutely do have to use code such as you posted to have focus in a
given control when moving to the new record.
You also need some code to save the record before you go to a new record.

Why? The act of moving to another record will automatically cause Access to
save the original record. There is no need to explicitly save the original
record.
 
J

Jeanette Cunningham

You're so right, Linq. Don't know what I was thinking.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 

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