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
 
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